diff --git a/bower.json b/bower.json index d21db9a..163dc60 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "optiscroll", - "version": "1.0.4", + "version": "1.0.5", "homepage": "https://github.com/wilsonfletcher/optiScroll", "authors": [ "Alberto Gasparin " diff --git a/dist/jquery.optiscroll.js b/dist/jquery.optiscroll.js index a15d8d6..b0a747b 100644 --- a/dist/jquery.optiscroll.js +++ b/dist/jquery.optiscroll.js @@ -1,5 +1,5 @@ /*! -* Optiscroll.js v1.0.4 +* Optiscroll.js v1.0.5 * https://github.com/wilsonfletcher/Optiscroll/ * by Alberto Gasparin * @@ -87,17 +87,20 @@ Optiscroll.Instance.prototype = { var me = this, settings = me.settings; - // add instance to global array for timed check - if(settings.autoUpdate) { - G.instances.push( me ); - } - // initialize scrollbars me.scrollbars = { v: new Scrollbar('v', me), h: new Scrollbar('h', me) }; + // Stop initialization if old IE + if(!document.addEventListener) { return; } + + // add instance to global array for timed check + if(settings.autoUpdate) { + G.instances.push( me ); + } + // disable forced scrollbars if Firefox // because we cannot hide native scrollbars yet if(G.nativeScrollbarSize === 0 && 'mozRequestAnimationFrame' in window) { @@ -184,7 +187,7 @@ Optiscroll.Instance.prototype = { if( oldcH !== undefined ) { // if the element is no more in the DOM - if(sH === 0 && cH === 0 && !document.body.contains(me.element)) { + if(sH === 0 && cH === 0 && !Utils.containsNode(document.body, me.element)) { me.destroy(); return false; } @@ -711,6 +714,13 @@ var Utils = { }, + containsNode: function (parent, node) { + return parent.contains ? + parent != node && parent.contains(node) : + !!(parent.compareDocumentPosition(node) & 16); + }, + + // Global height checker // looped to listen element changes checkLoop: function () { @@ -745,7 +755,7 @@ var Utils = { var G = Optiscroll.G = { isTouch: 'ontouchstart' in window, cssTransition: cssTest('transition'), - cssTransform: cssTest('transform'), + cssTransform: cssTest('transform') || '', nativeScrollbarSize: getScrollbarWidth(), instances: [], @@ -773,7 +783,7 @@ function getScrollbarWidth () { outerEl, innerEl, width = 0; outerEl = document.createElement('div'); - outerEl.style.cssText = 'overflow:auto;width:50px;height:50px;' + 'position:absolute;left:-100px'; + outerEl.style.cssText = 'overflow:scroll;width:50px;height:50px;' + 'position:absolute;left:-100px'; innerEl = document.createElement('div'); innerEl.style.cssText = 'width:100px;height:100px'; diff --git a/dist/jquery.optiscroll.min.js b/dist/jquery.optiscroll.min.js index e882340..006e3cb 100644 --- a/dist/jquery.optiscroll.min.js +++ b/dist/jquery.optiscroll.min.js @@ -1,9 +1,9 @@ /*! -* Optiscroll.js v1.0.4 +* Optiscroll.js v1.0.5 * https://github.com/wilsonfletcher/Optiscroll/ * by Alberto Gasparin * * @copyright 2014 Wilson Fletcher * @license Released under MIT LICENSE */ -!function(a,b,c,d){"use strict";function e(){var a,c,d=b.documentElement,e=0;return a=b.createElement("div"),a.style.cssText="overflow:auto;width:50px;height:50px;position:absolute;left:-100px",c=b.createElement("div"),c.style.cssText="width:100px;height:100px",a.appendChild(c),d.appendChild(a),e=a.offsetWidth-a.clientWidth,d.removeChild(a),e}function f(a){var c=a.charAt(0).toUpperCase()+a.slice(1),e=b.createElement("test"),f=(a+" "+["Webkit","Moz","O","ms"].join(c+" ")+c).split(" ");for(var g in f)if(e.style[f[g]]!==d)return f[g];return!1}function g(a,b,c){var d=a.className.split(/\s+/),e=d.indexOf(b);c?~e||d.push(b):~e&&d.splice(e,1),a.className=d.join(" ")}function h(a,b,c){for(var e in b)!b.hasOwnProperty(e)||a[e]!==d&&c||(a[e]=b[e]);return a}function i(a,b,c){var d,e;if(a.length)for(d=0,e=a.length;e>d;d++)a[d][b].apply(a[d],c);else for(d in a)a[d][b].apply(a[d],c)}function j(a,b){var c,d;return function(){var e=this,f=Date.now(),g=arguments;c&&c+b>f?(clearTimeout(d),d=setTimeout(function(){c=f,a.apply(e,g)},b)):(c=f,a.apply(e,g))}}"function"==typeof a.CustomEvent||function(a){function c(a,c){c=c||{bubbles:!1,cancelable:!1,detail:d};var e=b.createEvent("CustomEvent");return e.initCustomEvent(a,c.bubbles,c.cancelable,c.detail),e}c.prototype=a.Event.prototype,a.CustomEvent=c}(a);var k=function r(a,b){return new r.Instance(a,b||{})},l=k.globalSettings={scrollMinUpdateInterval:25,checkFrequency:1e3,pauseCheck:!1};k.defaults={preventParentScroll:!1,forceScrollbars:!1,scrollStopDelay:300,maxTrackSize:95,minTrackSize:5,draggableTracks:!0,autoUpdate:!0,classPrefix:"optiscroll"},k.Instance=function(a,b){var c=this;c.element=a,c.scrollEl=a.children[0],c.settings=h(h({},k.defaults),b||{}),c.cache={},c.init()},k.Instance.prototype={init:function(){var b=this,c=b.settings;c.autoUpdate&&p.instances.push(b),b.scrollbars={v:new n("v",b),h:new n("h",b)},0===p.nativeScrollbarSize&&"mozRequestAnimationFrame"in a&&(c.forceScrollbars=!1),(p.nativeScrollbarSize||c.forceScrollbars)&&(o.hideNativeScrollbars(b.scrollEl),i(b.scrollbars,"create")),p.isTouch&&c.preventParentScroll&&g(b.element,c.classPrefix+"-prevent",!0),b.update(),b.bind(),c.autoUpdate&&!p.checkTimer&&o.checkLoop()},bind:function(){var a=this,b=a.listeners={},c=a.scrollEl;b.scroll=j(function(b){m.scroll(b,a)},l.scrollMinUpdateInterval),b.overflow=b.underflow=b.overflowchanged=function(){a.update()},p.isTouch&&(b.touchstart=function(b){m.touchstart(b,a)},b.touchend=function(b){m.touchend(b,a)}),a.settings.preventParentScroll&&(b.mousewheel=b.wheel=function(b){m.wheel(b,a)});for(var d in b)c.addEventListener(d,b[d])},update:function(){var a=this,c=a.cache.clientH,e=a.scrollEl,f=a.cache,g=e.scrollHeight,h=e.clientHeight,j=e.scrollWidth,k=e.clientWidth;if(g!==f.scrollH||h!==f.clientH||j!==f.scrollW||k!==f.clientW){if(f.scrollH=g,f.clientH=h,f.scrollW=j,f.clientW=k,c!==d){if(0===g&&0===h&&!b.body.contains(a.element))return a.destroy(),!1;a.fireCustomEvent("sizechange")}i(a.scrollbars,"update")}},scrollTo:function(a,b,c){var d,e,f,g,h=this,i=h.cache;p.pauseCheck=!0,h.update(),d=h.scrollEl.scrollLeft,e=h.scrollEl.scrollTop,f=+a,"left"===a&&(f=0),"right"===a&&(f=i.scrollW-i.clientW),a===!1&&(f=d),g=+b,"top"===b&&(g=0),"bottom"===b&&(g=i.scrollH-i.clientH),b===!1&&(g=e),h.animateScroll(d,f,e,g,+c)},scrollIntoView:function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n,o,q=this,r=q.scrollEl;p.pauseCheck=!0,q.update(),"string"==typeof a?a=r.querySelector(a):a.length&&a.jquery&&(a=a[0]),"number"==typeof c&&(c={top:c,right:c,bottom:c,left:c}),c=c||{},d=a.getBoundingClientRect(),e=r.getBoundingClientRect(),l=n=r.scrollLeft,m=o=r.scrollTop,j=l+d.left-e.left,k=m+d.top-e.top,f=j-(c.left||0),g=k-(c.top||0),h=j+d.width-q.cache.clientW+(c.right||0),i=k+d.height-q.cache.clientH+(c.bottom||0),l>f&&(n=f),h>l&&(n=h),m>g&&(o=g),i>m&&(o=i),q.animateScroll(l,n,m,o,+b)},animateScroll:function(a,b,d,e,f){var g=this,h=g.scrollEl,i=Date.now();if(b!==a||e!==d){if(0===f)return h.scrollLeft=b,void(h.scrollTop=e);isNaN(f)&&(f=15*c.pow(c.max(c.abs(b-a),c.abs(e-d)),.54));var j=function(){var g=c.min(1,(Date.now()-i)/f),k=o.easingFunction(g);e!==d&&(h.scrollTop=~~(k*(e-d))+d),b!==a&&(h.scrollLeft=~~(k*(b-a))+a),1>g&&q(j)};q(j)}},destroy:function(){var a=this,b=a.scrollEl,c=a.listeners,d=p.instances.indexOf(a);for(var e in c)b.removeEventListener(e,c[e]);i(a.scrollbars,"remove"),b.removeAttribute("style"),b.removeAttribute("data-scroll"),g(a.element,a.settings.classPrefix+"-prevent",!1),d>-1&&q(function(){p.instances.splice(d,1)})},fireCustomEvent:function(a){var b,c=this,d=c.cache,e=d.scrollH,f=d.scrollW;b={scrollbarV:h({},d.v),scrollbarH:h({},d.h),scrollTop:d.v.position*e,scrollLeft:d.h.position*f,scrollBottom:(1-d.v.position-d.v.size)*e,scrollRight:(1-d.h.position-d.h.size)*f,scrollWidth:f,scrollHeight:e,clientWidth:d.clientW,clientHeight:d.clientH},c.element.dispatchEvent(new CustomEvent(a,{detail:b}))}};var m={scroll:function(a,b){p.pauseCheck||b.fireCustomEvent("scrollstart"),p.pauseCheck=!0,b.scrollbars.v.update(),b.scrollbars.h.update(),b.fireCustomEvent("scroll"),clearTimeout(b.cache.timerStop),b.cache.timerStop=setTimeout(function(){m.scrollStop(b)},b.settings.scrollStopDelay)},touchstart:function(a,b){p.pauseCheck=!1,b.scrollbars.v.update(),b.scrollbars.h.update(),b.settings.preventParentScroll&&m.wheel(a,b)},touchend:function(a,b){clearTimeout(b.cache.timerStop)},scrollStop:function(a){a.fireCustomEvent("scrollstop"),p.pauseCheck=!1},wheel:function(a,b){var c=b.cache,d=c.v,e=c.h;d.enabled&&d.percent%100===0&&(b.scrollEl.scrollTop=d.percent?c.scrollH-c.clientH-1:1),e.enabled&&e.percent%100===0&&(b.scrollEl.scrollLeft=e.percent?c.scrollW-c.clientW-1:1)}},n=function(a,d){var e="v"===a,f=d.element,i=d.scrollEl,j=d.settings,k=d.cache,l=k[a]={},m=e?"H":"W",n="client"+m,o="scroll"+m,q=e?"scrollTop":"scrollLeft",r=e?["top","bottom"]:["left","right"],s="height 0.2s ease 0s, width 0.2s ease 0s, opacity 0.2s ease 0s",t=!1,u=null,v=null,w=!1,x={dragData:null,dragStart:function(a){var b=a.touches?a.touches[0]:a;x.dragData={x:b.pageX,y:b.pageY,scroll:i[q]}},dragMove:function(a){var b,c,d=a.touches?a.touches[0]:a;x.dragData&&(a.preventDefault(),b=e?d.pageY-x.dragData.y:d.pageX-x.dragData.x,c=b/k[n],i[q]=x.dragData.scroll+c*k[o])},dragEnd:function(){x.dragData=null}};return{toggle:function(b){t=b,v&&(g(f,a+"track-on",t),t&&(v.style[p.cssTransition]=s)),l.enabled=t},create:function(){u=b.createElement("div"),v=b.createElement("b"),u.className=j.classPrefix+"-"+a,v.className=j.classPrefix+"-"+a+"track",u.appendChild(v),f.appendChild(u),j.draggableTracks&&this.bind(!0)},update:function(){var a,b,d,e,f,g=this;(t||k[n]!==k[o])&&(d=this.calc(),a=d.size,b=l.size,e=1/a*d.position*100,f=c.abs(d.position-(l.position||0))*k[n],1===a&&t&&g.toggle(!1),1>a&&!t&&g.toggle(!0),v&&t&&g.style(e,f,a,b),l=h(l,d),t&&g.fireEdgeEv())},style:function(a,b,c,d){var f=this;c!==d&&(v.style[e?"height":"width"]=100*c+"%"),b&&(f.animateTrack(p.isTouch&&b>20),v.style[p.cssTransform]="translate("+(e?"0%,"+a+"%":a+"%,0%")+")")},animateTrack:function(a){(a||w)&&(v.style[p.cssTransition]=s+(a?", "+p.cssTransformDashed+" 0.2s linear 0s":"")),w=a},bind:function(a){var c=(a?"add":"remove")+"EventListener",d=p.isTouch?["touchstart","touchmove","touchend"]:["mousedown","mousemove","mouseup"];v&&v[c](d[0],x.dragStart),b[c](d[1],x.dragMove),b[c](d[2],x.dragEnd)},calc:function(){var a,b,d=i[q],e=k[n],f=k[o],g=e/f,h=f-e;return 1===g||0===f?{position:0,size:1,percent:0}:(b=100*d/h,1>=d&&(b=0),d>=h-1&&(b=100),g=c.max(g,j.minTrackSize/100),g=c.min(g,j.maxTrackSize/100),a=(1-g)*(b/100),{position:a,size:g,percent:b})},fireEdgeEv:function(){var a=l.percent;l.was!==a&&a%100===0&&(d.fireCustomEvent("scrollreachedge"),d.fireCustomEvent("scrollreach"+r[a/100])),l.was=a},remove:function(){this.toggle(!1),this.bind(!1),u&&u.parentNode&&u.parentNode.removeChild(u)}}},o={hideNativeScrollbars:function(a){var b=p.nativeScrollbarSize,c=a.style;if(0===b){var d=Date.now();a.setAttribute("data-scroll",d),c.display="none",p.isTouch?o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"display: none;"):o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"width: 0; height: 0;"),q(function(){c.display="block"})}else c.overflow="scroll",c.right=-b+"px",c.bottom=-b+"px"},addCssRule:function(a,c){var d=b.getElementById("scroll-sheet");d||(d=b.createElement("style"),d.id="scroll-sheet",b.head.appendChild(d)),d.innerHTML+=a+"{"+c+"} "},checkLoop:function(){return p.instances.length?(p.pauseCheck||i(p.instances,"update"),void(l.checkFrequency&&(p.checkTimer=setTimeout(function(){o.checkLoop()},l.checkFrequency)))):void(p.checkTimer=null)},easingFunction:function(a){return--a*a*a+1}},p=k.G={isTouch:"ontouchstart"in a,cssTransition:f("transition"),cssTransform:f("transform"),nativeScrollbarSize:e(),instances:[],checkTimer:null,pauseCheck:!1};p.cssTransformDashed="transform"===p.cssTransform?p.cssTransform:"-"+p.cssTransform.replace("T","-t").toLowerCase();var q=function(){return a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.msRequestAnimationFrame||function(b){a.setTimeout(b,1e3/60)}}();"function"==typeof define&&define.amd&&define(function(){return k}),"undefined"!=typeof module&&module.exports&&(module.exports=k),a.Optiscroll=k}(window,document,Math),function(a){var b="optiscroll";a.fn[b]=function(c){var d,e;return"string"==typeof c&&(e=Array.prototype.slice.call(arguments),d=e.shift()),this.each(function(){var f=a(this),g=f.data(b);g?g&&"string"==typeof d&&(g[d].apply(g,e),"destroy"===d&&f.removeData(b)):(g=new window.Optiscroll(this,c||{}),f.data(b,g))})}}(jQuery||Zepto); \ No newline at end of file +!function(a,b,c,d){"use strict";function e(){var a,c,d=b.documentElement,e=0;return a=b.createElement("div"),a.style.cssText="overflow:scroll;width:50px;height:50px;position:absolute;left:-100px",c=b.createElement("div"),c.style.cssText="width:100px;height:100px",a.appendChild(c),d.appendChild(a),e=a.offsetWidth-a.clientWidth,d.removeChild(a),e}function f(a){var c=a.charAt(0).toUpperCase()+a.slice(1),e=b.createElement("test"),f=(a+" "+["Webkit","Moz","O","ms"].join(c+" ")+c).split(" ");for(var g in f)if(e.style[f[g]]!==d)return f[g];return!1}function g(a,b,c){var d=a.className.split(/\s+/),e=d.indexOf(b);c?~e||d.push(b):~e&&d.splice(e,1),a.className=d.join(" ")}function h(a,b,c){for(var e in b)!b.hasOwnProperty(e)||a[e]!==d&&c||(a[e]=b[e]);return a}function i(a,b,c){var d,e;if(a.length)for(d=0,e=a.length;e>d;d++)a[d][b].apply(a[d],c);else for(d in a)a[d][b].apply(a[d],c)}function j(a,b){var c,d;return function(){var e=this,f=Date.now(),g=arguments;c&&c+b>f?(clearTimeout(d),d=setTimeout(function(){c=f,a.apply(e,g)},b)):(c=f,a.apply(e,g))}}"function"==typeof a.CustomEvent||function(a){function c(a,c){c=c||{bubbles:!1,cancelable:!1,detail:d};var e=b.createEvent("CustomEvent");return e.initCustomEvent(a,c.bubbles,c.cancelable,c.detail),e}c.prototype=a.Event.prototype,a.CustomEvent=c}(a);var k=function r(a,b){return new r.Instance(a,b||{})},l=k.globalSettings={scrollMinUpdateInterval:25,checkFrequency:1e3,pauseCheck:!1};k.defaults={preventParentScroll:!1,forceScrollbars:!1,scrollStopDelay:300,maxTrackSize:95,minTrackSize:5,draggableTracks:!0,autoUpdate:!0,classPrefix:"optiscroll"},k.Instance=function(a,b){var c=this;c.element=a,c.scrollEl=a.children[0],c.settings=h(h({},k.defaults),b||{}),c.cache={},c.init()},k.Instance.prototype={init:function(){var c=this,d=c.settings;c.scrollbars={v:new n("v",c),h:new n("h",c)},b.addEventListener&&(d.autoUpdate&&p.instances.push(c),0===p.nativeScrollbarSize&&"mozRequestAnimationFrame"in a&&(d.forceScrollbars=!1),(p.nativeScrollbarSize||d.forceScrollbars)&&(o.hideNativeScrollbars(c.scrollEl),i(c.scrollbars,"create")),p.isTouch&&d.preventParentScroll&&g(c.element,d.classPrefix+"-prevent",!0),c.update(),c.bind(),d.autoUpdate&&!p.checkTimer&&o.checkLoop())},bind:function(){var a=this,b=a.listeners={},c=a.scrollEl;b.scroll=j(function(b){m.scroll(b,a)},l.scrollMinUpdateInterval),b.overflow=b.underflow=b.overflowchanged=function(){a.update()},p.isTouch&&(b.touchstart=function(b){m.touchstart(b,a)},b.touchend=function(b){m.touchend(b,a)}),a.settings.preventParentScroll&&(b.mousewheel=b.wheel=function(b){m.wheel(b,a)});for(var d in b)c.addEventListener(d,b[d])},update:function(){var a=this,c=a.cache.clientH,e=a.scrollEl,f=a.cache,g=e.scrollHeight,h=e.clientHeight,j=e.scrollWidth,k=e.clientWidth;if(g!==f.scrollH||h!==f.clientH||j!==f.scrollW||k!==f.clientW){if(f.scrollH=g,f.clientH=h,f.scrollW=j,f.clientW=k,c!==d){if(0===g&&0===h&&!o.containsNode(b.body,a.element))return a.destroy(),!1;a.fireCustomEvent("sizechange")}i(a.scrollbars,"update")}},scrollTo:function(a,b,c){var d,e,f,g,h=this,i=h.cache;p.pauseCheck=!0,h.update(),d=h.scrollEl.scrollLeft,e=h.scrollEl.scrollTop,f=+a,"left"===a&&(f=0),"right"===a&&(f=i.scrollW-i.clientW),a===!1&&(f=d),g=+b,"top"===b&&(g=0),"bottom"===b&&(g=i.scrollH-i.clientH),b===!1&&(g=e),h.animateScroll(d,f,e,g,+c)},scrollIntoView:function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n,o,q=this,r=q.scrollEl;p.pauseCheck=!0,q.update(),"string"==typeof a?a=r.querySelector(a):a.length&&a.jquery&&(a=a[0]),"number"==typeof c&&(c={top:c,right:c,bottom:c,left:c}),c=c||{},d=a.getBoundingClientRect(),e=r.getBoundingClientRect(),l=n=r.scrollLeft,m=o=r.scrollTop,j=l+d.left-e.left,k=m+d.top-e.top,f=j-(c.left||0),g=k-(c.top||0),h=j+d.width-q.cache.clientW+(c.right||0),i=k+d.height-q.cache.clientH+(c.bottom||0),l>f&&(n=f),h>l&&(n=h),m>g&&(o=g),i>m&&(o=i),q.animateScroll(l,n,m,o,+b)},animateScroll:function(a,b,d,e,f){var g=this,h=g.scrollEl,i=Date.now();if(b!==a||e!==d){if(0===f)return h.scrollLeft=b,void(h.scrollTop=e);isNaN(f)&&(f=15*c.pow(c.max(c.abs(b-a),c.abs(e-d)),.54));var j=function(){var g=c.min(1,(Date.now()-i)/f),k=o.easingFunction(g);e!==d&&(h.scrollTop=~~(k*(e-d))+d),b!==a&&(h.scrollLeft=~~(k*(b-a))+a),1>g&&q(j)};q(j)}},destroy:function(){var a=this,b=a.scrollEl,c=a.listeners,d=p.instances.indexOf(a);for(var e in c)b.removeEventListener(e,c[e]);i(a.scrollbars,"remove"),b.removeAttribute("style"),b.removeAttribute("data-scroll"),g(a.element,a.settings.classPrefix+"-prevent",!1),d>-1&&q(function(){p.instances.splice(d,1)})},fireCustomEvent:function(a){var b,c=this,d=c.cache,e=d.scrollH,f=d.scrollW;b={scrollbarV:h({},d.v),scrollbarH:h({},d.h),scrollTop:d.v.position*e,scrollLeft:d.h.position*f,scrollBottom:(1-d.v.position-d.v.size)*e,scrollRight:(1-d.h.position-d.h.size)*f,scrollWidth:f,scrollHeight:e,clientWidth:d.clientW,clientHeight:d.clientH},c.element.dispatchEvent(new CustomEvent(a,{detail:b}))}};var m={scroll:function(a,b){p.pauseCheck||b.fireCustomEvent("scrollstart"),p.pauseCheck=!0,b.scrollbars.v.update(),b.scrollbars.h.update(),b.fireCustomEvent("scroll"),clearTimeout(b.cache.timerStop),b.cache.timerStop=setTimeout(function(){m.scrollStop(b)},b.settings.scrollStopDelay)},touchstart:function(a,b){p.pauseCheck=!1,b.scrollbars.v.update(),b.scrollbars.h.update(),b.settings.preventParentScroll&&m.wheel(a,b)},touchend:function(a,b){clearTimeout(b.cache.timerStop)},scrollStop:function(a){a.fireCustomEvent("scrollstop"),p.pauseCheck=!1},wheel:function(a,b){var c=b.cache,d=c.v,e=c.h;d.enabled&&d.percent%100===0&&(b.scrollEl.scrollTop=d.percent?c.scrollH-c.clientH-1:1),e.enabled&&e.percent%100===0&&(b.scrollEl.scrollLeft=e.percent?c.scrollW-c.clientW-1:1)}},n=function(a,d){var e="v"===a,f=d.element,i=d.scrollEl,j=d.settings,k=d.cache,l=k[a]={},m=e?"H":"W",n="client"+m,o="scroll"+m,q=e?"scrollTop":"scrollLeft",r=e?["top","bottom"]:["left","right"],s="height 0.2s ease 0s, width 0.2s ease 0s, opacity 0.2s ease 0s",t=!1,u=null,v=null,w=!1,x={dragData:null,dragStart:function(a){var b=a.touches?a.touches[0]:a;x.dragData={x:b.pageX,y:b.pageY,scroll:i[q]}},dragMove:function(a){var b,c,d=a.touches?a.touches[0]:a;x.dragData&&(a.preventDefault(),b=e?d.pageY-x.dragData.y:d.pageX-x.dragData.x,c=b/k[n],i[q]=x.dragData.scroll+c*k[o])},dragEnd:function(){x.dragData=null}};return{toggle:function(b){t=b,v&&(g(f,a+"track-on",t),t&&(v.style[p.cssTransition]=s)),l.enabled=t},create:function(){u=b.createElement("div"),v=b.createElement("b"),u.className=j.classPrefix+"-"+a,v.className=j.classPrefix+"-"+a+"track",u.appendChild(v),f.appendChild(u),j.draggableTracks&&this.bind(!0)},update:function(){var a,b,d,e,f,g=this;(t||k[n]!==k[o])&&(d=this.calc(),a=d.size,b=l.size,e=1/a*d.position*100,f=c.abs(d.position-(l.position||0))*k[n],1===a&&t&&g.toggle(!1),1>a&&!t&&g.toggle(!0),v&&t&&g.style(e,f,a,b),l=h(l,d),t&&g.fireEdgeEv())},style:function(a,b,c,d){var f=this;c!==d&&(v.style[e?"height":"width"]=100*c+"%"),b&&(f.animateTrack(p.isTouch&&b>20),v.style[p.cssTransform]="translate("+(e?"0%,"+a+"%":a+"%,0%")+")")},animateTrack:function(a){(a||w)&&(v.style[p.cssTransition]=s+(a?", "+p.cssTransformDashed+" 0.2s linear 0s":"")),w=a},bind:function(a){var c=(a?"add":"remove")+"EventListener",d=p.isTouch?["touchstart","touchmove","touchend"]:["mousedown","mousemove","mouseup"];v&&v[c](d[0],x.dragStart),b[c](d[1],x.dragMove),b[c](d[2],x.dragEnd)},calc:function(){var a,b,d=i[q],e=k[n],f=k[o],g=e/f,h=f-e;return 1===g||0===f?{position:0,size:1,percent:0}:(b=100*d/h,1>=d&&(b=0),d>=h-1&&(b=100),g=c.max(g,j.minTrackSize/100),g=c.min(g,j.maxTrackSize/100),a=(1-g)*(b/100),{position:a,size:g,percent:b})},fireEdgeEv:function(){var a=l.percent;l.was!==a&&a%100===0&&(d.fireCustomEvent("scrollreachedge"),d.fireCustomEvent("scrollreach"+r[a/100])),l.was=a},remove:function(){this.toggle(!1),this.bind(!1),u&&u.parentNode&&u.parentNode.removeChild(u)}}},o={hideNativeScrollbars:function(a){var b=p.nativeScrollbarSize,c=a.style;if(0===b){var d=Date.now();a.setAttribute("data-scroll",d),c.display="none",p.isTouch?o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"display: none;"):o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"width: 0; height: 0;"),q(function(){c.display="block"})}else c.overflow="scroll",c.right=-b+"px",c.bottom=-b+"px"},addCssRule:function(a,c){var d=b.getElementById("scroll-sheet");d||(d=b.createElement("style"),d.id="scroll-sheet",b.head.appendChild(d)),d.innerHTML+=a+"{"+c+"} "},containsNode:function(a,b){return a.contains?a!=b&&a.contains(b):!!(16&a.compareDocumentPosition(b))},checkLoop:function(){return p.instances.length?(p.pauseCheck||i(p.instances,"update"),void(l.checkFrequency&&(p.checkTimer=setTimeout(function(){o.checkLoop()},l.checkFrequency)))):void(p.checkTimer=null)},easingFunction:function(a){return--a*a*a+1}},p=k.G={isTouch:"ontouchstart"in a,cssTransition:f("transition"),cssTransform:f("transform")||"",nativeScrollbarSize:e(),instances:[],checkTimer:null,pauseCheck:!1};p.cssTransformDashed="transform"===p.cssTransform?p.cssTransform:"-"+p.cssTransform.replace("T","-t").toLowerCase();var q=function(){return a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.msRequestAnimationFrame||function(b){a.setTimeout(b,1e3/60)}}();"function"==typeof define&&define.amd&&define(function(){return k}),"undefined"!=typeof module&&module.exports&&(module.exports=k),a.Optiscroll=k}(window,document,Math),function(a){var b="optiscroll";a.fn[b]=function(c){var d,e;return"string"==typeof c&&(e=Array.prototype.slice.call(arguments),d=e.shift()),this.each(function(){var f=a(this),g=f.data(b);g?g&&"string"==typeof d&&(g[d].apply(g,e),"destroy"===d&&f.removeData(b)):(g=new window.Optiscroll(this,c||{}),f.data(b,g))})}}(jQuery||Zepto); \ No newline at end of file diff --git a/dist/optiscroll.js b/dist/optiscroll.js index 676f316..ca790dd 100644 --- a/dist/optiscroll.js +++ b/dist/optiscroll.js @@ -1,5 +1,5 @@ /*! -* Optiscroll.js v1.0.4 +* Optiscroll.js v1.0.5 * https://github.com/wilsonfletcher/Optiscroll/ * by Alberto Gasparin * @@ -87,17 +87,20 @@ Optiscroll.Instance.prototype = { var me = this, settings = me.settings; - // add instance to global array for timed check - if(settings.autoUpdate) { - G.instances.push( me ); - } - // initialize scrollbars me.scrollbars = { v: new Scrollbar('v', me), h: new Scrollbar('h', me) }; + // Stop initialization if old IE + if(!document.addEventListener) { return; } + + // add instance to global array for timed check + if(settings.autoUpdate) { + G.instances.push( me ); + } + // disable forced scrollbars if Firefox // because we cannot hide native scrollbars yet if(G.nativeScrollbarSize === 0 && 'mozRequestAnimationFrame' in window) { @@ -184,7 +187,7 @@ Optiscroll.Instance.prototype = { if( oldcH !== undefined ) { // if the element is no more in the DOM - if(sH === 0 && cH === 0 && !document.body.contains(me.element)) { + if(sH === 0 && cH === 0 && !Utils.containsNode(document.body, me.element)) { me.destroy(); return false; } @@ -711,6 +714,13 @@ var Utils = { }, + containsNode: function (parent, node) { + return parent.contains ? + parent != node && parent.contains(node) : + !!(parent.compareDocumentPosition(node) & 16); + }, + + // Global height checker // looped to listen element changes checkLoop: function () { @@ -745,7 +755,7 @@ var Utils = { var G = Optiscroll.G = { isTouch: 'ontouchstart' in window, cssTransition: cssTest('transition'), - cssTransform: cssTest('transform'), + cssTransform: cssTest('transform') || '', nativeScrollbarSize: getScrollbarWidth(), instances: [], @@ -773,7 +783,7 @@ function getScrollbarWidth () { outerEl, innerEl, width = 0; outerEl = document.createElement('div'); - outerEl.style.cssText = 'overflow:auto;width:50px;height:50px;' + 'position:absolute;left:-100px'; + outerEl.style.cssText = 'overflow:scroll;width:50px;height:50px;' + 'position:absolute;left:-100px'; innerEl = document.createElement('div'); innerEl.style.cssText = 'width:100px;height:100px'; diff --git a/dist/optiscroll.min.js b/dist/optiscroll.min.js index 49614ef..b96fc4f 100644 --- a/dist/optiscroll.min.js +++ b/dist/optiscroll.min.js @@ -1,9 +1,9 @@ /*! -* Optiscroll.js v1.0.4 +* Optiscroll.js v1.0.5 * https://github.com/wilsonfletcher/Optiscroll/ * by Alberto Gasparin * * @copyright 2014 Wilson Fletcher * @license Released under MIT LICENSE */ -!function(a,b,c,d){"use strict";function e(){var a,c,d=b.documentElement,e=0;return a=b.createElement("div"),a.style.cssText="overflow:auto;width:50px;height:50px;position:absolute;left:-100px",c=b.createElement("div"),c.style.cssText="width:100px;height:100px",a.appendChild(c),d.appendChild(a),e=a.offsetWidth-a.clientWidth,d.removeChild(a),e}function f(a){var c=a.charAt(0).toUpperCase()+a.slice(1),e=b.createElement("test"),f=(a+" "+["Webkit","Moz","O","ms"].join(c+" ")+c).split(" ");for(var g in f)if(e.style[f[g]]!==d)return f[g];return!1}function g(a,b,c){var d=a.className.split(/\s+/),e=d.indexOf(b);c?~e||d.push(b):~e&&d.splice(e,1),a.className=d.join(" ")}function h(a,b,c){for(var e in b)!b.hasOwnProperty(e)||a[e]!==d&&c||(a[e]=b[e]);return a}function i(a,b,c){var d,e;if(a.length)for(d=0,e=a.length;e>d;d++)a[d][b].apply(a[d],c);else for(d in a)a[d][b].apply(a[d],c)}function j(a,b){var c,d;return function(){var e=this,f=Date.now(),g=arguments;c&&c+b>f?(clearTimeout(d),d=setTimeout(function(){c=f,a.apply(e,g)},b)):(c=f,a.apply(e,g))}}"function"==typeof a.CustomEvent||function(a){function c(a,c){c=c||{bubbles:!1,cancelable:!1,detail:d};var e=b.createEvent("CustomEvent");return e.initCustomEvent(a,c.bubbles,c.cancelable,c.detail),e}c.prototype=a.Event.prototype,a.CustomEvent=c}(a);var k=function r(a,b){return new r.Instance(a,b||{})},l=k.globalSettings={scrollMinUpdateInterval:25,checkFrequency:1e3,pauseCheck:!1};k.defaults={preventParentScroll:!1,forceScrollbars:!1,scrollStopDelay:300,maxTrackSize:95,minTrackSize:5,draggableTracks:!0,autoUpdate:!0,classPrefix:"optiscroll"},k.Instance=function(a,b){var c=this;c.element=a,c.scrollEl=a.children[0],c.settings=h(h({},k.defaults),b||{}),c.cache={},c.init()},k.Instance.prototype={init:function(){var b=this,c=b.settings;c.autoUpdate&&p.instances.push(b),b.scrollbars={v:new n("v",b),h:new n("h",b)},0===p.nativeScrollbarSize&&"mozRequestAnimationFrame"in a&&(c.forceScrollbars=!1),(p.nativeScrollbarSize||c.forceScrollbars)&&(o.hideNativeScrollbars(b.scrollEl),i(b.scrollbars,"create")),p.isTouch&&c.preventParentScroll&&g(b.element,c.classPrefix+"-prevent",!0),b.update(),b.bind(),c.autoUpdate&&!p.checkTimer&&o.checkLoop()},bind:function(){var a=this,b=a.listeners={},c=a.scrollEl;b.scroll=j(function(b){m.scroll(b,a)},l.scrollMinUpdateInterval),b.overflow=b.underflow=b.overflowchanged=function(){a.update()},p.isTouch&&(b.touchstart=function(b){m.touchstart(b,a)},b.touchend=function(b){m.touchend(b,a)}),a.settings.preventParentScroll&&(b.mousewheel=b.wheel=function(b){m.wheel(b,a)});for(var d in b)c.addEventListener(d,b[d])},update:function(){var a=this,c=a.cache.clientH,e=a.scrollEl,f=a.cache,g=e.scrollHeight,h=e.clientHeight,j=e.scrollWidth,k=e.clientWidth;if(g!==f.scrollH||h!==f.clientH||j!==f.scrollW||k!==f.clientW){if(f.scrollH=g,f.clientH=h,f.scrollW=j,f.clientW=k,c!==d){if(0===g&&0===h&&!b.body.contains(a.element))return a.destroy(),!1;a.fireCustomEvent("sizechange")}i(a.scrollbars,"update")}},scrollTo:function(a,b,c){var d,e,f,g,h=this,i=h.cache;p.pauseCheck=!0,h.update(),d=h.scrollEl.scrollLeft,e=h.scrollEl.scrollTop,f=+a,"left"===a&&(f=0),"right"===a&&(f=i.scrollW-i.clientW),a===!1&&(f=d),g=+b,"top"===b&&(g=0),"bottom"===b&&(g=i.scrollH-i.clientH),b===!1&&(g=e),h.animateScroll(d,f,e,g,+c)},scrollIntoView:function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n,o,q=this,r=q.scrollEl;p.pauseCheck=!0,q.update(),"string"==typeof a?a=r.querySelector(a):a.length&&a.jquery&&(a=a[0]),"number"==typeof c&&(c={top:c,right:c,bottom:c,left:c}),c=c||{},d=a.getBoundingClientRect(),e=r.getBoundingClientRect(),l=n=r.scrollLeft,m=o=r.scrollTop,j=l+d.left-e.left,k=m+d.top-e.top,f=j-(c.left||0),g=k-(c.top||0),h=j+d.width-q.cache.clientW+(c.right||0),i=k+d.height-q.cache.clientH+(c.bottom||0),l>f&&(n=f),h>l&&(n=h),m>g&&(o=g),i>m&&(o=i),q.animateScroll(l,n,m,o,+b)},animateScroll:function(a,b,d,e,f){var g=this,h=g.scrollEl,i=Date.now();if(b!==a||e!==d){if(0===f)return h.scrollLeft=b,void(h.scrollTop=e);isNaN(f)&&(f=15*c.pow(c.max(c.abs(b-a),c.abs(e-d)),.54));var j=function(){var g=c.min(1,(Date.now()-i)/f),k=o.easingFunction(g);e!==d&&(h.scrollTop=~~(k*(e-d))+d),b!==a&&(h.scrollLeft=~~(k*(b-a))+a),1>g&&q(j)};q(j)}},destroy:function(){var a=this,b=a.scrollEl,c=a.listeners,d=p.instances.indexOf(a);for(var e in c)b.removeEventListener(e,c[e]);i(a.scrollbars,"remove"),b.removeAttribute("style"),b.removeAttribute("data-scroll"),g(a.element,a.settings.classPrefix+"-prevent",!1),d>-1&&q(function(){p.instances.splice(d,1)})},fireCustomEvent:function(a){var b,c=this,d=c.cache,e=d.scrollH,f=d.scrollW;b={scrollbarV:h({},d.v),scrollbarH:h({},d.h),scrollTop:d.v.position*e,scrollLeft:d.h.position*f,scrollBottom:(1-d.v.position-d.v.size)*e,scrollRight:(1-d.h.position-d.h.size)*f,scrollWidth:f,scrollHeight:e,clientWidth:d.clientW,clientHeight:d.clientH},c.element.dispatchEvent(new CustomEvent(a,{detail:b}))}};var m={scroll:function(a,b){p.pauseCheck||b.fireCustomEvent("scrollstart"),p.pauseCheck=!0,b.scrollbars.v.update(),b.scrollbars.h.update(),b.fireCustomEvent("scroll"),clearTimeout(b.cache.timerStop),b.cache.timerStop=setTimeout(function(){m.scrollStop(b)},b.settings.scrollStopDelay)},touchstart:function(a,b){p.pauseCheck=!1,b.scrollbars.v.update(),b.scrollbars.h.update(),b.settings.preventParentScroll&&m.wheel(a,b)},touchend:function(a,b){clearTimeout(b.cache.timerStop)},scrollStop:function(a){a.fireCustomEvent("scrollstop"),p.pauseCheck=!1},wheel:function(a,b){var c=b.cache,d=c.v,e=c.h;d.enabled&&d.percent%100===0&&(b.scrollEl.scrollTop=d.percent?c.scrollH-c.clientH-1:1),e.enabled&&e.percent%100===0&&(b.scrollEl.scrollLeft=e.percent?c.scrollW-c.clientW-1:1)}},n=function(a,d){var e="v"===a,f=d.element,i=d.scrollEl,j=d.settings,k=d.cache,l=k[a]={},m=e?"H":"W",n="client"+m,o="scroll"+m,q=e?"scrollTop":"scrollLeft",r=e?["top","bottom"]:["left","right"],s="height 0.2s ease 0s, width 0.2s ease 0s, opacity 0.2s ease 0s",t=!1,u=null,v=null,w=!1,x={dragData:null,dragStart:function(a){var b=a.touches?a.touches[0]:a;x.dragData={x:b.pageX,y:b.pageY,scroll:i[q]}},dragMove:function(a){var b,c,d=a.touches?a.touches[0]:a;x.dragData&&(a.preventDefault(),b=e?d.pageY-x.dragData.y:d.pageX-x.dragData.x,c=b/k[n],i[q]=x.dragData.scroll+c*k[o])},dragEnd:function(){x.dragData=null}};return{toggle:function(b){t=b,v&&(g(f,a+"track-on",t),t&&(v.style[p.cssTransition]=s)),l.enabled=t},create:function(){u=b.createElement("div"),v=b.createElement("b"),u.className=j.classPrefix+"-"+a,v.className=j.classPrefix+"-"+a+"track",u.appendChild(v),f.appendChild(u),j.draggableTracks&&this.bind(!0)},update:function(){var a,b,d,e,f,g=this;(t||k[n]!==k[o])&&(d=this.calc(),a=d.size,b=l.size,e=1/a*d.position*100,f=c.abs(d.position-(l.position||0))*k[n],1===a&&t&&g.toggle(!1),1>a&&!t&&g.toggle(!0),v&&t&&g.style(e,f,a,b),l=h(l,d),t&&g.fireEdgeEv())},style:function(a,b,c,d){var f=this;c!==d&&(v.style[e?"height":"width"]=100*c+"%"),b&&(f.animateTrack(p.isTouch&&b>20),v.style[p.cssTransform]="translate("+(e?"0%,"+a+"%":a+"%,0%")+")")},animateTrack:function(a){(a||w)&&(v.style[p.cssTransition]=s+(a?", "+p.cssTransformDashed+" 0.2s linear 0s":"")),w=a},bind:function(a){var c=(a?"add":"remove")+"EventListener",d=p.isTouch?["touchstart","touchmove","touchend"]:["mousedown","mousemove","mouseup"];v&&v[c](d[0],x.dragStart),b[c](d[1],x.dragMove),b[c](d[2],x.dragEnd)},calc:function(){var a,b,d=i[q],e=k[n],f=k[o],g=e/f,h=f-e;return 1===g||0===f?{position:0,size:1,percent:0}:(b=100*d/h,1>=d&&(b=0),d>=h-1&&(b=100),g=c.max(g,j.minTrackSize/100),g=c.min(g,j.maxTrackSize/100),a=(1-g)*(b/100),{position:a,size:g,percent:b})},fireEdgeEv:function(){var a=l.percent;l.was!==a&&a%100===0&&(d.fireCustomEvent("scrollreachedge"),d.fireCustomEvent("scrollreach"+r[a/100])),l.was=a},remove:function(){this.toggle(!1),this.bind(!1),u&&u.parentNode&&u.parentNode.removeChild(u)}}},o={hideNativeScrollbars:function(a){var b=p.nativeScrollbarSize,c=a.style;if(0===b){var d=Date.now();a.setAttribute("data-scroll",d),c.display="none",p.isTouch?o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"display: none;"):o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"width: 0; height: 0;"),q(function(){c.display="block"})}else c.overflow="scroll",c.right=-b+"px",c.bottom=-b+"px"},addCssRule:function(a,c){var d=b.getElementById("scroll-sheet");d||(d=b.createElement("style"),d.id="scroll-sheet",b.head.appendChild(d)),d.innerHTML+=a+"{"+c+"} "},checkLoop:function(){return p.instances.length?(p.pauseCheck||i(p.instances,"update"),void(l.checkFrequency&&(p.checkTimer=setTimeout(function(){o.checkLoop()},l.checkFrequency)))):void(p.checkTimer=null)},easingFunction:function(a){return--a*a*a+1}},p=k.G={isTouch:"ontouchstart"in a,cssTransition:f("transition"),cssTransform:f("transform"),nativeScrollbarSize:e(),instances:[],checkTimer:null,pauseCheck:!1};p.cssTransformDashed="transform"===p.cssTransform?p.cssTransform:"-"+p.cssTransform.replace("T","-t").toLowerCase();var q=function(){return a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.msRequestAnimationFrame||function(b){a.setTimeout(b,1e3/60)}}();"function"==typeof define&&define.amd&&define(function(){return k}),"undefined"!=typeof module&&module.exports&&(module.exports=k),a.Optiscroll=k}(window,document,Math); \ No newline at end of file +!function(a,b,c,d){"use strict";function e(){var a,c,d=b.documentElement,e=0;return a=b.createElement("div"),a.style.cssText="overflow:scroll;width:50px;height:50px;position:absolute;left:-100px",c=b.createElement("div"),c.style.cssText="width:100px;height:100px",a.appendChild(c),d.appendChild(a),e=a.offsetWidth-a.clientWidth,d.removeChild(a),e}function f(a){var c=a.charAt(0).toUpperCase()+a.slice(1),e=b.createElement("test"),f=(a+" "+["Webkit","Moz","O","ms"].join(c+" ")+c).split(" ");for(var g in f)if(e.style[f[g]]!==d)return f[g];return!1}function g(a,b,c){var d=a.className.split(/\s+/),e=d.indexOf(b);c?~e||d.push(b):~e&&d.splice(e,1),a.className=d.join(" ")}function h(a,b,c){for(var e in b)!b.hasOwnProperty(e)||a[e]!==d&&c||(a[e]=b[e]);return a}function i(a,b,c){var d,e;if(a.length)for(d=0,e=a.length;e>d;d++)a[d][b].apply(a[d],c);else for(d in a)a[d][b].apply(a[d],c)}function j(a,b){var c,d;return function(){var e=this,f=Date.now(),g=arguments;c&&c+b>f?(clearTimeout(d),d=setTimeout(function(){c=f,a.apply(e,g)},b)):(c=f,a.apply(e,g))}}"function"==typeof a.CustomEvent||function(a){function c(a,c){c=c||{bubbles:!1,cancelable:!1,detail:d};var e=b.createEvent("CustomEvent");return e.initCustomEvent(a,c.bubbles,c.cancelable,c.detail),e}c.prototype=a.Event.prototype,a.CustomEvent=c}(a);var k=function r(a,b){return new r.Instance(a,b||{})},l=k.globalSettings={scrollMinUpdateInterval:25,checkFrequency:1e3,pauseCheck:!1};k.defaults={preventParentScroll:!1,forceScrollbars:!1,scrollStopDelay:300,maxTrackSize:95,minTrackSize:5,draggableTracks:!0,autoUpdate:!0,classPrefix:"optiscroll"},k.Instance=function(a,b){var c=this;c.element=a,c.scrollEl=a.children[0],c.settings=h(h({},k.defaults),b||{}),c.cache={},c.init()},k.Instance.prototype={init:function(){var c=this,d=c.settings;c.scrollbars={v:new n("v",c),h:new n("h",c)},b.addEventListener&&(d.autoUpdate&&p.instances.push(c),0===p.nativeScrollbarSize&&"mozRequestAnimationFrame"in a&&(d.forceScrollbars=!1),(p.nativeScrollbarSize||d.forceScrollbars)&&(o.hideNativeScrollbars(c.scrollEl),i(c.scrollbars,"create")),p.isTouch&&d.preventParentScroll&&g(c.element,d.classPrefix+"-prevent",!0),c.update(),c.bind(),d.autoUpdate&&!p.checkTimer&&o.checkLoop())},bind:function(){var a=this,b=a.listeners={},c=a.scrollEl;b.scroll=j(function(b){m.scroll(b,a)},l.scrollMinUpdateInterval),b.overflow=b.underflow=b.overflowchanged=function(){a.update()},p.isTouch&&(b.touchstart=function(b){m.touchstart(b,a)},b.touchend=function(b){m.touchend(b,a)}),a.settings.preventParentScroll&&(b.mousewheel=b.wheel=function(b){m.wheel(b,a)});for(var d in b)c.addEventListener(d,b[d])},update:function(){var a=this,c=a.cache.clientH,e=a.scrollEl,f=a.cache,g=e.scrollHeight,h=e.clientHeight,j=e.scrollWidth,k=e.clientWidth;if(g!==f.scrollH||h!==f.clientH||j!==f.scrollW||k!==f.clientW){if(f.scrollH=g,f.clientH=h,f.scrollW=j,f.clientW=k,c!==d){if(0===g&&0===h&&!o.containsNode(b.body,a.element))return a.destroy(),!1;a.fireCustomEvent("sizechange")}i(a.scrollbars,"update")}},scrollTo:function(a,b,c){var d,e,f,g,h=this,i=h.cache;p.pauseCheck=!0,h.update(),d=h.scrollEl.scrollLeft,e=h.scrollEl.scrollTop,f=+a,"left"===a&&(f=0),"right"===a&&(f=i.scrollW-i.clientW),a===!1&&(f=d),g=+b,"top"===b&&(g=0),"bottom"===b&&(g=i.scrollH-i.clientH),b===!1&&(g=e),h.animateScroll(d,f,e,g,+c)},scrollIntoView:function(a,b,c){var d,e,f,g,h,i,j,k,l,m,n,o,q=this,r=q.scrollEl;p.pauseCheck=!0,q.update(),"string"==typeof a?a=r.querySelector(a):a.length&&a.jquery&&(a=a[0]),"number"==typeof c&&(c={top:c,right:c,bottom:c,left:c}),c=c||{},d=a.getBoundingClientRect(),e=r.getBoundingClientRect(),l=n=r.scrollLeft,m=o=r.scrollTop,j=l+d.left-e.left,k=m+d.top-e.top,f=j-(c.left||0),g=k-(c.top||0),h=j+d.width-q.cache.clientW+(c.right||0),i=k+d.height-q.cache.clientH+(c.bottom||0),l>f&&(n=f),h>l&&(n=h),m>g&&(o=g),i>m&&(o=i),q.animateScroll(l,n,m,o,+b)},animateScroll:function(a,b,d,e,f){var g=this,h=g.scrollEl,i=Date.now();if(b!==a||e!==d){if(0===f)return h.scrollLeft=b,void(h.scrollTop=e);isNaN(f)&&(f=15*c.pow(c.max(c.abs(b-a),c.abs(e-d)),.54));var j=function(){var g=c.min(1,(Date.now()-i)/f),k=o.easingFunction(g);e!==d&&(h.scrollTop=~~(k*(e-d))+d),b!==a&&(h.scrollLeft=~~(k*(b-a))+a),1>g&&q(j)};q(j)}},destroy:function(){var a=this,b=a.scrollEl,c=a.listeners,d=p.instances.indexOf(a);for(var e in c)b.removeEventListener(e,c[e]);i(a.scrollbars,"remove"),b.removeAttribute("style"),b.removeAttribute("data-scroll"),g(a.element,a.settings.classPrefix+"-prevent",!1),d>-1&&q(function(){p.instances.splice(d,1)})},fireCustomEvent:function(a){var b,c=this,d=c.cache,e=d.scrollH,f=d.scrollW;b={scrollbarV:h({},d.v),scrollbarH:h({},d.h),scrollTop:d.v.position*e,scrollLeft:d.h.position*f,scrollBottom:(1-d.v.position-d.v.size)*e,scrollRight:(1-d.h.position-d.h.size)*f,scrollWidth:f,scrollHeight:e,clientWidth:d.clientW,clientHeight:d.clientH},c.element.dispatchEvent(new CustomEvent(a,{detail:b}))}};var m={scroll:function(a,b){p.pauseCheck||b.fireCustomEvent("scrollstart"),p.pauseCheck=!0,b.scrollbars.v.update(),b.scrollbars.h.update(),b.fireCustomEvent("scroll"),clearTimeout(b.cache.timerStop),b.cache.timerStop=setTimeout(function(){m.scrollStop(b)},b.settings.scrollStopDelay)},touchstart:function(a,b){p.pauseCheck=!1,b.scrollbars.v.update(),b.scrollbars.h.update(),b.settings.preventParentScroll&&m.wheel(a,b)},touchend:function(a,b){clearTimeout(b.cache.timerStop)},scrollStop:function(a){a.fireCustomEvent("scrollstop"),p.pauseCheck=!1},wheel:function(a,b){var c=b.cache,d=c.v,e=c.h;d.enabled&&d.percent%100===0&&(b.scrollEl.scrollTop=d.percent?c.scrollH-c.clientH-1:1),e.enabled&&e.percent%100===0&&(b.scrollEl.scrollLeft=e.percent?c.scrollW-c.clientW-1:1)}},n=function(a,d){var e="v"===a,f=d.element,i=d.scrollEl,j=d.settings,k=d.cache,l=k[a]={},m=e?"H":"W",n="client"+m,o="scroll"+m,q=e?"scrollTop":"scrollLeft",r=e?["top","bottom"]:["left","right"],s="height 0.2s ease 0s, width 0.2s ease 0s, opacity 0.2s ease 0s",t=!1,u=null,v=null,w=!1,x={dragData:null,dragStart:function(a){var b=a.touches?a.touches[0]:a;x.dragData={x:b.pageX,y:b.pageY,scroll:i[q]}},dragMove:function(a){var b,c,d=a.touches?a.touches[0]:a;x.dragData&&(a.preventDefault(),b=e?d.pageY-x.dragData.y:d.pageX-x.dragData.x,c=b/k[n],i[q]=x.dragData.scroll+c*k[o])},dragEnd:function(){x.dragData=null}};return{toggle:function(b){t=b,v&&(g(f,a+"track-on",t),t&&(v.style[p.cssTransition]=s)),l.enabled=t},create:function(){u=b.createElement("div"),v=b.createElement("b"),u.className=j.classPrefix+"-"+a,v.className=j.classPrefix+"-"+a+"track",u.appendChild(v),f.appendChild(u),j.draggableTracks&&this.bind(!0)},update:function(){var a,b,d,e,f,g=this;(t||k[n]!==k[o])&&(d=this.calc(),a=d.size,b=l.size,e=1/a*d.position*100,f=c.abs(d.position-(l.position||0))*k[n],1===a&&t&&g.toggle(!1),1>a&&!t&&g.toggle(!0),v&&t&&g.style(e,f,a,b),l=h(l,d),t&&g.fireEdgeEv())},style:function(a,b,c,d){var f=this;c!==d&&(v.style[e?"height":"width"]=100*c+"%"),b&&(f.animateTrack(p.isTouch&&b>20),v.style[p.cssTransform]="translate("+(e?"0%,"+a+"%":a+"%,0%")+")")},animateTrack:function(a){(a||w)&&(v.style[p.cssTransition]=s+(a?", "+p.cssTransformDashed+" 0.2s linear 0s":"")),w=a},bind:function(a){var c=(a?"add":"remove")+"EventListener",d=p.isTouch?["touchstart","touchmove","touchend"]:["mousedown","mousemove","mouseup"];v&&v[c](d[0],x.dragStart),b[c](d[1],x.dragMove),b[c](d[2],x.dragEnd)},calc:function(){var a,b,d=i[q],e=k[n],f=k[o],g=e/f,h=f-e;return 1===g||0===f?{position:0,size:1,percent:0}:(b=100*d/h,1>=d&&(b=0),d>=h-1&&(b=100),g=c.max(g,j.minTrackSize/100),g=c.min(g,j.maxTrackSize/100),a=(1-g)*(b/100),{position:a,size:g,percent:b})},fireEdgeEv:function(){var a=l.percent;l.was!==a&&a%100===0&&(d.fireCustomEvent("scrollreachedge"),d.fireCustomEvent("scrollreach"+r[a/100])),l.was=a},remove:function(){this.toggle(!1),this.bind(!1),u&&u.parentNode&&u.parentNode.removeChild(u)}}},o={hideNativeScrollbars:function(a){var b=p.nativeScrollbarSize,c=a.style;if(0===b){var d=Date.now();a.setAttribute("data-scroll",d),c.display="none",p.isTouch?o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"display: none;"):o.addCssRule('[data-scroll="'+d+'"]::-webkit-scrollbar',"width: 0; height: 0;"),q(function(){c.display="block"})}else c.overflow="scroll",c.right=-b+"px",c.bottom=-b+"px"},addCssRule:function(a,c){var d=b.getElementById("scroll-sheet");d||(d=b.createElement("style"),d.id="scroll-sheet",b.head.appendChild(d)),d.innerHTML+=a+"{"+c+"} "},containsNode:function(a,b){return a.contains?a!=b&&a.contains(b):!!(16&a.compareDocumentPosition(b))},checkLoop:function(){return p.instances.length?(p.pauseCheck||i(p.instances,"update"),void(l.checkFrequency&&(p.checkTimer=setTimeout(function(){o.checkLoop()},l.checkFrequency)))):void(p.checkTimer=null)},easingFunction:function(a){return--a*a*a+1}},p=k.G={isTouch:"ontouchstart"in a,cssTransition:f("transition"),cssTransform:f("transform")||"",nativeScrollbarSize:e(),instances:[],checkTimer:null,pauseCheck:!1};p.cssTransformDashed="transform"===p.cssTransform?p.cssTransform:"-"+p.cssTransform.replace("T","-t").toLowerCase();var q=function(){return a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.msRequestAnimationFrame||function(b){a.setTimeout(b,1e3/60)}}();"function"==typeof define&&define.amd&&define(function(){return k}),"undefined"!=typeof module&&module.exports&&(module.exports=k),a.Optiscroll=k}(window,document,Math); \ No newline at end of file diff --git a/package.json b/package.json index f8594f2..8fd9a45 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scrollbars", "user interface" ], - "version": "1.0.4", + "version": "1.0.5", "homepage": "https://github.com/wilsonfletcher/optiscroll", "repository": { "type": "git",