失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > javascript自定义滚动条实现代码【javascript】

javascript自定义滚动条实现代码【javascript】

时间:2020-11-25 11:23:34

相关推荐

javascript自定义滚动条实现代码【javascript】

web前端|js教程

javascript,滚动条

web前端-js教程

在工作中经常会遇到内容会超出固定的一个范围,超出的内容一般会使用到滚动条来滚动显示。

卡密发卡 php源码,ubuntu64 vim,tomcat百科手机版,jupyter 网络爬虫,PHP中的控制流程分为,汕头seo优势lzw

但是用浏览器默认的滚动条经常被产品经理鄙视,可是用css却改变不了滚动条的样式,还好,有万能的js ^_^~~

磁力链下载站源码,ubuntu永久增加网关,启动tomcat文件一闪而过,爬虫子的地方,为什么说php做网页特别好,优化重点seolzw

网上有各种各样的插件,但最顺手的还是自己写的,还可以一边撸一边当学习,自己动手丰衣足食 (*^__^*)

商家信息网 源码,vscode设置多张背景,ubuntu联网安装,如何清除tomcat,树莓派sqlite亮灯记录,python爬虫有哪几种代码,php imap 扩展,数据seo优化质量保障,网站制作工具 简易,不定时自动弹出网页,视频类建站模板下载lzw

其中这三个问题深深地困扰我:

1、滚动条高度

2、每次点击向上、向下按钮的时候滚动条应该移动多少距离

3、每拖动1px滚动条,页面需要移动多少?

整个的框架大概是长这样的:

先来看看第一个问题。

由于目前已经知道内容区域的高度和内容可视高度以及滚动条可滚动区域的高度了,由于内容区域与滚动条每次移动的距离都是成正比的,所以第一个问题很好解决:

滚动条可移动范围 / 滚动条高度 = 内容高度 / 内容可视高度

每次点击按钮的时候滚动条应该移动多少距离?

这里我是给参数distance设置一个值来决定每次点按钮的时候,内容区域应该滚动多少的距离。改变这个值可以改变内容区域滚动的快慢,如果有更好的处理方法和建议记得告诉我喔~

目前,内容区域每次滚动的距离是知道了,剩下的是计算滚动条相对于应该移动多少距离?

滚动条可移动范围 /滚动条每次移动距离 = 内容区域高度 / 内容区域每次移动多少距离

效果如下:

这里还有一个问题,就是同时还得区分是单次点击还是长按。

所以得判断一下从按下按钮到松开时候的时长,目前设置为<100ms为单次点击,否则为长按:

拖动滚动条的时候,每移动1px滚动条,内容区域需要移动多少?

先知道每1PX的距离占滚动条可移动范围的百分之几,再用内容区域高度除以所得的这个百分比,就得出滚动条每移动1px内容区域相对滚动多少距离了。

内容区域滚动的距离 = 内容区域高度 / (滚动条滚动区域 / 1)

demo完整代码如下:

注意:因为用的是seajs写的,所以稍微留意下文件的加载情况啦

css:

