当前位置:网站首页>JS Bom location 楼层导航效果 offsetTop data-n 方括号选择器
JS Bom location 楼层导航效果 offsetTop data-n 方括号选择器
2022-07-30 02:20:00 【HandsomeDanielWu】


如果祖先元素有定位,效果会像子绝父相对
data-n,自定义属性
方括号选择器
<style>
* {
margin: 0;
padding: 0;
}
.content-part {
width: 1000px;
margin: 0px auto;
margin-bottom: 30px;
background-color: #ccc;
font-size: 50px;
}
.floornav {
position: fixed;
right: 40px;
top: 50%;
margin-top: -100px;
width: 120px;
height: 200px;
background-color: orange;
}
.floornav ul {
list-style: none;
}
.floornav ul li {
width: 120px;
height: 40px;
line-height: 40px;
text-align: center;
font-size: 26px;
/* 小手指针 */
cursor: pointer;
}
.floornav ul li.current {
background: purple;
color: white;
}
</style>
</head>
<body>
<nav class="floornav">
<ul id="list">
<li data-n="科技" class="current">科技</li>
<li data-n="体育">体育</li>
<li data-n="新闻">新闻</li>
<li data-n="娱乐">娱乐</li>
<li data-n="视频">视频</li>
</ul>
</nav>
<section class="content-part" style="height:674px;" data-n="科技">
科技栏目
</section>
<section class="content-part" style="height:567px;" data-n="体育">
体育栏目
</section>
<section class="content-part" style="height:739px;" data-n="新闻">
新闻栏目
</section>
<section class="content-part" style="height:574px;" data-n="娱乐">
娱乐栏目
</section>
<section class="content-part" style="height:1294px;" data-n="视频">
视频栏目
</section>
<script>
// 使用事件委托给li添加监听
var list = document.getElementById('list');
var contentParts = document.querySelectorAll('.content-part');
var lis = document.querySelectorAll('#list li');
list.onclick = function (e) {
if (e.target.tagName.toLowerCase() == 'li') {
// getAttribute表示得到标签身上的某个属性值
var n = e.target.getAttribute('data-n');
// 可以用属性选择器(就是方括号选择器)来寻找带有相同data-n的content-part
var contentPart = document.querySelector('.content-part[data-n=' + n + ']');
// 让页面的卷动自动成为这个盒子的offsetTop值
document.documentElement.scrollTop = contentPart.offsetTop;
}
}
// 在页面加载好之后,将所有的content-part盒子的offsetTop值推入数组
var offsetTopArr = [];
// 遍历所有的contentPart,将它们的净位置推入数组
for (var i = 0; i < contentParts.length; i++) {
offsetTopArr.push(contentParts[i].offsetTop);
}
// 为了最后一项可以方便比较,我们可以推入一个无穷大
offsetTopArr.push(Infinity);
console.log(offsetTopArr);
// 当前所在楼层
var nowfloor = -1;
// 窗口的卷动
window.onscroll = function () {
// 得到当前的窗口卷动值
var scrollTop = document.documentElement.scrollTop;
// 遍历offsetTopArr数组,看看当前的scrollTop值在哪两个楼层之间
for (var i = 0; i < offsetTopArr.length; i++) {
if (scrollTop >= offsetTopArr[i] && scrollTop < offsetTopArr[i + 1]) {
break;
}
}
// 退出循环的时候,i是几,就表示当前楼层是几
// 如果当前所在楼层,不是i,表示环楼了
if (nowfloor != i) {
console.log(i);
// 让全局变量改变为这个楼层号
nowfloor = i;
// 设置下标为i的项有cur
for (var j = 0; j < lis.length; j++) {
if (j == i) {
lis[j].className = 'current';
} else {
lis[j].className = '';
}
}
}
};
</script>
</body>
边栏推荐
- Performance Testing Theory 1 | Sorting out difficult problems in performance testing
- flutter学习之widget的显示和隐藏
- LeetCode Question of the Day (874. Walking Robot Simulation)
- Unity便携式 VR 的实现
- diff和key的作用
- LeetCode每日一题(874. Walking Robot Simulation)
- Oracle数据库表空间整理回收与释放操作
- 成功解决AttributeError: ‘PngImageFile‘ object has no attribute ‘imshow‘
- 【C语言刷LeetCode】592. 分数加减运算(M)
- MPLS VPN跨域-optionC2
猜你喜欢
随机推荐
binary search tree
关于 SAP Fiori 应用的离线使用
It is really strong to apply the @Transactional transaction annotation to such perfection!
matlab用dde23求解带有固定时滞的时滞微分方程
手把手教你实现一个流动的渐变色边框
Not enough information to list load addresses in the image map.(STM32编译报错)
HCIP 第十四天
多线程---初阶
Embedded SIG | 分布式软总线
error: 'filesystem' is not a member of 'std'
The Rising Star of Interface Test Automation-YApi Interface Management Platform
固体火箭发动机三维装药逆向内弹道计算
Tibetan Mapping
API interface batch test
MPLS VPN跨域-optionC2
Understanding the prototype chain in js, what problem does the prototype chain solve?
Type-C charging and OTG chip - LDR6028A
JS开发3D建模软件
基于燃压缩空气储能系统的零碳微能源互联网优化调度(Matlab代码实现)
Object.freeze()学习








