JS基础之DOM节点和网页元素基本操作练习(侧边栏菜单,滚动菜单,tab选项卡,背景切换)

练习1:侧边栏菜单

  效果图:

 

  code:

<!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>侧边栏菜单</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        aside{
            width: 200px;
            background-color: #cacaca;
            color: #ffffff;
        }
        aside>ul>li{
            height: 40px;
            border-bottom: 1px solid #ffffff;
            text-align: center;
            overflow: hidden;
            user-select: none;
            transition: all 0.3s linear;
        }

        aside>ul>li>p{
            height: 40px;
            width: 100%;
            background-color: #000000;
            line-height: 40px;
        }

        .ndMenu li{
            height: 30px;
            line-height: 30px;
            width: 100%;
            background-color: #cacaca;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <aside>
        <ul>
            <li>
                <p>菜单1</p>
                <ul class="ndMenu">
                    <li>菜单1-1</li>
                    <li>菜单1-2</li>
                </ul>
            </li>
            <li>
                <p>菜单2</p>
                <ul class="ndMenu">
                    <li>菜单2-1</li>
                    <li>菜单2-2</li>
                </ul>
            </li>
            <li>
                <p>菜单3</p>
                <ul class="ndMenu">
                    <li>菜单3-1</li>
                    <li>菜单3-2</li>
                </ul>
            </li>
            <li><p>菜单4</p></li>
        </ul>
    </aside>

    <script>
        let stMenu = document.querySelectorAll("aside>ul>li>p");

        stMenu.forEach(x=>{
            x.onclick=function(){
                if(hasNextE(this)){
                    // 获取点击的p标签所在的li标签
                    let high = this.parentElement;
                    if(high.style.height == "" || high.style.height == "40px"){
                        // 每次展开2级菜单时,关闭其他菜单
                        closeAllStMenu();
                        // 获取2级菜单个数,计算2级菜单总高度
                        let subHigh = high.lastElementChild.children.length * 30;
                        // 修改1级菜单高度
                        high.style.height = 40+subHigh+"px";
                    }else{
                        high.style.height = "40px";
                    }
                }
                    
                
            }
        });

        // 判断当前元素是否有下一个兄弟元素
        function hasNextE(e){
            if(e.nextElementSibling == null){
                return false;
            }
            return true;
        }

        // 关闭所有一级菜单
        function closeAllStMenu(){
            stMenu.forEach(y=>{
                y.parentNode.style.height = "40px";
            });
        }

    </script>
</body>
</html>

练习2:滚动菜单

  效果图:

 

  code:

<!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>
    <link rel="stylesheet" href="./iconfont.css">
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html{
            width: 100%;
            height: 100%;
            background-color: #2D2C41;
        }
        #main{
            margin: 0 auto;
            width: 360px;
            background-color: #cacaca;
            color: #000000;
            border-radius: 4px;
            overflow: hidden;
        }
        #main>ul>li{
            height: 40px;
            border-bottom: 1px solid #cacaca;
            text-align: left;
            overflow: hidden;
            user-select: none;
            transition: all 0.3s linear;
        }

        #main>ul>li>div{
            height: 40px;
            width: 100%;
            background-color: #ffffff;
            line-height: 40px;
            box-sizing: border-box;
            padding-left: 20px;
            position: relative;
            transition: all 0.2s linear;
        }

        .ndMenu li{
            height: 40px;
            line-height: 40px;
            width: 100%;
            color: #ffffff;
            background-color: #444359;
            cursor: pointer;
            box-sizing: border-box;
            padding-left: 20px;
            border-bottom: 1px solid #ffffff30;
            transition: all 0.3s linear;
        }

        .ndMenu li:hover{
            background-color: #B63B4D;
        }

        .arrow{
            position: absolute;
            display: inline-block;
            right: 20px;
            transition: all 0.3s linear;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div id="main">
        <ul>
            <li>
                <div><span class="iconfont icon-time"></span>菜单1<span class="arrow">∨</span></div>
                <ul class="ndMenu">
                    <li>菜单1-1</li>
                    <li>菜单1-2</li>
                </ul>
            </li>
            <li>
                <div><span class="iconfont icon-setting"></span>菜单2<span class="arrow">∨</span></div>
                <ul class="ndMenu">
                    <li>菜单2-1</li>
                    <li>菜单2-2</li>
                </ul>
            </li>
            <li>
                <div><span class="iconfont icon-codepen"></span>菜单3<span class="arrow">∨</span></div>
                <ul class="ndMenu">
                    <li>菜单3-1</li>
                    <li>菜单3-2</li>
                </ul>
            </li>
            <li>
                <div><span class="iconfont icon-feed"></span>菜单4<span class="arrow">∨</span></div>
                <ul class="ndMenu">
                    <li>菜单4-1</li>
                    <li>菜单4-2</li>
                </ul>
            </li>
        </ul>
    </div>

    <script>
        let stMenu = document.querySelectorAll("#main>ul>li>div");

        stMenu.forEach(x=>{
            x.onclick=function(){
                if(hasNextE(this)){
                    // 获取点击的p标签所在的li标签
                    let high = this.parentElement;
                    if(high.style.height == "" || high.style.height == "40px"){
                        // 每次展开2级菜单时,关闭其他菜单
                        closeAllStMenu();
                        
                        // 获取2级菜单个数,计算2级菜单总高度
                        let subHigh = high.lastElementChild.children.length * 40;
                        
                        // 修改1级菜单高度、颜色、箭头旋转角度
                        high.style.height = 40+subHigh+"px";
                        this.style.color = "#B63B4D";
                        this.lastElementChild.style.transform = "rotate(180deg)";
                    }else{
                        high.style.height = "40px";
                        this.style.color = "#000000";
                        this.lastElementChild.style.transform = "rotate(0deg)";
                    }
                }
                    
                
            }
        });

        // 判断当前元素是否有下一个兄弟元素
        function hasNextE(e){
            if(e.nextElementSibling == null){
                return false;
            }
            return true;
        }

        // 关闭所有一级菜单
        function closeAllStMenu(){
            stMenu.forEach(y=>{
                y.parentNode.style.height = "40px";
                y.style.color = "#000000";
                y.lastElementChild.style.transform = "rotate(0deg)";
            });
        }

    </script>
</body>
</html>

练习3:Tab选项卡

  效果图:

  code:

<!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>Tab选项卡效果</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html{
            background-color: #484B5F;
        }
        h1{
            margin: 30px 0 10px;
            text-align: center;
            color: #dadada;
        }
        section{
            padding: 40px 0;
            width: 100%;
            margin: 0 auto;
            background-color: #ffffff;
            overflow: hidden;
        }
        section li{
            color: #8C8C8C;
            height: 30px;
            line-height: 30px;
            display: inline-block;
            background-color: #EDEDED;
            padding: 0 15px;
            border-radius: 12px 12px 0 0;
            cursor: pointer;
            transition: all 0.3s linear;
        }
        #tag{
            height: 50px;
            position: relative;
        }
        #tag>ul{
            position: absolute;
            bottom: 0;
            left: 0;
        }
        #tag,#content{
            width: 900px;
            margin: 0 auto;
        }
        #content{
            box-sizing: border-box;
            border: 1px solid #cacaca;
            padding: 10px 15px;
        }
        #content h3{
            line-height: 36px;
        }
        #content p{
            color: #8C8C8C;
        }

        .textdisplay{
            animation: textdisplay 0.3s linear;
        }

        /* 自定义动画,透明度从0到1 */
        @keyframes textdisplay {
            0%{
                opacity: 0;
            }
            100%{
                opacity: 1;
            }
        }
    </style>

    <script>
        var data = [
            {
                "title":"Content 1",
                "content":"Aliquam eleifend magna mauris, id egestas eros dictum ac. Vivamus ac turpis at nisi mattis aliquam. In hac habitasse platea dictumst."
            },
            {
                "title":"Content 2",
                "content":"Donec congue ligula non risus dictum, eget vehicula diam mattis. Pellentesque at ante ipsum. Suspendisse rutrum elementum dolor, non congue risus sagittis id."
            },
            {
                "title":"Content 3",
                "content":"Vivamus sem odio, mattis vel dui aliquet, iaculis lacinia nibh. Vestibulum tincidunt, lacus vel semper pretium, nulla sapien blandit massa, et tempor turpis urna eu mi."
            },
            {
                "title":"Content 4",
                "content":"Donec congue ligula non risus dictum, eget vehicula diam mattis. Pellentesque at ante ipsum. Suspendisse rutrum elementum dolor, non congue risus sagittis id."
            },
            {
                "title":"Content 5",
                "content":"Donec congue ligula non risus dictum, eget vehicula diam mattis. Pellentesque at ante ipsum. Suspendisse rutrum elementum dolor, non congue risus sagittis id."
            }
        ];
    </script>
