Permalink
Please sign in to comment.
Browse files
Using propHooks to animate the window and use jQuery's functions #107
- Loading branch information...
Showing
with
81 additions
and 106 deletions.
- +1 −1 LICENSE
- +2 −2 bower.json
- +13 −0 changes.txt
- +1 −1 composer.json
- +42 −41 jquery.scrollTo.js
- +3 −3 jquery.scrollTo.min.js
- +2 −2 package.json
- +1 −1 tests/ElemMaxY-compat.html
- +1 −1 tests/ElemMaxY-quirks.html
- +1 −1 tests/WinMaxY-compat.html
- +1 −1 tests/WinMaxY-quirks.html
- +2 −2 tests/WinMaxY-with-iframe-compat.html
- +11 −50 tests/test.js
| @@ -1,6 +1,6 @@ | ||
| (The MIT License) | ||
| -Copyright (c) 2007-2014 Ariel Flesler <[email protected]> | ||
| +Copyright (c) 2007-2015 Ariel Flesler <[email protected]> | ||
| Permission is hereby granted, free of charge, to any person obtaining | ||
| a copy of this software and associated documentation files (the | ||
13
changes.txt
| @@ -1,7 +1,7 @@ | ||
| /** | ||
| - * Copyright (c) 2007-2014 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com | ||
| + * Copyright (c) 2007-2015 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com | ||
| * Licensed under MIT | ||
| * @author Ariel Flesler | ||
| - * @version 1.4.14 | ||
| + * @version 2.0.0 | ||
| */ | ||
| -;(function(k){'use strict';k(['jquery'],function($){var j=$.scrollTo=function(a,b,c){return $(window).scrollTo(a,b,c)};j.defaults={axis:'xy',duration:0,limit:!0};j.window=function(a){return $(window)._scrollable()};$.fn._scrollable=function(){return this.map(function(){var a=this,isWin=!a.nodeName||$.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!isWin)return a;var b=(a.contentWindow||a).document||a.ownerDocument||a;return/webkit/i.test(navigator.userAgent)||b.compatMode=='BackCompat'?b.body:b.documentElement})};$.fn.scrollTo=function(f,g,h){if(typeof g=='object'){h=g;g=0}if(typeof h=='function')h={onAfter:h};if(f=='max')f=9e9;h=$.extend({},j.defaults,h);g=g||h.duration;h.queue=h.queue&&h.axis.length>1;if(h.queue)g/=2;h.offset=both(h.offset);h.over=both(h.over);return this._scrollable().each(function(){if(f==null)return;var d=this,$elem=$(d),targ=f,toff,attr={},win=$elem.is('html,body');switch(typeof targ){case'number':case'string':if(/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)){targ=both(targ);break}targ=win?$(targ):$(targ,this);if(!targ.length)return;case'object':if(targ.is||targ.style)toff=(targ=$(targ)).offset()}var e=$.isFunction(h.offset)&&h.offset(d,targ)||h.offset;$.each(h.axis.split(''),function(i,a){var b=a=='x'?'Left':'Top',pos=b.toLowerCase(),key='scroll'+b,old=d[key],max=j.max(d,a);if(toff){attr[key]=toff[pos]+(win?0:old-$elem.offset()[pos]);if(h.margin){attr[key]-=parseInt(targ.css('margin'+b))||0;attr[key]-=parseInt(targ.css('border'+b+'Width'))||0}attr[key]+=e[pos]||0;if(h.over[pos])attr[key]+=targ[a=='x'?'width':'height']()*h.over[pos]}else{var c=targ[pos];attr[key]=c.slice&&c.slice(-1)=='%'?parseFloat(c)/100*max:c}if(h.limit&&/^\d+$/.test(attr[key]))attr[key]=attr[key]<=0?0:Math.min(attr[key],max);if(!i&&h.queue){if(old!=attr[key])animate(h.onAfterFirst);delete attr[key]}});animate(h.onAfter);function animate(a){$elem.animate(attr,g,h.easing,a&&function(){a.call(this,targ,h)})}}).end()};j.max=function(a,b){var c=b=='x'?'Width':'Height',scroll='scroll'+c;if(!$(a).is('html,body'))return a[scroll]-$(a)[c.toLowerCase()]();var d='client'+c,html=a.ownerDocument.documentElement,body=a.ownerDocument.body;return Math.max(html[scroll],body[scroll])-Math.min(html[d],body[d])};function both(a){return $.isFunction(a)||$.isPlainObject(a)?a:{top:a,left:a}}return j})}(typeof define==='function'&&define.amd?define:function(a,b){if(typeof module!=='undefined'&&module.exports){module.exports=b(require('jquery'))}else{b(jQuery)}})); | ||
| +;(function(k){'use strict';k(['jquery'],function($){var j=$.scrollTo=function(a,b,c){return $(window).scrollTo(a,b,c)};j.defaults={axis:'xy',duration:0,limit:true};function isWin(a){return!a.nodeName||$.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!==-1}$.fn.scrollTo=function(f,g,h){if(typeof g==='object'){h=g;g=0}if(typeof h==='function'){h={onAfter:h}}if(f==='max'){f=9e9}h=$.extend({},j.defaults,h);g=g||h.duration;h.queue=h.queue&&h.axis.length>1;if(h.queue){g/=2}h.offset=both(h.offset);h.over=both(h.over);return this.each(function(){if(f===null)return;var d=isWin(this),elem=d?window:this,$elem=$(elem),targ=f,attr={},toff;switch(typeof targ){case'number':case'string':if(/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)){targ=both(targ);break}targ=d?$(targ):$(targ,elem);if(!targ.length)return;case'object':if(targ.is||targ.style){toff=(targ=$(targ)).offset()}}var e=$.isFunction(h.offset)&&h.offset(elem,targ)||h.offset;$.each(h.axis.split(''),function(i,a){var b=a==='x'?'Left':'Top',pos=b.toLowerCase(),key='scroll'+b,prev=$(elem)[key](),max=j.max(elem,a);if(toff){attr[key]=toff[pos]+(d?0:prev-$elem.offset()[pos]);if(h.margin){attr[key]-=parseInt(targ.css('margin'+b),10)||0;attr[key]-=parseInt(targ.css('border'+b+'Width'),10)||0}attr[key]+=e[pos]||0;if(h.over[pos]){attr[key]+=targ[a==='x'?'width':'height']()*h.over[pos]}}else{var c=targ[pos];attr[key]=c.slice&&c.slice(-1)==='%'?parseFloat(c)/100*max:c}if(h.limit&&/^\d+$/.test(attr[key])){attr[key]=attr[key]<=0?0:Math.min(attr[key],max)}if(!i&&h.queue){if(prev!==attr[key]){animate(h.onAfterFirst)}attr={}}});animate(h.onAfter);function animate(a){var b=$.extend({},h,{duration:g,complete:a&&function(){a.call(elem,targ,h)}});$elem.animate(attr,b)}})};j.max=function(a,b){var c=b==='x'?'Width':'Height',scroll='scroll'+c;if(!isWin(a))return a[scroll]-$(a)[c.toLowerCase()]();var d='client'+c,doc=a.ownerDocument||a.document,html=doc.documentElement,body=doc.body;return Math.max(html[scroll],body[scroll])-Math.min(html[d],body[d])};function both(a){return $.isFunction(a)||$.isPlainObject(a)?a:{top:a,left:a}}$.Tween.propHooks.scrollLeft=$.Tween.propHooks.scrollTop={get:function(t){return $(t.elem)[t.prop]()},set:function(t){var v=Math.round(t.now);if(this.get(t)!==v){$(t.elem)[t.prop](v)}}};return j})}(typeof define==='function'&&define.amd?define:function(a,b){'use strict';if(typeof module!=='undefined'&&module.exports){module.exports=b(require('jquery'))}else{b(jQuery)}})); |
| @@ -1,60 +1,21 @@ | ||
| $.fn.test = function(){ | ||
| - if( location.search == '?notest' ) | ||
| + // Hide title on iframes | ||
| + if (window.self !== window.top) { | ||
| + $('h1').hide(); | ||
| + } | ||
| + | ||
| + if (location.search === '?notest') { | ||
| return this; | ||
| + } | ||
| - testScrollable(); | ||
| - | ||
| $.scrollTo.defaults.axis = 'xy'; | ||
| - this._scrollable().find('div').html( | ||
| + var root = this.is('iframe') ? this.contents() : $('body'); | ||
| + root.find('#ua').html( | ||
| navigator.userAgent + | ||
| '<br />' + | ||
| - 'document.compatMode is "' + document.compatMode + '"' + | ||
| - '<br />' + | ||
| - 'scrolling the ' + this._scrollable().prop('nodeName') | ||
| + 'document.compatMode is "' + document.compatMode + '"' | ||
| ); | ||
| - /*var orig = [ $(window).scrollLeft(), $(window).scrollTop() ]; | ||
| - | ||
| - scrollTo(0,1); | ||
| - var elem = document.documentElement.scrollTop ? document.documentElement : document.body; | ||
| - scrollTo(0,9e9); | ||
| - var max = $(window).scrollTop(); | ||
| - scrollTo(orig[0],orig[1]); | ||
| - | ||
| - setTimeout(function(){ | ||
| - alert( elem.nodeName + ' ' + max ); | ||
| - }, 1000 );*/ | ||
| - return this.scrollTo('max', 1000).scrollTo(0, 1000) | ||
| + return this.scrollTo('max', 1000).scrollTo(0, 1000); | ||
| }; | ||
| - | ||
| -function assert( bool, message ){ | ||
| - if( !bool ){ | ||
| - alert('FAIL: ' + message); | ||
| - throw new Error(); | ||
| - } | ||
| -}; | ||
| - | ||
| -function f( name ){ | ||
| - return $(name)[0]; | ||
| -} | ||
| - | ||
| -function testScrollable(){ | ||
| - | ||
| - $.each([ window, document, f('body'), f('html') ], function(i, elem){ | ||
| - var s = $(elem)._scrollable(); | ||
| - assert( s.length == 1, 'scrollable must always return exactly 1 element' ); | ||
| - assert( s.is('html,body'), 'scrollable must either html or body for window scrolling' ); | ||
| - }); | ||
| - | ||
| - $('body :not(iframe)').each(function(){ | ||
| - var s = $(this)._scrollable(); | ||
| - assert( s.length == 1, 'scrollable must always return exactly 1 element' ); | ||
| - assert( s[0] == this, 'scrollable must return the same element for normal element scrolling' ); | ||
| - }); | ||
| -}; | ||
| - | ||
| -$(function(){ | ||
| - if( location.search == '?notest' ) | ||
| - $('h1').hide(); | ||
| -}); |
0 comments on commit
d87e471