当前位置:网站首页>页面返回顶部和固定导航栏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>边栏推荐
- 解决启动filebeat时遇到Exiting: error unpacking config data: more than one namespace configured accessing错误
- webrtc 中怎么根据 SDP 创建或关联底层的 socket 对象?
- The difference and connection between dist, pdist and pdist2 in MATLAB
- 网络运维系列:端口占用、端口开启检测
- Linux下mysql的彻底卸载
- Mysql理解MVCC与BufferPool缓存机制
- makefile——杂项
- 常见(MySQL)面试题(含答案)
- 【数据读写】csv文件与xls/xlsx文件
- 【QMT】给QMT量化交易软件安装和调用第三方库(举例通达信pytdx,MyTT,含代码)
猜你喜欢
随机推荐
The difference and connection between dist, pdist and pdist2 in MATLAB
网络请求——跨域 的概念
SQL在MySQL中是如何执行的
Mediasoup 杂谈(待完善)
Mysql理解MVCC与BufferPool缓存机制
ks.cfg 怎么读取光盘 (cdrom) 上的文件并执行对应的脚本
JVM常量池详解
【数据读写】csv文件与xls/xlsx文件
filebeat的配置
Scala的安装和IDEA的使用(初入茅庐)
CDN的加速原理是什么?
只出现一次的数字||| —— 哈希映射、异或位运算+分治思想
异常抛出错误
nvm详细安装步骤以及使用(window10系统)
解决跨域的方法 --- Proxy
小知识系列:Fork之后如何与原仓库分支同步
tab 替换空格
为什么 RTP 的视频的采样率是 90kHz ?
Filter 过滤器
IIR滤波器设计之冲激响应不变法与双线性变换法









