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

废话不多说,直接上代码

滑动条

*{

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;

}

1111

2222

3333

4444

5555

6666

7777

8888

9999

0000

aaaa

bbbb

cccc

dddd

eeee

var options ={

interactiveScrollbars:true

}

window.hScoll.buildScoll('content',options);

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开发)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值