js 简易版滚动条实例(适用于移动端H5开发)
程序员文章站
2022-05-26 10:57:03
废话不多说,直接上代码
滑动条...
废话不多说,直接上代码
<!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>
js代码:
/** * 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'+''); } } } })();
以上这篇js 简易版滚动条实例(适用于移动端h5开发)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。