听别人说用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>

教程到此结束,希望可以帮到你们

最新文章

  1. 常用的js正则表达式
  2. a版本冲刺第四天
  3. 微软四十周年 Microsoft’s 40th anniversary
  4. 京东分布式MySQL集群方案介绍
  5. 记一个dynamic的坑
  6. Object C学习笔记14-分类(category)
  7. SQL里IN的用法以及优化
  8. sql模糊查询
  9. php 随机显示图片的函数(实例)
  10. 获取上海地区AQI质量数据Python脚本
  11. 如何解决卸载McAfee时出现“处于托管模式时无法删除”问题(转)
  12. hdu_4787_GRE Words Revenge(在线AC自动机)
  13. JQuery学习笔记——基础选择器
  14. CentOS7.3利用kubeadm安装kubernetes1.7.3完整版(官方文档填坑篇)
  15. Python-psutil模块
  16. 面向对象写的简单的colors rain
  17. Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
  18. DUILIB UI创建过程
  19. BZOJ2333 [SCOI2011]棘手的操作 堆 左偏树 可并堆
  20. WEKA结果解读

热门文章

  1. ribbon源码(4) 载均衡算法
  2. 2019.7.12 sdfzoier做题统计
  3. Apollo系列(一):分布式配置中心Apollo安装(Linux、Docker)
  4. UnityShader学习笔记- Stencil Buffer
  5. Spring学习(九)--Spring的AOP
  6. Leetcode PHP题解--D125 107. Binary Tree Level Order Traversal II
  7. Spring系列之事务的控制 注解实现+xml实现+事务的隔离等级
  8. .net Winform 揭开语音识别的神秘面纱
  9. jvm优化案例
  10. ATMEGA的SPI总线 - 第2部分