forked from azoff/overscroll
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.overscroll.min.js
28 lines (25 loc) · 6.95 KB
/
jquery.overscroll.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Overscroll v1.3.5
A jQuery Plugin that emulates the iPhone scrolling experience in a browser.
http://azoffdesign.com/overscroll
Intended for use with the latest jQuery
http://code.jquery.com/jquery-latest.min.js
Copyright 2010, Jonathan Azoff
Dual licensed under the MIT or GPL Version 2 licenses.
http://jquery.org/license
Date: Thursday, August 26th 2010
*/
(function(k,j,f,c){c=f.fn.overscroll=function(a){return this.each(function(){c.init(f(this),a)})};f.extend(c,{events:{wheel:"mousewheel DOMMouseScroll",start:"select mousedown touchstart",drag:"mousemove touchmove",scroll:"scroll",resize:"resize",end:"mouseup mouseleave touchend",ignored:"dragstart drag"},div:"<div/>",noop:function(){return false},constants:{scrollDuration:800,timeout:400,captureThreshold:3,wheelDelta:20,scrollDelta:5.9,thumbThickness:8,thumbOpacity:0.7,boundingBox:1E6,cssURL:/url\((.+)\)/},
init:function(a,b,d,e){d={};b=f.extend({openedCursor:"url('http://github.com/downloads/azoff/Overscroll/opened.cur')",closedCursor:"url('http://github.com/downloads/azoff/Overscroll/closed.cur')",showThumbs:true,wheelDirection:"vertical",wheelDelta:c.constants.wheelDelta,scrollDelta:c.constants.scrollDelta,direction:"multi",onDriftEnd:f.noop},b||{});b.scrollDelta=j.abs(b.scrollDelta);b.wheelDelta=j.abs(b.wheelDelta);b.cache={openedCursor:new Image,closedCursor:new Image};if(e=c.constants.cssURL.exec(b.openedCursor))b.cache.openedCursor.src=
e[1];if(e=c.constants.cssURL.exec(b.closedCursor))b.cache.closedCursor.src=e[1];b.openedCss={cursor:b.openedCursor+",default"};b.closedCss={cursor:b.closedCursor+",default"};a.css("overflow","hidden").css(b.openedCss).bind(c.events.wheel,d,c.wheel).bind(c.events.start,d,c.start).bind(c.events.end,d,c.stop).bind(c.events.resize,d,c.resize).bind(c.events.ignored,c.noop);d.target=a;d.options=b;c.setThumbs(d)},toggleDragMode:function(a,b){b?a.target.css(a.options.closedCss):a.target.css(a.options.openedCss);
if(a.thumbs)if(b){a.thumbs.vertical&&a.thumbs.vertical.stop(true,true).fadeTo("fast",c.constants.thumbOpacity);a.thumbs.horizontal&&a.thumbs.horizontal.stop(true,true).fadeTo("fast",c.constants.thumbOpacity)}else{a.thumbs.vertical&&a.thumbs.vertical.fadeTo("fast",0);a.thumbs.horizontal&&a.thumbs.horizontal.fadeTo("fast",0)}},setPosition:function(a,b,d){b.x=a.pageX;b.y=a.pageY;b.index=d;return b},wheel:function(a,b){if(a.wheelDelta)b=a.wheelDelta/(k.opera?-120:120);if(a.detail)b=-a.detail/3;if(!a.data.wheelCapture){a.data.wheelCapture=
{timeout:null};c.toggleDragMode(a.data,true);a.data.target.stop(true,true).data("dragging",true)}b*=a.data.options.wheelDelta;if(a.data.options.wheelDirection==="horizontal")this.scrollLeft-=b;else this.scrollTop-=b;a.data.wheelCapture.timeout&&clearTimeout(a.data.wheelCapture.timeout);a.data.wheelCapture.timeout=setTimeout(function(){a.data.wheelCapture=undefined;c.toggleDragMode(a.data,false);a.data.target.data("dragging",false);a.data.options.onDriftEnd.call(a.data.target,a.data)},c.constants.timeout);
return false},scroll:function(a,b,d,e,i,g,h){b=a.data.thumbs;d=a.data.sizing;e=this.scrollLeft;i=this.scrollTop;if(b.horizontal){g=e*d.container.width/d.container.scrollWidth;h=d.thumbs.horizontal.top;if(d.relative){g+=e;h+=i}b.horizontal.css("margin",h+"px 0 0 "+g+"px")}if(b.vertical){g=d.thumbs.vertical.left;h=i*d.container.height/d.container.scrollHeight;if(d.relative){g+=e;h+=i}b.vertical.css("margin",h+"px 0 0 "+g+"px")}},start:function(a){a.data.target.bind(c.events.drag,a.data,c.drag).stop(true,
true).data("dragging",false);c.toggleDragMode(a.data,true);a.data.position=c.setPosition(a,{});a.data.capture=c.setPosition(a,{},2);return true},drag:function(a){if(a.data.options.direction!=="vertical")this.scrollLeft-=a.pageX-a.data.position.x;if(a.data.options.direction!=="horizontal")this.scrollTop-=a.pageY-a.data.position.y;c.setPosition(a,a.data.position);if(--a.data.capture.index<=0){a.data.target.data("dragging",true);c.setPosition(a,a.data.capture,c.constants.captureThreshold)}return true},
stop:function(a,b,d,e){if(a.data.position){a.data.target.unbind(c.events.drag,c.drag);if(a.data.target.data("dragging")){b=a.data.options.scrollDelta*(a.pageX-a.data.capture.x);d=a.data.options.scrollDelta*(a.pageY-a.data.capture.y);e={};if(a.data.options.direction!=="vertical")e.scrollLeft=this.scrollLeft-b;if(a.data.options.direction!=="horizontal")e.scrollTop=this.scrollTop-d;a.data.target.animate(e,{duration:c.constants.scrollDuration,easing:"cubicEaseOut",complete:function(){a.data.target.data("dragging",
false);a.data.options.onDriftEnd.call(a.data.target,a.data);c.toggleDragMode(a.data,false)}})}else c.toggleDragMode(a.data,false);a.data.capture=a.data.position=undefined}return!a.data.target.data("dragging")},resize:function(a){c.setThumbs(a.data)},setThumbs:function(a){var b=a.target,d=a.options;a.sizing=c.getSizing(b);if(d.showThumbs){if(a.thumbs){a.thumbs.horizontal&&a.thumbs.horizontal.remove();a.thumbs.vertical&&a.thumbs.vertical.remove()}else a.thumbs={};if(a.sizing.container.scrollWidth>0&&
d.direction!=="vertical"){a.thumbs.horizontal=f(c.div).css(c.getThumbCss(a.sizing.thumbs.horizontal,d.thumbCss)).fadeTo(0,0);b.prepend(a.thumbs.horizontal)}if(a.sizing.container.scrollHeight>0&&d.direction!=="horizontal"){a.thumbs.vertical=f(c.div).css(c.getThumbCss(a.sizing.thumbs.vertical,d.thumbCss)).fadeTo(0,0);b.prepend(a.thumbs.vertical)}a.sizing.relative=a.thumbs.vertical||a.thumbs.horizontal;if(a.sizing.relative){a.sizing.relative.oldOffset=a.sizing.relative.offset();b.scrollTop(c.constants.boundingBox).scrollLeft(c.constants.boundingBox);
a.sizing.relative.remove().prependTo(b);a.sizing.relative.newOffset=a.sizing.relative.offset();a.sizing.relative=a.sizing.relative.oldOffset.left!=a.sizing.relative.newOffset.left||a.sizing.relative.oldOffset.top!=a.sizing.relative.newOffset.top;b.scrollTop(0).scrollLeft(0);b.bind(c.events.scroll,a,c.scroll)}}},getSizing:function(a,b,d){b={};b.container={width:a.width(),height:a.height()};d={left:a.scrollLeft(),top:a.scrollTop()};a.scrollLeft(c.constants.boundingBox).scrollTop(c.constants.boundingBox);
b.container.scrollWidth=a.scrollLeft();b.container.scrollHeight=a.scrollTop();a.scrollTop(d.top).scrollLeft(d.left);b.thumbs={horizontal:{width:b.container.width*b.container.width/b.container.scrollWidth,height:c.constants.thumbThickness,corner:c.constants.thumbThickness/2,left:0,top:b.container.height-c.constants.thumbThickness},vertical:{width:c.constants.thumbThickness,height:b.container.height*b.container.height/b.container.scrollHeight,corner:c.constants.thumbThickness/2,left:b.container.width-
c.constants.thumbThickness,top:0}};b.container.width-=b.thumbs.horizontal.width;b.container.height-=b.thumbs.vertical.height;return b},getThumbCss:function(a,b){return f.extend({position:"absolute","background-color":"black",width:a.width+"px",height:a.height+"px",margin:a.top+"px 0 0 "+a.left+"px","-moz-border-radius":a.corner+"px","-webkit-border-radius":a.corner+"px","border-radius":a.corner+"px"},b||{})}});f.extend(f.easing,{cubicEaseOut:function(a,b,d,e){return(d+e)*((a=a/1-1)*a*a+1)+d}})})(window,
Math,jQuery);