其他分享
首页 > 其他分享> > JS:手机底部导航切换效果

JS:手机底部导航切换效果

作者:互联网

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title></title>
</head>

<body>
    <style>
        .content {
            width: 350px;
            height: 500px;
            background-color: goldenrod;
        }

        .view {
            width: 350px;
            height: 400px;
            background-color: palegreen;
        }

        .tabbar {
            width: 350px;
            height: 100px;
            background-color: goldenrod;
            display: flex;
            justify-content: flex-start;
            flex-wrap: nowrap;
        }

        .btn {
            width: 25%;
            background-color: gainsboro;
            cursor: pointer;
            position: relative;
        }

        .btn:first-child {
            background-color: skyblue;
        }

        .viewconent {
            width: 350px;
            height: 400px;
            background-color: palegreen;
            display: none;
        }

        .viewconent:first-child {
            display: block;
        }

        #img1 {
            visibility: visible;
            display: block;
            height: 40px;
            width: 40px;
        }

        #img2 {
            position: absolute;
            visibility: hidden;
            display: block;
            height: 40px;
            width: 40px;
            top: 20px;
        }
    </style>

    <div class="content">
        <div class="view">
            <div class="viewconent">home</div>
            <div class="viewconent">car</div>
            <div class="viewconent">my</div>
            <div class="viewconent">set</div>
        </div>
        <div class="tabbar">
            <div class="btn">
                <span>home</span>
                <img id='img1' src="./icon_grey/homefill.png" alt="">
                <img id="img2" src="./icon_skyblue/homefill.png" alt="">
            </div>
            <div class="btn">
                <span>car</span>
                <img id='img1' src="./icon_grey/cartfill.png" alt="">
                <img id="img2" src="./icon_skyblue/cartfill.png" alt="">
            </div>
            <div class="btn">
                <span>my</span>
                <img id='img1' src="./icon_grey/myfill.png" alt="">
                <img id="img2" src="./icon_skyblue/myfill.png" alt="">
            </div>
            <div class="btn">
                <span>set</span>
                <img id='img1' src="./icon_grey/setting-fill.png" alt="">
                <img id="img2" src="./icon_skyblue/setting-fill.png" alt="">
            </div>
        </div>
    </div>

    <script>
        var tabbarbtns = document.querySelectorAll(".tabbar .btn");//获取每个底部的类名
        tabbarbtns.forEach((el, index) => {
            // console.log(el);//测试是否成功引入的实参
            el.onclick = function () {
                tabbarbtns.forEach((el2) => { el2.style.backgroundColor = "gainsboro"; })//获取点击事件,遍历每个底部分别设置背景颜色
                el.style.backgroundColor = "skyblue";//将点击的部分背景颜色变为蓝色
                //1.获取点击了第x个按钮index/el.indexof1()
                //2.把内容区域的所有的隐藏 把内容区域的第x个显示
                var viewconents = document.querySelectorAll(".viewconent");//获取所有的视图页面
                viewconents.forEach((el3) => { el3.style.display = "none" });//将所有的都设置为不展示
                viewconents[index].style.display = "block";//将点击的部分对应的视图设置为可看见
                var img2 = document.querySelectorAll('#img2');
                img2.forEach((el4,index) => { el4.style.visibility = "hidden" });
                img2[index].style.visibility = "visible";
            }
        })
    </script>
</body>

</html>
*图片地址使用的是本地相对路径

标签:style,color,JS,width,切换,background,height,导航,display
来源: https://www.cnblogs.com/LIXI-/p/16479754.html