欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js 简易版滚动条实例(适用于移动端H5开发)

程序员文章站 2023-02-19 17:28:28
废话不多说,直接上代码 滑动条...</div> <div class="content"> <p><span style="background-color: #ccffcc"><strong>废话不多说,直接上代码</strong></span></p> <div class="jb51code"> <pre class="brush: javascript;"> <!doctype html> <html> <head> <title>滑动条</title> <meta charset="utf-8"> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <script type="text/javascript" src="./hscoll.js"></script> </head> <style> *{ margin: 0; padding: 0; } #content{ margin-top: 50px; width:100%; height: 200px; background: #eeeeee; overflow: hidden; position: relative; /**transform: translate(0px, -70px);*/ } #scoll{ overflow: hidden; } #content2{ margin-top: 50px; width:100%; height: 200px; background: red; overflow: hidden; position: relative; /**transform: translate(0px, -70px);*/ } #scoll2{ overflow: hidden; } .scrollbars{ position: absolute; height: 100%; right: 0; top: 0; width: 5px; border-radius: 5px; } .scollb{ position: absolute; right: 0; top: 0; width: 100%; background: #999999; border-radius: 5px; } </style> <body> <div id="content"> <div id="scoll"> <p>1111</p> <p>2222</p> <p>3333</p> <p>4444</p> <p>5555</p> <p>6666</p> <p>7777</p> <p>8888</p> <p>9999</p> <p>0000</p> <p>aaaa</p> <p>bbbb</p> <p>cccc</p> <p>dddd</p> <p>eeee</p> </div> </div> </body> <script> var options ={ interactivescrollbars:true } window.hscoll.buildscoll('content',options); </script> </html></pre> </div> <p><strong>js代码:</strong></p> <div class="jb51code"> <pre class="brush: javascript;"> /** * created by hechao on 2017/6/25. */ (function(){ /**添加window对象hscoll属性*/ window.hscoll = { buildscoll:function(el,options){ app.init(el,options); } } var app = { /**初始化组件*/ init:function(el,option){ app.options = option; app.prevy = 0; app.el = document.getelementbyid(el); app.scoll = this.el.children[0]; app.h = this.el.offsetheight;//滑动范围高度 app.ch = this.el.scrollheight;//内容的高度 if(parsefloat(this.h)<=parsefloat(this.ch)){ app.sdiv = document.createelement('div'); app.scollb = document.createelement('div'); app.sdiv.setattribute('class','scrollbars'); app.scollb.setattribute('class','scollb'); app.scollb.style.height = parsefloat(this.h)*parsefloat(this.h)/parsefloat(this.ch) + 'px'; app.el.appendchild(this.sdiv); app.sdiv.appendchild(this.scollb); app.initevent(); } }, /**绑定事件*/ initevent:function (){ app.el.addeventlistener('touchstart', app.touchstart, false); app.el.addeventlistener('touchmove', app.touchmove, false); app.el.addeventlistener('touchend', app.touchend, false); }, /**记录滑动初始位置*/ touchstart:function(e){ var point = app.getpoint(e); app.starty = point.pagey; }, /**手指移动时,滚动条滚动*/ touchmove:function(e){ e.preventdefault();//阻止默认行为 var point = app.getpoint(e); app.movey = point.pagey; app.deltay = app.starty - app.movey; if((app.prevy - app.deltay)<=0 && (app.prevy - app.deltay)>= -(app.ch-app.h)){ app.domove(app.prevy - app.deltay); } if(app.options.interactivescrollbars){ app.domove2(app.prevy - app.deltay); }else{ if((app.prevy - app.deltay)<=0 && (app.prevy - app.deltay)>= -(app.ch-app.h)){ app.domove2(app.prevy - app.deltay); } } }, /**手指离开时,判断位置*/ touchend:function(e){ app.prevy = app.prevy - app.deltay; if(app.prevy >= 0){ app.prevy = 0; app.domove(app.prevy,true); app.domove2(app.prevy,true); } if(app.prevy <= -(app.ch-app.h)){ app.prevy = -(app.ch-app.h); app.domove(app.prevy,true); app.domove2(app.prevy,true); } }, getpoint:function (e) { return e.touches ? e.touches[0] : e; }, /**内容滑动*/ domove:function (y,t){ if(t){ app.scoll.setattribute('style', 'transform: translate(0px, '+y+'px);transition:transform 300ms ease'); }else{ app.scoll.setattribute('style', 'transform: translate(0px, '+y+'px);transition:transform 0ms ease'); } }, /**滚动条滑动*/ domove2:function(y,t){ if(t){ app.scollb.setattribute('style', 'transform: translate(0px, '+-parsefloat(y)*parsefloat(app.h)/parsefloat(app.ch)+'px);transition:transform 0ms ease;height:'+parsefloat(app.h)*parsefloat(app.h)/parsefloat(app.ch) + 'px'+''); }else{ app.scollb.setattribute('style', 'transform: translate(0px, '+-parsefloat(y)*parsefloat(app.h)/parsefloat(app.ch)+'px);transition:transform 0ms ease;height:'+parsefloat(app.h)*parsefloat(app.h)/parsefloat(app.ch) + 'px'+''); } } } })();</pre> </div> <p>以上这篇js 简易版滚动条实例(适用于移动端h5开发)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。</p> </div> <div class="info-pre-next"> <p> 上一篇: <a href="/article/1989662.html"> 抖音上最火的头发颜色:北极星绿上榜,第八饱和度高 </a> </p> <p> 下一篇: <a href="/article/1989664.html"> 十大内衣品牌,爱慕内衣力压古今内衣 </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/1989663.html" target="_blank" title="js 简易版滚动条实例(适用于移动端H5开发)"> <h2> js 简易版滚动条实例(适用于移动端H5开发) </h2> </a> </li> <li> <a href="/article/1142430.html" target="_blank" title="js 简易版滚动条实例(适用于移动端H5开发)"> <h2> js 简易版滚动条实例(适用于移动端H5开发) </h2> </a> </li> </ul> </div> </article> </div> </main> <footer><div class="box"><div class="ft_nav"><div class="ft_about"><p>关于网站</p><ul><li><a href="/sitemap.xml" target="_blank" title="网站地图">网站地图</a></li><li><a href="/list/2/" title="最新程序员文章站">最新程序员文章站</a></li></ul></div><div class="ft_contact"><ul><li>本站所有数据收集于网络如有侵犯到您的权益,请联系我们进行下架处理。</li><li class="email_show"></li></ul></div></div><div class="copyright"><div class="cr_left"><p> 备案号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备20058927号</a></p><p>© Copyright © 2020-2022 www.superweb999.com 程序员文章站. </p></div></div></div></footer> </body> </html>