css和js实现硬件加速渲染自定义滚动条
2024-10-20 05:50:05
听别人说用CSS的变换来实现渲染有硬件加速的效果,看到很多大网站都开始陆续使用上了,我也来说说怎么做,我这边实现的滚动条有自然滚动效果,看起来比较自然,说的再多不如直接写,让我们开始吧!
我们需要自己定义页面滚动的话,我们肯定不想让浏览器自带的滚动条出现,所以我们要先隐藏页面所有的滚动条,下面的CSS样式是兼容各个浏览器的隐藏滚动条
*::-webkit-scrollbar {
width: 0 !important
}
/* IE 10+ */
* {
-ms-overflow-style: none;
}
/* Firefox */
* {
overflow: -moz-scrollbars-none;
}
滚动条隐藏起来了,我们下一步需要做的就是写页面代码
<div class="scrollBox">
<div class="scrollContent">
<!-- 你的内容 -->
<!-- 内容结束 -->
</div>
</div>
后面继续写对应的css样式
.scrollBox {
position: fixed;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.scrollContent {
transform: translate3d(0, 0px, 0);
transition: all ease-out 0.6s;
}
写完后我们开始写js逻辑
let mousetop = 0;
const my_mousewheel = (e) => {
if ((mousetop + e.deltaY) < -1 || (mousetop + e.deltaY) > (document.querySelector(".scrollBox").scrollHeight - document.querySelector(".scrollBox").offsetHeight + 1)) {
//这里初略判断滚动是否到顶部或者到底部
if ((mousetop + e.deltaY) < -1 && mousetop >= 1) {
//二次判断是否真到顶部
mousetop = 0;
document.querySelector(".scrollContent").style.removeProperty("transform");
return;
}
if ((mousetop + e.deltaY) > (document.querySelector(".scrollBox").scrollHeight - document.querySelector(".scrollBox").offsetHeight + 1) && mousetop <= (document.querySelector(".scrollBox").scrollHeight- document.querySelector(".scrollBox").offsetHeight) - 1) {
//二次判断是否真到底部
mousetop = document.querySelector(".scrollBox").scrollHeight - document.querySelector(".scrollBox").offsetHeight;
document.querySelector(".scrollContent").style.cssText = `transform: translate3d(0,-${mousetop}px,0);`;
return;
}
return;
}
mousetop += e.deltaY;
document.querySelector(".scrollContent").style.cssText = `transform: translate3d(0,-${mousetop}px,0);`;
}
document.querySelector(".scrollBox").onmousewheel = my_mousewheel;
最后到了附上源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css和js实现硬件加速渲染自定义滚动条</title>
<style>
*::-webkit-scrollbar {
width: 0 !important
}
/* IE 10+ */
* {
-ms-overflow-style: none;
}
/* Firefox */
* {
overflow: -moz-scrollbars-none;
}
html,
body {
margin: 0;
padding: 0;
font-size: 100px;
background: #fff;
}
a {
text-decoration: none;
text-decoration-color: #000;
color: #000;
}
li {
list-style: none;
}
.scrollBox {
position: fixed;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.scrollContent {
transform: translate3d(0, 0px, 0);
transition: all ease-out 0.6s;
}
</style>
</head>
<body>
<div class="scrollBox">
<div class="scrollContent">
<!-- 你的内容 -->
<!-- 内容结束 -->
</div>
</div>
<script>
let mousetop = 0;
const my_mousewheel = (e) => {
if ((mousetop + e.deltaY) < -1 || (mousetop + e.deltaY) > (document.querySelector(".scrollBox").scrollHeight - document.querySelector(".scrollBox").offsetHeight + 1)) {
//这里初略判断滚动是否到顶部或者到底部
if ((mousetop + e.deltaY) < -1 && mousetop >= 1) {
//二次判断是否真到顶部
mousetop = 0;
document.querySelector(".scrollContent").style.removeProperty("transform");
return;
}
if ((mousetop + e.deltaY) > (document.querySelector(".scrollBox").scrollHeight - document.querySelector(".scrollBox").offsetHeight + 1) && mousetop <= (document.querySelector(".scrollBox").scrollHeight- document.querySelector(".scrollBox").offsetHeight) - 1) {
//二次判断是否真到底部
mousetop = document.querySelector(".scrollBox").scrollHeight - document.querySelector(".scrollBox").offsetHeight;
document.querySelector(".scrollContent").style.cssText = `transform: translate3d(0,-${mousetop}px,0);`;
return;
}
return;
}
mousetop += e.deltaY;
document.querySelector(".scrollContent").style.cssText = `transform: translate3d(0,-${mousetop}px,0);`;
}
document.querySelector(".scrollBox").onmousewheel = my_mousewheel;
</script>
</body>
</html>
教程到此结束,希望可以帮到你们
最新文章
- 常用的js正则表达式
- a版本冲刺第四天
- 微软四十周年 Microsoft’s 40th anniversary
- 京东分布式MySQL集群方案介绍
- 记一个dynamic的坑
- Object C学习笔记14-分类(category)
- SQL里IN的用法以及优化
- sql模糊查询
- php 随机显示图片的函数(实例)
- 获取上海地区AQI质量数据Python脚本
- 如何解决卸载McAfee时出现“处于托管模式时无法删除”问题(转)
- hdu_4787_GRE Words Revenge(在线AC自动机)
- JQuery学习笔记——基础选择器
- CentOS7.3利用kubeadm安装kubernetes1.7.3完整版(官方文档填坑篇)
- Python-psutil模块
- 面向对象写的简单的colors rain
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
- DUILIB UI创建过程
- BZOJ2333 [SCOI2011]棘手的操作 堆 左偏树 可并堆
- WEKA结果解读
热门文章
- ribbon源码(4) 载均衡算法
- 2019.7.12 sdfzoier做题统计
- Apollo系列(一):分布式配置中心Apollo安装(Linux、Docker)
- UnityShader学习笔记- Stencil Buffer
- Spring学习(九)--Spring的AOP
- Leetcode PHP题解--D125 107. Binary Tree Level Order Traversal II
- Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级
- .net Winform 揭开语音识别的神秘面纱
- jvm优化案例
- ATMEGA的SPI总线 - 第2部分