</head>
<body>
    <h1>Tab选项卡</h1>
    <section>
        <div id="tag">
            <ul>
                <li>Long Tab Name</li>
                <li>Another Tab</li>
                <li>Tab3</li>
                <li>Tab4</li>
                <li>Tab5</li>
            </ul>
        </div>
        <div id="content">
            <h3>标题</h3>
            <p>内容</p>
        </div>
    </section>
    <script>
        let tags = document.querySelectorAll("#tag li");
        let titles = document.querySelector("#content>h3");
        let cont = document.querySelector("#content>p");

        // 加载时默认选中第一个tag
        changeStyle(tags[0], 0);

        tags.forEach((x,index)=>{
            x.onclick = function(){
                tagOrign();
                changeStyle(x, index);
            }
        });

        // 初始化选项样式
        function tagOrign(){
            tags.forEach(x=>{
                x.style.borderTop = "none";
                x.style.color = "#8C8C8C";
                x.style.backgroundColor = "#EDEDED";
            });
        }

        // 更改标签样式和文本内容
        function changeStyle(x, index){
            titles.innerText = data[index].title;
            cont.innerText = data[index].content;
            
            // 给文本添加含有动画的类
            titles.classList.add("textdisplay");
            cont.classList.add("textdisplay");
            
            // 通过增加上边框实现类似高度增加效果
            x.style.borderTop = "5px solid #68AD8F";
            x.style.color = "#ffffff";
            x.style.backgroundColor = "#68AD8F";

            // 定时删除动画类,以便每次点击都能有动画?
            setTimeout(() => {
                titles.classList.remove("textdisplay");
                cont.classList.remove("textdisplay");
            }, 300);
        }

    </script>
