请老师检查
来源:2-8 项目作业
小英雄哪托
2022-05-17 17:44:53
<!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>
header {
background-color: #ccc;
overflow: hidden;
padding-top: 12px;
padding-right: 12px;
box-sizing: border-box;
}
header button {
color: #fff;
background-color: #000;
width: 80px;
height: 40px;
float: right;
margin-left: 20px;
}
header ul {
float: right;
list-style: none;
box-sizing: border-box;
position: relative;
}
header ul li {
float: left;
margin-right: 30px;
cursor: pointer;
}
header ul li.current {
color: red;
}
header ul .box {
width: 34px;
height: 2px;
background-color: red;
position: absolute;
left: 39px;
bottom: -7px;
}
</style>
</head>
<body>
<header>
<button>立即购买</button>
<ul id="nav">
<li class="current">首页</li>
<li>外观</li>
<li>配置</li>
<li>说明</li>
<li>型号</li>
<div class="box" id="box"></div>
</ul>
</header>
<script>
var nav = document.getElementById('nav');
var box = document.getElementById('box');
var lis = document.querySelectorAll('li');
var idx = 1;
nav.onmouseover = function (e) {
if (e.target.tagName.toLowerCase() == 'li') {
for (var i = 0; i < lis.length; i++) {
if (lis[i] === e.target) {
lis[i].className = 'current';
box.style.left = 39 + i * 62 + 'px';
} else {
lis[i].className = 'none';
}
}
}
}
</script>
</body>
</html>1回答
同学你好,视频效果图中下划线是滑动效果。上传代码中是下划线是直接移动,建议给.box添加transition过渡,参考:

祝学习愉快!