当前位置:网站首页>在线直播源码,JS动态效果之,侧边栏滚动固定效果
在线直播源码,JS动态效果之,侧边栏滚动固定效果
2022-06-28 03:44:00 【云豹网络科技】
在线直播源码,JS动态效果之,侧边栏滚动固定效果
结构代码:
<div class="slider-bar">
<span class="goBack">返回顶部</span>
</div>
<div class="header w">头部区域</div>
<div class="banner w">banner区域</div>
<div class="main w">主体部分</div>
样式代码:
.slider-bar {
position: absolute;
left: 50%;
top: 300px;
margin-left: 600px;
width: 45px;
height: 130px;
background-color: pink;
}
.w {
width: 1200px;
margin: 10px auto;
}
.header {
height: 150px;
background-color: purple;
}
.banner {
height: 250px;
background-color: skyblue;
}
.main {
height: 1000px;
background-color: yellowgreen;
}
span {
display: none;
position: absolute;
bottom: 0;
}
JS代码:
// 获取元素
var sliderbar = document.querySelector('.slider-bar');
var banner = document.querySelector('.banner');
var bannerTop = banner.offsetTop; // 获取主体部分距离顶部的距离
var sliderbarTop = sliderbar.offsetTop - bannerTop; // 看上图我们发现侧边栏是固定banner部分右边的,因此我们要用两个值相减获取banner顶着浏览器页面最上方的时候,侧边栏距离浏览器最上方的值。
document.addEventListener('scroll', function() {
// 给【页面】添加一个滚动事件,我们事件的对象时页面的滚动。
if (window.pageYOffset >= bannerTop) {
// 浏览器滚动上方被遮盖住的部分大于bannerTop,也就是说banenr上方以及没有东西了,再或者说header部分完全被滚动走了。
sliderbar.style.position = 'fixed';
sliderbar.style.top = sliderbarTop + 'px';
} else {
sliderbar.style.position = 'absolute'; // 如果没有滚动走,那就让侧边栏还随着页面的滚动而滚动
sliderbar.style.top = 300 + 'px'; // 元素起初的位置
}
if (window.pageYOffset >= mainTop) {
goBack.style.display = 'block'; // 如果banner部分被滚动走了,那就让侧边栏内容给显示出来
} else {
goBack.style.display = 'none'; // 否则不显示
}
// 3. 当我们点击了返回顶部模块,就让窗口滚动的页面的最上方
goBack.addEventListener('click', function() {
// 里面的x和y 不跟单位的 直接写数字即可
// window.scroll(0, 0);
// 因为是窗口滚动 所以对象是window
animate(window, 0);
});
// 动画函数
function animate(obj, target, callback) {
// console.log(callback); callback = function() {} 调用的时候 callback()
// 先清除以前的定时器,只保留当前的一个定时器执行
clearInterval(obj.timer);
obj.timer = setInterval(function() {
// 步长值写到定时器的里面
// 把我们步长值改为整数 不要出现小数的问题
// var step = Math.ceil((target - obj.offsetLeft) / 10);
var step = (target - window.pageYOffset) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (window.pageYOffset == target) {
// 停止动画 本质是停止定时器
clearInterval(obj.timer);
// 回调函数写到定时器结束里面
// if (callback) {
// // 调用函数
// callback();
// }
callback && callback();
}
// 把每次加1 这个步长值改为一个慢慢变小的值 步长公式:(目标值 - 现在的位置) / 10
// obj.style.left = window.pageYOffset + step + 'px';
window.scroll(0, window.pageYOffset + step);
}, 15);
}
})
var main = document.querySelector('.main');
var mainTop = main.offsetTop;
var goBack = document.querySelector('.goBack');
以上就是在线直播源码,JS动态效果之,侧边栏滚动固定效果, 更多内容欢迎关注之后的文章
边栏推荐
- 02 MongoDB数据类型、重要概念以及shell常用指令
- 单一职责原则
- Pinda general permission system (day 5~day 6)
- 歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
- Market competitiveness of robot programming education
- Zipkin service link tracking
- 03 MongoDB文档的各种增加、更新、删除操作总结
- 利用ELK 搭建日志分析系统(一)—— 组件介绍
- Staggered and permutation combination formula
- Using elk to build a log analysis system (I) -- component introduction
猜你喜欢

@Several scenarios of transactional failure

Tiktok actual battle ~ take off the blogger

A summary of my recent situation in June 2022

揭开SSL的神秘面纱,了解如何用SSL保护数据

leetcode:494. All methods of adding and subtracting operators to the array to get the specified value

Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"

Detailed explanation of iptables firewall rules and firewalld firewall rules

阿里P8倾情推荐,Fiddler抓包工具实战篇(一)

Genicam gentl standard ver1.5 (2)

Analysis of future teacher research ability under steam education framework
随机推荐
Pinda general permission system (day 5~day 6)
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
Adder - Notes
PostgreSQL implements batch update, deletion and insertion
《性能之巅第2版》阅读笔记(二)--性能观察工具
[MySQL] multi table connection query
MSc 307 (88) (2010 FTPC code) Part 9 bedding test
From zero to one, I will teach you to build a "search by text and map" search service (I)
Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
Chapter 1 Introduction to bash
等保2.0密码要求是什么?法律依据有哪些?
利用ELK 搭建日志分析系统(二)—— 部署安装
PostgreSQL 实现批量更新、删除、插入
【小程序实战系列】电商平台源码及功能实现
Unity C # e-learning (11) -- custom protocol generation tool
Two methods of shell script parameter passing based on arm5718
GCD maximum common divisor
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
01 overview, application scenarios, Download methods, connection methods and development history of mongodb
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?