当前位置:网站首页>页面返回顶部和固定导航栏js基础案例
页面返回顶部和固定导航栏js基础案例
2022-08-02 14:20:00 【铃儿响叮当不响】
案例代码
<!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>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.content {
margin: 0 0 0 200px;
overflow: hidden;
width: 800px;
height: 3000px;
background-color: skyblue;
float: left;
}
.backtop {
margin-left: 1000px;
margin-top: 550px;
width: 30px;
position: fixed;
float: left;
}
.backtop .icon {
height:50px;
width: 50px;
display: none;
opacity: 0.3;
}
.header {
position: fixed;
top: -80px;
left: 0;
width: 100%;
height: 80px;
background-color: pink;
text-align: center;
color: white;
line-height: 80px;
font-size: 20px;
transition: all .3s;
}
.miaosha {
width: 300px;
height: 300px;
background-color: sandybrown;
margin-top: 500px;
color: brown;
}
</style>
</head>
<body>
<div class="header">我是顶部导航栏</div>
<div class="content">
<div class="miaosha">秒杀模块</div>
</div>
<div class="backtop">
<img class="icon" src="./image/箭头.jpg">
</div>
<script>
// 1、页面滚动事件
let miaosha = document.querySelector('.miaosha')
let header = document.querySelector('.header')
let backtop = document.querySelector('.backtop')
let icon = document.querySelector('.icon')
window.addEventListener('scroll',function(){
if(document.documentElement.scrollTop >= backtop.offsetTop){
icon.style.display = 'block'
} else{
icon.style.display = 'none'
}
// console.log(document.documentElement.scrollTop)
// 2、要检测滚动的距离 >= 秒杀模块的offsetTop值 则滑入
// console.log(miaosha.offsetTop)
if( document.documentElement.scrollTop >= miaosha.offsetTop){
header.style.top = '0'
}else{
header.style.top = '-80px'
}
})
// 回到顶部
backtop.addEventListener('click',function(){
document.documentElement.scrollTop = 0
})
</script>
</body>
</html>
边栏推荐
猜你喜欢
Mysql删库恢复数据
移除元素 - 双指针
[Fault Diagnosis] Weak Fault Diagnosis of Fan Bearing Based on PSO_VMD_MCKD Method
static关键字的三种重要作用详解
VsCode更新后,怎么使用使用快捷键同时生成多个元素
抽象队列同步器AQS应用Lock详解
lammps学习(二)联合原子模型聚乙烯拉伸
Based on the SVM regression forecast 】 【 LibSVM realize the prediction of a characteristic data
【SVM回归预测】基于LibSVM实现多特征数据的预测
C语言的基本程序结构详细讲解
随机推荐
【网络参考模型】
lammps学习(一)单晶硅纳米磨削
Principles of permutation entropy, fuzzy entropy, approximate entropy, sample entropy and approximate entropy implemented by MATLAB
小知识系列:Fork之后如何与原仓库分支同步
JSP技术
webrtc 数据接收流程图解
MATLAB file operations
flask获取post请求参数
小知识点系列:StringUtil.isEmpty()与StringUtil.isBlank()的区别
【无标题】
抽象队列同步器AQS应用Lock详解
nodejs 的下载安装与环境配置
在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
2021年度总结——收获圆满的一年
Mysql索引优化二
APP版本更新通知流程测试要点
【IP基本原理-ARP原理】
XML技术
RTMP, RTSP, SRT 推流和拉流那些事
test2