用html+js+css3实现整屏滚动fullpage功能

2023-11-27 10:50

本文主要是介绍用html+js+css3实现整屏滚动fullpage功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在jquery库中看到有人封装了整屏滚动(fullpage)的插件,感觉还挺有意思,然后想了一下怎么用原生js 实现。

本文的实例讲述了原生javascript实现的全屏滚动功能。分享供大家参考,具体如下:

1. 计算当前浏览器屏幕高度,每次翻页显示的内容高度即为屏幕高度

2. 对鼠标滚轮事件进行监听,注意滚轮事件的浏览器兼容问题。

效果图如下:

目录

 一、先看看页面布局

html部分

css部分

js部分

二、代码总结


 一、先看看页面布局

html部分

<div class="container"><div class="section section1"><h1>第1屏</h1></div><div class="section section2"><h1>第2屏</h1></div><div class="section section3"><h1>第3屏</h1></div><div class="section section4"><h1>第4屏</h1></div><div class="section section5"><h1>第5屏</h1></div></div><ul class="controls"><li class="active">1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

css部分

 <style>* {padding: 0;margin: 0;}html,body {width: 100%;height: 100%;overflow: hidden;}.container {width: 100%;height: 500%;position: absolute;top: 0;transition: all 0.3s ease;}.section1 {background-color: rebeccapurple;background: url(./轮播图/img/01.jpg);}.section2 {background-color: skyblue;background: url(./轮播图/img/02.jpg);}.section3 {background-color: red;background: url(./轮播图/img/03.jpg);}.section4 {background-color: orange;background: url(./轮播图/img/04.jpg);}.section5 {background-color: lightgreen;background: url(./轮播图/img/05.jpg);}.section {width: 100%;height: 20%;display: flex;color: white;justify-content: center;align-items: center;background-size: cover;}.controls {position: absolute;top: 50%;right: 20px;transform: translateY(-50%);list-style: none;}.controls li {width: 50px;height: 50px;font: bold 22px/50px '宋体';text-align: center;background-color: #000;color: white;cursor: pointer;border-radius: 50%;}.controls li+li {margin-top: 5px;}.controls li.active {background-color: #fff;color: red;}</style>

图片可以换成自己喜欢的图片。

js部分

主要核心部分

 //实现滚动效果const container = document.querySelector('.container')const lis = document.querySelectorAll('.controls li') var viewHeight = null //声明页面高度var index = 0; //当前索引var flag = true; //节流开关document.addEventListener('mousewheel', function (e) {e = e || window.eventconsole.log(e);// 获取整屏的高度viewHeight = document.body.clientHeight;if (flag) {  //节流阀flag = falseif (e.wheelDelta > 0) {index--if (index < 0) {index = 0}} else {index++;if (index > lis.length - 1) {index = lis.length - 1}}container.style.top = -index * viewHeight + 'px'changeColor(index)setTimeout(function () {flag = true}, 500)}})

这里是本案例最核心的函数部分,1.给页面绑定鼠标滚轮事件,对鼠标滚轮事件进行监听,

通过事件对象event的wheelDelta属性值来判断鼠标滚动的方向:

 wheelDelta的值为正的话说明鼠标向上滚动,值为负说明鼠标向下滚动。

2.这里使用了一个节流阀防止鼠标滚动过快导致屏幕滚动过快,用了一个开关思想,当鼠标触发滚动事件的时候将开关关闭,用延时定时器将开关打开。

小li颜色切换部分

 //绑定点击事件for (let i = 0; i < lis.length; i++) {lis[i].onclick = function () {viewHeight = document.body.clientHeightindex = ichangeColor(index)container.style.top = -index * viewHeight + 'px'}}//改变小li颜色function changeColor(index) {for (var j = 0; j < lis.length; j++) {lis[j].className = ''}lis[index].className = 'active'}

封装一个改变小li颜色的函数,通过传入索引,改变当前索引的小li类名,然后循环给小li绑定点击事件,这里使用let关键字的特点,具有块级作用域来保存每个小li的索引值,点击后将i值赋值给全局index保存索引。通过每次移动当前索引乘以整个屏幕的高度来实现滚动整个屏幕。

因为考虑兼容性问题,在点击小li或者滚动鼠标滚轮时重新获取一次整个屏幕的高度。防止因实时性出现问题。

二、代码总结

<!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;}html,body {width: 100%;height: 100%;overflow: hidden;}.container {width: 100%;height: 500%;position: absolute;top: 0;transition: all 0.3s ease;}.section1 {background-color: rebeccapurple;background: url(./轮播图/img/01.jpg);}.section2 {background-color: skyblue;background: url(./轮播图/img/02.jpg);}.section3 {background-color: red;background: url(./轮播图/img/03.jpg);}.section4 {background-color: orange;background: url(./轮播图/img/04.jpg);}.section5 {background-color: lightgreen;background: url(./轮播图/img/05.jpg);}.section {width: 100%;height: 20%;display: flex;color: white;justify-content: center;align-items: center;background-size: cover;}.controls {position: absolute;top: 50%;right: 20px;transform: translateY(-50%);list-style: none;}.controls li {width: 50px;height: 50px;font: bold 22px/50px '宋体';text-align: center;background-color: #000;color: white;cursor: pointer;border-radius: 50%;}.controls li+li {margin-top: 5px;}.controls li.active {background-color: #fff;color: red;}</style>
</head><body><div class="container"><div class="section section1"><h1>第1屏</h1></div><div class="section section2"><h1>第2屏</h1></div><div class="section section3"><h1>第3屏</h1></div><div class="section section4"><h1>第4屏</h1></div><div class="section section5"><h1>第5屏</h1></div></div><ul class="controls"><li class="active">1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><script>//实现滚动效果const container = document.querySelector('.container')const lis = document.querySelectorAll('.controls li') var viewHeight = null //声明页面高度var index = 0; //当前索引var flag = true; //节流开关document.addEventListener('mousewheel', function (e) {e = e || window.eventconsole.log(e);// 获取整屏的高度viewHeight = document.body.clientHeight;if (flag) {  //节流阀flag = falseif (e.wheelDelta > 0) {index--if (index < 0) {index = 0}} else {index++;if (index > lis.length - 1) {index = lis.length - 1}}container.style.top = -index * viewHeight + 'px'changeColor(index)setTimeout(function () {flag = true}, 500)}})//绑定点击事件for (let i = 0; i < lis.length; i++) {lis[i].onclick = function () {viewHeight = document.body.clientHeightindex = ichangeColor(index)container.style.top = -index * viewHeight + 'px'}}//改变小li颜色function changeColor(index) {for (var j = 0; j < lis.length; j++) {lis[j].className = ''}lis[index].className = 'active'}</script>
</body></html>

感谢大家浏览!!!

这篇关于用html+js+css3实现整屏滚动fullpage功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/427531

相关文章

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景