当前位置:网站首页>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>
边栏推荐
- 基于燃压缩空气储能系统的零碳微能源互联网优化调度(Matlab代码实现)
- VLAN 实验
- OSPF shamlink 解决后门链路问题
- JNPF3.4.2 system upgrade announcement
- 05. Private properties in script_setup
- Is it difficult for AI to land?Cloud native helps enterprises quickly apply machine learning MLOps
- Push the image to the Alibaba Cloud private warehouse
- 固体火箭发动机三维装药逆向内弹道计算
- The display and hiding of widgets for flutter learning
- Object.freeze() learning
猜你喜欢

postgresql日常运维技能,适合初学者

解决:npm ERR code ELIFECYCLE npm ERR errno 1(安装脚手架过程中,在npm run dev 时发生错误)

DAP数据加工流程梳理

【2023海康威视提前批笔试题】~ 题目及参考答案

fluttter学习之ButtonStyle 、MaterialStateProperty

STM32L4R9ZIY6PTR STM32L4高性能嵌入式—MCU

English grammar_indefinite pronouns -some & any

The role of interface testing

AI落地难?云原生助力企业快速应用机器学习 MLOps

五种绑定彻底弄懂this,默认绑定、隐式绑定、显式绑定、new绑定、箭头函数绑定详解
随机推荐
HCIP 第十五天
error: 'filesystem' is not a member of 'std'
Linux Jenkins查找缓存文件及删除 (2022-07测试可用)
LeetCode 2342. 数位和相等数对的最大和
MPLS VPN跨域-optionC2
Object.freeze() learning
EL 表达式
How Junior Testers Grow Fast
binary search tree
博客搭建十:hugo博客添加友链
接口测试的作用
记一次搭建conda虚拟环境
Understanding the prototype chain in js, what problem does the prototype chain solve?
[Notes] Stuttering word segmentation to draw word cloud map
nrm ls 为什么前面不带 *了
App测试需要测什么
[Microservice~Nacos] Configuration Center of Nacos
matlab洗碗机节水模型的优化设计-这是个课题名称,不是买洗碗机,审核的人仔细看下,谢谢
Teach you how to achieve a flowing gradient border
mysql 报错 is too long for user name (should be no longer than 16)