-
Notifications
You must be signed in to change notification settings - Fork 30
/
jquery.jscrollbar-2.0.0.min.js
16 lines (16 loc) · 4.64 KB
/
jquery.jscrollbar-2.0.0.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @fileOverview jquery.jscrollbar
* @author daiying.zhang
* @mail zdying@live.com/97532151@qq.com
* @site http://imf2e.com
* @version 2.0.0
*/
(function(f){function p(a,b,c,d){var e=this.opt,m=this.$con[0],g=this.$node.clone().appendTo("body").width(b).height(c).find(".jscrollbar"),n=e.showXBar&&m.scrollWidth>b,m=e.showYBar&&m.scrollHeight>c,k=0,k="",h="outer"===e.position?d:0;n&&m?k="xy":n?(k=g.css({height:c-h,zIndex:-1})[0].scrollHeight,k="x"+(e.showXBar&&k>c-h?"y":"")):m&&(k=g.css({width:b-h,zIndex:-1})[0].scrollWidth,k=(e.showXBar&&k>b-h?"x":"")+"y");this.scrollWidth=g[0].scrollWidth;this.scrollHeight=g[0].scrollHeight;g.parent().remove();
this.bars=k;e=this.delta=(this.bars.length-1)*d;g=this.thumbSize={x:q.call(this,"x"),y:q.call(this,"y")};this.maxPos={x:b-g.x-e,y:c-g.y-e};this.maxSPos={x:this.scrollWidth-b+e,y:this.scrollHeight-c+e};b=this.bars.split("");n=0;for(m=b.length;n<m;n++)c=l[g=b[n]],e=l["xy".replace(g,"")].s,f("<div></div>").addClass(g).data("thumbType",g).css(e,d).append(f(r)[c.s](this.thumbSize[g]).data("type",g)).insertAfter(this.$con.css(e,"-="+("outer"===this.opt.position?d:0)));"inner"===this.opt.position&&a.find(".x,.y").css("background",
"transparent")}function q(a){a=l[a];return Math.max(20,Math.pow(this.$con[a.s]()-this.delta,2)/this.$con[0][a.ss])}function s(){var a=this;this.$node.on("mousedown",".thumb",function(b){t.call(a,b,b.target);return!1}).on("click",".x,.y",function(b){var c=0,c=b.target,d=f.data(c,"thumbType"),e=l[d];c===this&&(c=b["page"+d.toUpperCase()]-f(c).offset()[e.p],a.scrollTo(d,c/a.maxPos[d]*a.maxSPos[d],a.opt.speed))});f(document).unbind("mouseup.jsb").bind("mouseup.jsb",function(){f(this).unbind("mousemove.jsb")});
u.call(this);"inner"===this.opt.position&&v.call(this)}function v(){var a=this.$node;a.hover(function(){a.find(".x,.y").stop().animate({opacity:1},200)},function(){a.find(".x,.y").stop().animate({opacity:0},500)}).mouseout()}function u(){var a=this.node,b=this,c=null;this.opt.mouseEvent&&(c=function(a){a=a||window.event;b.scrollBy(2===b.bars.length?"y":b.bars,-(a.wheelDelta?a.wheelDelta/120:-a.detail/3)*b.opt.mouseSpeed);f.event.fix(a).preventDefault()},a.addEventListener?(a.addEventListener("mousewheel",
c),a.addEventListener("DOMMouseScroll",c)):a.onmousewheel=c)}function t(a,b){var c={X:a.pageX,Y:a.pageY},d=this,e={X:parseInt(f(b).css("left"))||0,Y:parseInt(f(b).css("top"))||0};f(document).bind("mousemove.jsb",function(a){var g=f.data(b,"type"),h=g.toUpperCase(),k=l[g].p;a=Math.min(Math.max(0,e[h]+(a["page"+h]-c[h])),d.maxPos[g]);f(b).css(k,a);d.scroll(g);return!1})}function h(a,b){this.node=a[0];this.$node=a;this.width=a.width();this.height=a.height();this.opt=b;this.plugID="jsb_"+Math.floor(9E3*
Math.random()+1E3);var c=this.width,d=this.height,e=b.width;this.$node.css("overflow","hidden").addClass("jscrollbar").wrapInner(w.attr("id",this.plugID).css({width:c,height:d}));this.$con=f("#"+this.plugID);p.call(this,this.$node,c,d,e);s.call(this)}var w=f('<div class="jscrollbar" style="overflow: hidden"></div>'),r='<div class="thumb"></div>',l={x:{s:"width",p:"left",sp:"scrollLeft",ss:"scrollWidth"},y:{s:"height",p:"top",sp:"scrollTop",ss:"scrollHeight"}};h.fn=h.prototype;h.fn.update=function(a){var b=
this.$node,c=this.width=b.width(),d=this.height=b.height(),e=this.opt.width,f=this.getScrollPos("x"),g=this.getScrollPos("y");this.$node.find(".x,.y").remove();this.$con.css({width:c,height:d});p.call(this,b,c,d,e);switch(a){case "relative":f&&this.scrollTo("x",f);g&&this.scrollTo("y",g);break;case "bottom":this.scrollTo("y",this.maxSPos.y);break;case "right":this.scrollTo("x",this.maxSPos.x);break;default:this.scroll()}this.scroll()};h.fn.getThumbLocation=function(a){return parseFloat(this.$node.find("."+
a+" .thumb").css(l[a].p))||0};h.fn.getScrollPos=function(a){return this.$con[0][l[a].sp]};h.fn.scroll=function(a){void 0===a&&(a="x",this.scroll("y"));this.$con[0][l[a].sp]=this.getThumbLocation(a)/this.maxPos[a]*this.maxSPos[a]};h.fn.scrollBy=function(a,b){this.scrollTo(a,this.$con[0][l[a].sp]+b)};h.fn.scrollTo=function(a,b,c){b=Math.max(Math.min(this.maxSPos[a],b),0);var d={};d[l[a].sp]=b;this.$con.animate(d,c||0);this.$node.trigger("scroll",[a,b]);c=c||0;var d=l[a],e={};e[d.p]=this.maxPos[a]*(b||
this.$con[0][d.sp])/this.maxSPos[a];this.$node.find("."+a+" .thumb").animate(e,c)};f.fn.jscrollbar=function(a){if("string"===typeof a){var b=this.data("jsb_data");return b?b[a].apply(b,[].slice.call(arguments,1)):this}a=f.extend({},{width:8,position:"inner",showXBar:!0,showYBar:!0,mouseEvent:!0,mouseSpeed:30,speed:250},a);return this.each(function(){f.data(this,"jsb_data")||f.data(this,"jsb_data",new h(f(this),a))})};f.fn.jscrollbar.version="2.0.0"})(jQuery);