首页 > 作文

简洁的纯JS全屏滚动代码

更新时间:2023-03-31 14:05:13 阅读: 评论:0

htmldiv id=”scrollPage” style=”position:fixed;width:100%;height:100%;top:0px;left:0px;z-index:3;overflow:hidden;text-align:center;”div class=”scrollBox”第一页/divdiv class=”scrollBox”第二页/divdiv class=”scrollBox”第三页/divdiv class=”scrollBox”第四页/div/divjs(function(){ let scrollPage = document.getElementById(‘scrollPage’); scrollPage.style.visibilit农村环保y = “visible”; let boxes = document.querySelectorAll(‘.scrollBox’); let scrollTimeout = 0; let scrollInterval = 0; let wheelDelta = 0; let scrollIndex = 0; let targetTop = 0; let touchStartPoint = 0; let touchStartTop = 0; /*屏幕定位函数*/ let scrollTo = function(jump){ if(jump!==undefined){ if(jump===scrollIndex){return;} scrollIndex = jump; }el { if(scrollTimeout||scrollInterval||(wheelDelta0scrollPage.scrollTop==0)||(wheelDelta0scrollPage.scrollTop+scrollPage.offtHeight==scrollPage.scrollHeight)){return;} scrollIndex = wheelDelta0?scrollIndex-1:scrollIndex+1; } targetTop = scrollIndex*scrollPage.offtHeight; if(!(scrollTimeout||scrollInterval)){ scrollTimeout = window.tTimeout(function(){ scrollInterval = window.tInterval(function(){ let q = (targetTop-scrollPage.scrollTop)*0.25;//缓冲动作漫画速度 scrollPage.scrollTop = scrollPage.scrollTop+q; if(parInt(q)==0){ scrollPage.scrollTop = targetTop; window.clearInterval(scrollInterval); sc完全立方rollTimeout = 0; scrollInterval = 0; } },50);//帧速度 },touchStartPoint?0:250);//使用鼠独立女性标滑轮时的延时响应(避免一次滚动多屏),如果是触屏滑动事情则不延时 } }; /*鼠标滚动事情*/ scrollPage.addEventListener(‘mouwheel’,function(e){ wheelDelta = e.wheelDelta; scrollTo(); } ); /*触屏滑动事情*/ scrollPage.addEventListener(‘touchstart’,function(e){ touchStartPoint = e.touches[0].pageY; touchStartTop = scrollPage.scrollTop; e.preventDefault(); }); scrollPage.addEventListener(‘touchend’,function(e){ wheelDelta = e.changedTouches[0].pageY-touchStartPoint; if(Math.abs(wheelDelta)75){ scrollTo(); }el{ scrollPage.scrollTop = touchStartTop; } }); scrollPage.addEventListener(‘touchmove’,function(e){ if(!(scrollTimeout||scrollInterval)){ scrollPage.scrollTop = touchStartTop+(touchStartPoint-e.changedTouches[0].pageY); } }); /*定义右边迅速导航按钮*/ let navBar = document.createElement(“div”); navBar.tAttribute(“style”,”display:flex;flex-flow:column;align-items:center;justify-content:center;position:fixed;right:0px;top:50%;”); for(let i=0;iboxes.length;i++){ let navButton = document.createElement(“a”); navButton.tAttribute(“style”,”display:block;width:15px;height:15px;background:rgba(0,0,0,0.5);border-radius:100%;margin:10px;”); navButton.index = i; navButton.addEventListener(“click”,function(){ scrollTo(this.index); }); let scrollListener = function(e){ if(navButton.index==scrollIndex){ nav消防员的英雄事迹Button.style.background = R七一重要讲话20;rgba(255,255,255,0.5)”; }el{ navButton.style.background = “rgba(0,0,0,0.5)”; } }; scrollListener(); scrollPage.addEventListener(“scroll”,scrollListener); navBar.appendChild(navButton); } scrollPage.appendChild(navBar); navBar.style.marginTop = (navBar.offtHeight/-2)+”px”; let init = function(){ for(let i=0;iboxes.length;i++){ let box = boxes[i]; box.tAttribute(“style”,”height:”+scrollPage.offtHeight+”px;background:#”+((i+3)*123)+”;color:#fff;display:flex;justify-content:center;align-items:center;flex-flow:column;text-align:center;”); } } init(); //窗口尺寸变化时重置模块高度以及定位 window.addEventListener(“resize”,function(){ init(); scrollPage.scrollTop = scrollIndex*scrollPage.offtHeight });})();

本文发布于:2023-03-31 14:05:12,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/6b6c3ef3651bb755cad41193837a3b4a.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:简洁的纯JS全屏滚动代码.doc

本文 PDF 下载地址:简洁的纯JS全屏滚动代码.pdf

标签:事情   鼠标   消防员   滑轮
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图