</body>
</html>

练习4:背景图切换

  效果图:

 

  code:

<!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>
    <link rel="stylesheet" href="../练习2/iconfont.css">
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        header{
            background-color: #DD5F03;
            width: 100%;
            height: 300px;
        }
        #banner{
            width: 812px;
            height: 100%;
            margin: 0 auto;
            box-sizing: border-box;
            /* border: 1px dashed pink; */
            position: relative;
            background-repeat: no-repeat;
            background-size:contain;
            transition: all 0.3s linear;
        }
        .btnSmt{
            position: absolute;
            right: 0;
            bottom: 100px;
            width: 120px;
            height: 30px;
            border-radius: 15px;
            background-color: #ffffff;
            color: #DD5F03;
            border: 2px solid #cacaca;
        }
        #nav{
            position: absolute;
            left: 0;
            bottom: 0;
            display: flex;
            width: 100%;
            height: 70px;
            justify-content: space-evenly;
            align-items: flex-end;
        }
        #nav>li{
            width: 160px;
            height: 60px;
            background-color: #ffffff50;
            list-style: none;
            cursor: pointer;
            text-align: center;
            line-height: 60px;
            transition: all 0.3s linear;
        }
    </style>
</head>
<body>
    <header>
        <div id="banner">

            <button class="btnSmt">投放申请</button>

            <ul id="nav">
                <li><span class="iconfont icon-time"></span>大数据分析</li>
                <li><span class="iconfont icon-time"></span>媒体资源</li>
                <li><span class="iconfont icon-time"></span>精准营销</li>
                <li><span class="iconfont icon-time"></span>跨屏</li>
                <li><span class="iconfont icon-time"></span>监测与优化</li>
            </ul>
        </div>
    </header>
    <script>
        let banner = document.querySelector("#banner");
        let li = document.querySelectorAll("#nav>li");
        // 定义图片共同部分路径
        let imgUrl = "./images/index_slide_con0";

        // 加载时默认选中样式
        // banner.style.backgroundImage = `url('${imgUrl}${1}.png')`;
        // li[0].style.height = "70px";

        li.forEach((e,index)=>{
            e.onclick=function(){
                highOrign();
                // 更改背景图片路径                  拼接为完整的图片路径
                banner.style.backgroundImage = `url('${imgUrl}${index+1}.png')`;
                e.style.height = "70px";
                e.style.lineHeight = "70px";

                // 也可通过换类的方式改变样式
            }
        });
        // 模拟用户点击,加载时选中第一个li
        li[0].click();

        function highOrign(){
            li.forEach((e,index)=>{
                e.style.height = "60px";
                e.style.lineHeight = "60px";

                // 也可通过换类的方式改变样式
            });
        }
    </script>
</body>
</html>

新手上路,如有错误,望大佬们指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值