老师请检查一下作业

来源:2-8 项目作业

终有一死

2022-08-22 16:48:40

问题描述:a标签不是在li标签内吗,为什么触碰a标签不能移动而要触碰周围才能移动呢?

相关代码:

<!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;
        }
        .center-wrap {
            width: 1100px;
            margin: 0 auto;
        }
        nav {
            width: 100%;
            height: 60px;
        }
        nav .center-wrap .logo {
            float: left;
            width: 100px;
            height: 60px;
        }
        nav .center-wrap .logo p {
            font-size: 20px;
            line-height: 60px;
        }
        nav .center-wrap .component-nav {
            display: inline-block;
            position: relative;
            width: 280px;
            height: 60px;
            margin-left: 600px;
        }
        nav .center-wrap .component-nav ul li {
            float: left;
            list-style: none;
            line-height: 60px;
            margin-right: 30px;
            cursor: pointer;
        }
        nav .center-wrap .component-nav ul li:last-child {
            margin-right: 0px;
        }
        nav .center-wrap .component-nav ul li a {
            text-decoration: none;
            font-size: 14px;
            color: #000;
        }
        nav .center-wrap .component-nav ul li a:hover {
            color: #f01400;
        }
        nav .center-wrap .button-buy {
            position: relative;
            width: 100px;
            height: 40px;
            float: right;
            line-height: 40px;
        }
        nav .center-wrap .button-buy a{
            position: absolute;
            width: 100px;
            height: 40px;
            display: block;
            color: white;
            background-color: #000;
            text-decoration: none;
            text-align: center;
            line-height: 40px;
            font-size: 14px;
            top: 50%;
            margin-top: -10px;
        }
        nav .center-wrap .component-nav .underline {
            position: absolute;
            width: 28px;
            height: 3px;
            background-color: #f01400;
            bottom: 8px;
            left: 0px;
        }
       
    </style>
</head>
<body>
    <nav>
        <div class="center-wrap">
            <div class="logo">
                <p>慕课手机</p>
            </div>
            <div class="component-nav">
                <ul id="component-nav">
                    <li data-n="0"><a href="">首页</a></li>
                    <li data-n="1"><a href="">外观</a></li>
                    <li data-n="2"><a href="">配置</a></li>
                    <li data-n="3"><a href="">型号</a></li>
                    <li data-n="4"><a href="">说明</a></li>
                </ul>
                <div class="underline" id="underline"></div>
            </div>
            <div class="button-buy" id="button-buy">
                <a href="">立即购买</a>
            </div>
        </div>
    </nav>

    <script>
        var componentNav = document.getElementById('component-nav');
        var underline = document.getElementById('underline');

        componentNav.onmouseover = function (e) {
            if (e.target.tagName.toLowerCase() == 'li') {
                underline.style.transition = 'left .5s ease 0s';
                underline.style.left = Number(e.target.getAttribute('data-n')) * 58 + 'px';
            }
        }
       
    </script>
</body>
</html>


写回答

1回答

好帮手慕慕子

2022-08-22

同学你好,虽然a标签在li标签内,但是e.target获取的鼠标移入的当前元素,所以当鼠标移入a时,此时的e.target获取的是a元素,所以只有鼠标移入li时,才会有效果。

建议修改:调整为a标签添加data-n属性

https://img.mukewang.com/climg/6303451609dc3c8308260522.jpg

然后调整a为块元素填充满li

https://img.mukewang.com/climg/6303452c0905cd6e08780230.jpg

https://img.mukewang.com/climg/630345a8096e5c7e13340274.jpg

另外,效果视频中默认第一项文字为红色,可以添加特殊的类名设置。示例:

https://img.mukewang.com/climg/630345df09267db709980460.jpg

https://img.mukewang.com/climg/630345e9091eb77207800102.jpg

获取所有的a元素

https://img.mukewang.com/climg/6303466a0933497610540106.jpg

鼠标移入时,先遍历所有的a元素,设置类名为空,然后只给鼠标移入的当前a标签添加active类,示例:

https://img.mukewang.com/climg/6303469c09b7fa5615780456.jpg

祝学习愉快~

0

前端工程师

前端入门如同写字,如果你不知道从哪开始,那就选择前端(含Vue3.x,React17,TS)

20327 学习 · 17877 问题

查看课程