原生JS实现tab栏切换和拖拽效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #btns {
            width: 300px;
            height: 38px;
            list-style: none;
            border: 1px solid #f00;
            display: flex;
            margin: 0;
            padding: 0;
        }
        
        #btns li {
            flex: 1;
            line-height: 38px;
            text-align: center;
        }
        
        #content {
            width: 300px;
            height: 300px;
            background-color: pink;
            margin: 0;
            padding: 0;
            border: 1px solid green;
        }
        
        #content #count {
            display: none;
        }
    </style>
</head>

<body>
    <!--tab栏-->
    <div class="relative" style="position: relative; width: 500px; height: 500px; left: 50%; top: 50%; background-color: #f00;">
        <div id="dox" style="position: absolute; top: 50%; left: 50%;">
            <ul id="btns">
                <li id="btn">1</li>
                <li id="btn">2</li>
                <li id="btn">3</li>
            </ul>
            <ul id="content">
                <li id="count" style="display: block;">1</li>
                <li id="count">2</li>
                <li id="count">3</li>
            </ul>
        </div>
    </div>
    <input type="text" id="ipt" style="position: absolute;">
    <script>
        let btns = document.querySelectorAll('#btn');
        let count = document.querySelectorAll('#count');
        console.log(btns);
        console.log(count);
        btns.forEach((item, index) => {
            item.onclick = (() => {
                console.log(item, index);
                btns.forEach((a) => {
                        // a.style.color = 'black'
                    })
                    // item.style.color = 'red'
                count.forEach((b) => {
                    b.style.display = 'none'
                })
                count[index].style.display = 'block'
            })
        })

        // 拖拽事件
        let dox = document.querySelector('#dox');
        console.log(dox);
        dox.onmousedown = ((event) => {
            let disx = event.clientX - dox.offsetLeft;
            let disy = event.clientY - dox.offsetTop;
            console.log(disx);
            console.log(disy);
            document.onmousemove = ((event) => {
                dox.style.left = event.clientX - disx + 'px';
                dox.style.top = event.clientY - disy + 'px';
                console.log(dox.style.left);
                console.log(dox.style.top);
            })
            document.onmouseup = ((b) => {
                document.onmousemove = null
            })
        })
        let ipt = document.querySelector('#ipt');
        console.log(ipt);
        ipt.onmousedown = ((event) => {
                let disX = event.clientX - ipt.offsetLeft;
                let disY = event.clientY - ipt.offsetTop;
                console.log(disX);
                console.log(disY);
                document.onmousemove = ((a) => {
                    ipt.style.left = a.clientX - disX + 'px';
                    ipt.style.top = a.clientY - disY + 'px';
                })
                document.onmouseup = ((b) => {
                    document.onmousemove = null
                })
            })
            // let声明了可以不赋值但是const一旦声明就必须赋值
        let aa;
        console.log(aa);
        // const bb;
        // console.log(bb);
    </script>
</body>

</html>
好的,我可以为您提供一个简单的例子: HTML: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h3>Tab 1 Content</h3> <p>Some text...</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2 Content</h3> <p>Some text...</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3 Content</h3> <p>Some text...</p> </div> ``` CSS: ```css /* 隐藏所有tab内容 */ .tabcontent { display: none; } /* 标签页按钮样式 */ .tab button { background-color: #eee; border: none; color: black; padding: 10px 20px; cursor: pointer; } /* 激活的标签页按钮样式 */ .tab button.active { background-color: #ccc; } /* 标签页内容样式 */ .tabcontent { padding: 20px; border: 1px solid #ccc; } /* 第一个标签页默认显示 */ #tab1 { display: block; } ``` JavaScript: ```javascript function openTab(event, tabName) { // 获取所有的 tab 内容 var tabcontent = document.getElementsByClassName("tabcontent"); // 隐藏所有的 tab 内容 for (var i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 获取所有的 tab 按钮 var tablinks = document.getElementsByClassName("tablinks"); // 将所有的 tab 按钮样式设置为非激活状态 for (var i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // 显示当前选中的 tab 内容 document.getElementById(tabName).style.display = "block"; // 设置当前选中的 tab 按钮为激活状态 event.currentTarget.className += " active"; } ``` 当用户点击任意一个标签页按钮时,`openTab`函数会被调用,隐藏所有标签页内容,将当前标签页内容设置为显示状态,同时将当前标签页按钮样式设置为激活状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值