配合鼠标滚轮事件的可自定义样式的区域滚动条插件(基于jquery)
2013-07-02 15:43
1741 查看
预览效果:
![](http://images.cnitblog.com/blog/454674/201307/02154403-2cd9b159082747569e4b5e0dcc1231d2.gif)
CSS:
DOM:
JS:
调用方式:
1,首先要链接Jquery库
2,可以自由修改css,来实现需要的效果
3,页面可以出现多个类似的插件
执行创建所有滚动条插件
![](http://images.cnitblog.com/blog/454674/201307/02154403-2cd9b159082747569e4b5e0dcc1231d2.gif)
CSS:
.scrollbar{ margin:20px;width:300px;height:300px; background:#303030; color:#CCC;} .scrollbar-track{ float:right;height:300px;width:14px; background:#3C3C3C;} .scrollbar-handle{ background:#616161; height:80px; border-radius:7px; cursor:pointer;} .handle-hover{ background:#777;} .scrollbar-area{float:leftwidth:290px;height:300px; overflow:hidden}
DOM:
<div class="J_PZScrollbar scrollbar" data-dir="y"> <div class="scrollbar-track"> <div class="scrollbar-handle"></div> </div> <div class="scrollbar-area"> <div class="scroll-content"> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> <p>wefwefwef</p> </div> </div> </div>
JS:
(function(){ /** @基于jQuery滚动条插件 @修改css来实现需要的效果。 @页面可出现多个类似插件,只要复制dom结构到对应位置即可。 @杨永 @QQ:377746756 @call:18911082352 @版本:1.0 */ function PZScrollbar(object){ var _this_=this; //保存传递将来的对象 this.scrollObject=object; //获取方向属性 this.dir=this.scrollObject.attr("data-dir"); //保存滚动对象 this.scrollContent=$(".scroll-content",object); //获取滚动内容于于可视区域的比例 this.beishu={ mx:$(".scroll-content",object).width()/$(".scrollbar-area",object).width(), my:$(".scroll-content",object).height()/$(".scrollbar-area",object).height() }; //保存拖动句柄 this.scrollHandle=$(".scrollbar-handle",object); //设置滑块的尺寸 this.setScrollHandleSize(); //保存拖动句柄的父节点相对页面的偏移,尺寸 this.scrollHandleAreaPos={ top:this.scrollHandle.parent().offset().top, left:this.scrollHandle.parent().offset().left, width:this.scrollHandle.parent().width(), height:this.scrollHandle.parent().height(), maxY:this.scrollHandle.parent().height()-this.scrollHandle.height(), maxX:this.scrollHandle.parent().width()-this.scrollHandle.width() }; //获取滚动时他们之间的滚动关系倍数 this.scrollBeishu={ y:($(".scroll-content",object).height()-this.scrollHandle.parent().height())/(this.scrollHandle.parent().height()-this.scrollHandle.height()), x:($(".scroll-content",object).width()-this.scrollHandle.parent().width())/(this.scrollHandle.parent().width()-this.scrollHandle.width()) }; //初始化鼠标按下时,鼠标相对于滑动句柄的偏移值 this.mouseDownHandlePos={ top:null, left:null }; this.scrollHandle.mousedown(function(e){ var _this=this; //当鼠标按下时,设置鼠标相对于滑动句柄的偏移值 _this_.mouseDownHandlePos={ top:e.pageY-$(this).offset().top, left:e.pageX-$(this).offset().left }; //当鼠标按下的时,就绑定document的鼠标移动事件 $(document).mousemove(function(e){ //当移动的时候清除掉选中文本 if(document.selection&&document.selection.empty){//清楚IE选中文本 document.selection.empty(); }else if(window.getSelection){//清楚FF选中文本 window.getSelection().removeAllRanges(); }; //当鼠标在页面上移动的时候,实时计算出滑块的偏移值 var offset=_this_.getScrollHandleOffset( { top:e.pageY-_this_.scrollHandleAreaPos.top, left:e.pageX-_this_.scrollHandleAreaPos.left }, { top:_this_.mouseDownHandlePos.top, left:_this_.mouseDownHandlePos.left } ); if(_this_.dir=="y"){ _this_.animateScroll(offset.top,_this); }else{ _this_.animateScroll(offset.left,_this); }; }).mouseup(function(){ //当鼠标抬起的时候解绑定 $(this).unbind("mousemove"); }); }).hover(function(){ $(this).addClass("handle-hover"); },function(){ $(this).removeClass("handle-hover"); }); //指定一个默认计数 this.loop=0; //给整个对象绑定滚轮事件 if(window.addEventListener){ this.scrollObject.get(0).addEventListener("DOMMouseScroll",function(e){ e.preventDefault(); _this_.toHandleTopValue(e.detail); },false); this.scrollObject.get(0).addEventListener("mousewheel",function(e){ e.preventDefault(); //为了兼容chrom _this_.toHandleTopValue(e.wheelDelta<0?120:-120); },false); }else{ this.scrollObject.get(0).attachEvent("onmousewheel",function(e){ e.returnValue=false; _this_.toHandleTopValue(e.wheelDelta<0?120:-120); }) ; }; } PZScrollbar.prototype={ //当滚轮滚动的时候驱动滑块的位置 toHandleTopValue:function(e){ //判断鼠标的滚动方向 if(e>0){ this.loop+=20; if(this.dir=="y"){ if(this.loop>=this.scrollHandleAreaPos.maxY){ this.loop=this.scrollHandleAreaPos.maxY; }; }else{ if(this.loop>=this.scrollHandleAreaPos.maxX){ this.loop=this.scrollHandleAreaPos.maxX; }; }; this.animateScroll(this.loop,this.scrollHandle); }else{ this.loop-=20; if(this.loop<=0){this.loop=0;}; this.animateScroll(this.loop,this.scrollHandle); }; }, //驱动滚的区域 animateScroll:function(margin,thisObj){ //在鼠标拖动的时候把偏移值设置给计数器 this.loop=margin; //判断节点传递滚动方向 if(this.dir=="y"){ $(thisObj).css("marginTop",margin+"px"); this.scrollContent.css("marginTop",-margin*this.scrollBeishu.y+"px"); }else{ $(thisObj).css("marginLeft",margin+"px"); this.scrollContent.css("marginLeft",-margin*this.scrollBeishu.x+"px"); }; }, //按照比例设置滑块的尺寸 setScrollHandleSize:function(){ if(this.dir=="y"){ this.scrollHandle.height(this.scrollHandle.parent().height()/this.beishu.my+"px"); }else{ this.scrollHandle.width(this.scrollHandle.parent().width()/this.beishu.mx+"px"); } }, //计算出滑动句柄相对于滑动区域的便宜 getScrollHandleOffset:function(mouse,offset){ var x,y; if(mouse.top-offset.top<=0){ y=0; }else{ y=mouse.top-offset.top }; if(mouse.top-offset.top>=this.scrollHandleAreaPos.maxY){ y=this.scrollHandleAreaPos.maxY; }; if(mouse.left-offset.left<=0){ x=0; }else{ x=mouse.left-offset.left }; if(mouse.left-offset.left>=this.scrollHandleAreaPos.maxX){ x=this.scrollHandleAreaPos.maxX; }; return {top:y,left:x}; } }; PZScrollbar.init=function(scrolls){ var _this=this; scrolls.each(function(){ new _this($(this)); }); }; window["PZScrollbar"]=PZScrollbar; })();
调用方式:
1,首先要链接Jquery库
2,可以自由修改css,来实现需要的效果
3,页面可以出现多个类似的插件
执行创建所有滚动条插件
$(function(){ //初始化页面所以需要滚动条的区域 PZScrollbar.init($(".J_PZScrollbar")); });
相关文章推荐
- jquery的鼠标滚轮插件 Mousewheel响应跨浏览器的鼠标滚轮事件
- jquery 响应不同浏览器 鼠标滚轮上下滚动事件 相对的操作
- jquery监听鼠标滚轮(滚动)事件
- jquery的鼠标滚轮插件 Mousewheel响应跨浏览器的鼠标滚轮事件
- 常用的js插件-配合滚轮事件左右滚动
- jquery图片滚动插件(可自定义数字样式和左右点击按钮)
- jquery鼠标移入某区域屏蔽鼠标滚轮 滚动滚动条
- jquery 鼠标滚轮事件 插件 Mousewheel
- 滚动 CSS3 鼠标滚轮滚动插件jQuery Smoove.js
- 2.0Tip/Trick之Silverlight.js, Silverlight.supportedUserAgent.js, 自定义启动界面, 响应鼠标滚轮事件
- 基于jQuery的的一个隔行变色,鼠标移动变色的小插件
- jQuery实现鼠标滚轮动态改变样式或效果
- 基于jquery实现鼠标滚轮驱动的图片切换效果
- jQuery滚动事件之鼠标滚动到底部再加载数据
- 基于jQuery实现一个marquee无缝滚动的插件
- jcarousellite.js 基于Jquery的图片无缝滚动插件
- [PyQt] 自定义QSlider支持鼠标点击和滚轮事件
- Android开发 图片轮播条Lanner(可无限滚动循环,自定义dots样式位置,延迟时间,滚动时间,点击事件)
- 基于JQuery的Swiper插件实现移动手机端幻灯片切换(可在每一张幻灯片div中插入自定义的元素,代码简介,扩展性好。)