.wapper{scrollbar-3dlight-color:#000; position:relative; height:302px;width:300px;overflow:hidden;margin:0 auto;line-height:40px;text-align:center;} .area{background-color:#E2E2EF;width:100%; position:absolute;top:0px;left:0px;} .bar{position:absolute;top:0px;right:0px; height:100%;width:1rem;background-color:#ccc;} .scroll,.middle,.forward,.backward{display:block;cursor:pointer;position:absolute;right:0px;width:100%;} .forward,.backward{height:16px;background-color:#6868B1;} .middle{background-color:rgba(255, 255, 255, 0.22);top:16px;cursor:auto;} .scroll{position:absolute;top:0px;background-color:#C2C2E9;} .forward{top:0px;} .backward{bottom:0px;}

html:

1、this is content

2、this is content

3、this is content

4、this is content

5、this is content

6、this is content

7、this is content

8、this is content

9、this is content

10、this is content

11、this is content

seajs.use([lib/jquery/1.11.x/index.js, \_example/simulationScroll/simulationScroll.js], function($, scroll) { scroll.init({ wapper: $(.wapper), distance: 10, });});

js:

define(function(require, exports, module) { use strict; var $ = require(lib/jquery/1.11.x/index.js); var parameter = null; //检测设备类型 var startWhen, endWhen, moveWhen; var u = navigator.userAgent; if ( u.match(/\b(Windows\sNT|Macintosh)\b/) ) { // 鼠标 startWhen = mousedown; endWhen = mouseup; moveWhen = mousemove; } else { // 触摸屏 startWhen = ouchstart; endWhen = ouchend; moveWhen = ouchmove; } var simulation = { _mousedownTimer: 0, _setintervalId: 0, _longClick: false, //是否长点击 _turnOf: null, //滚动方向 init: function(options) { var t = this; t._scroll = $(.scroll); //滚动条 t._wapper = options.wapper.find(.area); //内容区域 t._distance = options.distance; //点击上下按钮页面每次滚动的距离 var forward = $(.forward), middle = $(.middle), backward = $(.backward); parameter = { view: t._wapper.parent().innerHeight(), //视图高度 page: t._wapper.height(), //内容高度 barArea: 0, //滚动条可移动范围 scrollHeight: 0, //滚动条的高度 scrollDistance: 0 //滚动条每次滚动的距离 }; //初始化滚动条 if (parameter.page > parameter.view) { //滚动条可移动范围 middle.height( parameter.view - forward.height() * 2); parameter.barArea = middle.height(); //滚动条高度 = 滚动条可滚动范围 / (页面高度 / 可视高度)的百分比 parameter.scrollHeight = parameter.barArea / (parameter.page / parameter.view) ; t._scroll.height(parameter.scrollHeight); //滚动条每次滚动的距离 = 滚动条可移动范围 * 页面每次滚动的百分比 parameter.scrollDistance = parameter.barArea / (parameter.page / t._distance) ; //拖动滚动条 t.liveEvent(); //点击向前按钮,如果按下鼠标到松开鼠标的时长<100ms,则为单次点击 forward.bind(startWhen, function(e){t._turnOf = forward;t.longPress(e, t.direction ); }).bind(endWhen, function(e) {t.mouseupFun(e, t.direction);t._turnOf = null; }); //点击向后按钮 backward.bind(startWhen, function(e){t.longPress(e, t.direction ); }).bind(endWhen, function(e){t.mouseupFun(e, t.direction ); }); //注册鼠标滚动事件 // FF if(document.addEventListener){document.addEventListener(DOMMouseScroll,t.mouseRuning,false); } //其它浏览器 document.onmousewheel = t.mouseRuning; } }, //鼠标滚动 mouseRuning: function(e) { var t = simulation; e = e || window.event; //ie、FF if (e.detail) { if (e.detail 0) {t._turnOf = forward;t.direction (); } else{t._turnOf = null;t.direction (); } } }, //判断是否长点击 longPress: function(e, moveFun ) { var t = this; if ( u.match(/\b(Windows\sNT|Macintosh)\b/) ) { e = e || window.event; // 限制为鼠标左键点击才触发 if (/^mouse/.test(e.type) && e.which !== 1) {return; } } t._setintervalId = setInterval(function(){ t._mousedownTimer += 10; if( t._mousedownTimer >= 100 ){moveFun(); } },20); }, mouseupFun: function(e, moveFun) {var t = this; if ( u.match(/\b(Windows\sNT|Macintosh)\b/) ) { e = e || window.event; // 限制为鼠标左键点击才触发 if (/^mouse/.test(e.type) && e.which !== 1) {return; } } clearTimeout(t._setintervalId); if( t._mousedownTimer < 100 ) { moveFun(); } t._mousedownTimer = 0; }, direction:function() { var t = simulation, barTop = t._scroll.position().top, pageTop = t._wapper.position().top, moveDistance = {}; if ( t._turnOf === forward) {//页面到顶,不执行任何操作if (barTop == 0) {return;}moveDistance = {page: pageTop + t._distance,bar: barTop - parameter.scrollDistance}//如果滚动条距离顶部的距离少 < 每次滚动的距离,或者已经滚动到顶部,则不再滚动if(barTop < parameter.scrollDistance || barTop <= 0){moveDistance = { page: 0, bar: 0}} } else {//页面到底,不执行任何操作if (barTop == parameter.barArea - parameter.scrollHeight){return;}moveDistance = {page: pageTop - t._distance,bar: barTop + parameter.scrollDistance};// 如果滚动条距离底部的距离值 = parameter.barArea) {moveDistance = { page: parameter.view - parameter.page, bar: parameter.barArea - parameter.scrollHeight};} } t._scroll.css({top: moveDistance.bar}); t._wapper.css({top: moveDistance.page}); }, //拖动滚动条 liveEvent: function() { var t = this, draging = false, currentY = 0, lastY = 0, pageY = 0; //检测设备类型 var _ua = function(e) { var Pos = null; if ( u.match(/\b(Windows\sNT|Macintosh)\b/) ) {e = e || window.event;// 限制为鼠标左键点击才触发if (/^mouse/.test(e.type) && e.which !== 1) {return;}Pos = {left : e.pageX,top: e.pageY} } else {Pos = {left : e.originalEvent.targetTouches[0].pageX,top: e.originalEvent.targetTouches[0].pageY} } return Pos; }; var _start = function(e) { //监控鼠标 e.preventDefault(); if (t._scroll.get(0).setCapture) {t._scroll.get(0).setCapture(); } draging = true; //记录当前滚动条的坐标 lastY = t._scroll.position().top;//记录按下鼠标的坐标 pageY = _ua(e).top; }; var _drag = function(e) { if( draging ) {var pageTop = t._wapper.position().top;var barTop = t._scroll.position().top;//滚动条每移动1px,页面相对滚动Npx 再 * 当前滚动条的到顶部的距离var pageMoveDistance = -(parameter.page / (parameter.barArea / 1)) * barTop;if (lastY + ( _ua(e).top - pageY ) = parameter.barArea) {currentY = parameter.barArea - parameter.scrollHeight;pageMoveDistance = parameter.view - parameter.page;}else {currentY = lastY + ( _ua(e).top - pageY);}t._scroll.css({ top:currentY});t._wapper.css({top: pageMoveDistance});} }; var _end = function(e) { if (draging) {draging = false;//在IE下释放对鼠标的控制if (t._scroll.get(0).setCapture) {t._scroll.get(0).releaseCapture();}document.onmousemove = null;document.onmouseup = null; } }; t._scroll.bind( startWhen, _start ); t._wapper.bind( startWhen, _start ); $(document).bind( moveWhen, _drag );$(document).bind( endWhen, _end ); $(document).bind(lur, _end); } } return simulation;});

以上就是javascript模拟滚动条实现代码,希望对大家的学习有所帮助。

如果觉得《javascript自定义滚动条实现代码【javascript】》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。