当前位置:网站首页>页面返回顶部和固定导航栏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>边栏推荐
猜你喜欢

为什么 RTP 的视频的采样率是 90kHz ?

一分钟之内搭建自己的直播服务器?

【交换机端口安全技术 】

Object.defineProperty方法(详解)

VsCode更新后,怎么使用使用快捷键同时生成多个元素

网络运维系列:Ubnt ER-X初始化和开启硬件NAT

MATLAB文件操作
![[Fault Diagnosis] Weak Fault Diagnosis of Fan Bearing Based on PSO_VMD_MCKD Method](/img/43/719caffc79950edd18719ad0ba3aff.jpg)
[Fault Diagnosis] Weak Fault Diagnosis of Fan Bearing Based on PSO_VMD_MCKD Method
CUDA programming based on Visual Studio 2015 (1): basic configuration

idea使用jdbc对数据库进行增删改查,以及使用懒汉方式实现单例模式
随机推荐
LAMP 环境搭建 yum源安装方式 (Apache 2.4.6 +mysql 8.0.28+php 8.1.3)
[Time series model] AR model (principle analysis + MATLAB code)
VLAN原理
【故障诊断】基于PSO_VMD_MCKD方法的风机轴承微弱故障诊断
静态方法的缺失
WebRTC 中有关 Media Stream & Track & Channel 之间的关系
test3
小知识系列:Fork之后如何与原仓库分支同步
static关键字的三种重要作用详解
初识art-template模板引擎
golang中使用泛型
2021 annual summary - complete a year of harvest
Based on the SVM regression forecast 】 【 LibSVM realize the prediction of a characteristic data
【频域分析】频谱泄露、频率分辨率、栅栏效应
SQL在MySQL中是如何执行的
MATLAB file operations
DOM —— 事件类型
小知识点系列-基于H2数据库单元测试
tab 替换空格
webrtc 有关 SDP 部分的解析流程分析