作业检查

来源:2-8 项目作业

以你为瘾丶

2021-09-13 10:56:27

相关代码:

<!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>
* {
margin: 0;
padding: 0;
}

.center-wrap {
width: 1152px;
margin: 0 auto;
}

.header-top {
height: 40px;
overflow: hidden;
}

.header-top .logo {
float: left;
}

.header-top .header-nav {
float: left;
line-height: 40px;
position: absolute;
right: 307px;
}

.header-top .header-nav ul {
list-style: none;
position: relative;
}

.header-top .header-nav ul li {
float: left;
margin-right: 45px;
cursor: pointer;
}

.header-top .header-nav ul li:hover {
color: #f01400;
}

.header-top .header-nav ul li:last-child {
margin-right: 0;
}

.header-top .header-nav .line {
width: 32px;
height: 3px;
background-color: #f01400;
position: absolute;
left: 0px;
bottom: 0;
transition: left .5s ease 0s;
}

.header-top .btn {
float: right;
line-height: 40px;
background-color: black;
width: 80px;
text-align: center;
}

.header-top .btn a {
text-decoration: none;
color: white;
display: block;
width: 80px;
height: 40px;
}
</style>

</head>

<body>
<header class="header-top">
<div class="center-wrap">
<div class="logo">
<h1>慕课手机</h1>
</div>
<div class="header-nav">
<ul id="ul">
<li data-n="0">首页</li>
<li data-n="1">外观</li>
<li data-n="2">配置</li>
<li data-n="3">型号</li>
<li data-n="4">说明</li>
</ul>
<div class="line" id="line"></div>
</div>
<div class="btn">
<a href="">立即购买</a>
</div>
</div>
</header>

<script>
var oUl = document.getElementById("ul")
var oLine = document.getElementById("line")
var list=document.getElementsByTagName("li")
oUl.onmouseover = function (e) {
if (e.target.tagName.toLowerCase() == "li") {
n = e.target.getAttribute("data-n")
oLine.style.left=n*77+"px"
}
}
</script>
</body>

</html>


写回答

1回答

好帮手慕慕子

2021-09-13

同学你好,下划线效果实现是对的,但是下划线所在位置的文字没有显示为红色,建议:可以添加一个active类,设置字体为红色,默认给第一项添加这个类名,如下:

https://img.mukewang.com/climg/613ec02109ee23c610821074.jpg

调整js代码,如下:

https://img.mukewang.com/climg/613ec090091cad1c10920532.jpg

祝学习愉快~


0

前端工程师

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

20327 学习 · 17877 问题

查看课程