首页 > 其他 > 详细

tab栏切换效果

时间:2020-06-05 22:35:13      阅读:85      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style-type: none;
        }
        
        .tab {
            width: 978px;
            margin: 100px auto;
        }
        
        .tab_list {
            height: 39px;
            border: 1px solid #ccc;
            background-color: #f1f1f1;
        }
        
        .tab_list li {
            float: left;
            height: 39px;
            line-height: 39px;
            padding: 0 20px;
            text-align: center;
            cursor: pointer;
        }
        
        .tab_list .current {
            background-color: #c81623;
            color: #fff;
        }
        
        .item_info {
            padding: 20px 0 0 20px;
        }
        
        .item {
            display: none;
        }
    </style>
</head>

<body>
    <div class="tab">
        <div class="tab_list">
            <ul>
                <li class="current">商品介绍</li>
                <li>规格与包装</li>
                <li>售后保障</li>
                <li>商品评价(50000)</li>
                <li>手机社区</li>
            </ul>
        </div>
        <div class="tab_con">
            <div class="item" style="display: block;">
                商品介绍模块内容
            </div>
            <div class="item">
                规格与包装模块内容
            </div>
            <div class="item">
                售后保障模块内容
            </div>
            <div class="item">
                商品评价(50000)模块内容
            </div>
            <div class="item">
                手机社区模块内容
            </div>

        </div>
    </div>
    <script>
        // 1 获取元素
        var tab_list = document.querySelector(‘.tab_list‘);
        var lis = tab_list.querySelectorAll(‘li‘);
        var items = document.querySelectorAll(‘.item‘);
        // 2 for循环 获取点击事件
        for (var i = 0; i < lis.length; i++) {

            // 设置li 索引号
            lis[i].setAttribute(‘index‘, i);
            lis[i].onclick = function() {
                // 取消所有li 属性
                for (var i = 0; i < lis.length; i++) {
                    lis[i].className = ‘‘;
                }
                // 留下自己的属性
                this.className = ‘current‘;

                // 2 下面的显示内容模块
                var index = this.getAttribute(‘index‘);
                console.log(index);
                // 取消所有 item属性隐藏
                for (var i = 0; i < items.length; i++) {
                    items[i].style.display = ‘none‘;
                }
                // 当前的显示出来
                items[index].style.display = ‘block‘;
            }
        }
    </script>
</body>

</html>

tab栏切换效果

原文:https://www.cnblogs.com/ericblog1992/p/13052233.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!