diff --git a/LICENSE b/LICENSE index 69c814d..fe5853e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ (The MIT License) -Copyright (c) 2007-2014 Ariel Flesler +Copyright (c) 2007-2015 Ariel Flesler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index 11ff010..7bf4b32 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ CDN provided by [cdnjs](https://cdnjs.com/libraries/jquery-scrollTo) If you want the latest stable version, get the latest release from the [releases page](https://github.com/flesler/jquery.scrollTo/releases). +## 2.0 + +Version 2.0 has been recently released. It is mostly backwards compatible, if you have any issue first check [this link](https://github.com/flesler/jquery.scrollTo/wiki/Migrating-to-2.0). +If your problem is not solved then go ahead and [report the issue](https://github.com/flesler/jquery.scrollTo/issues/new). + ## Notes * Apart from the target and duration, the plugin can receive a hash of settings. Documentation and examples are included in the source file. diff --git a/bower.json b/bower.json index eee7ef0..5b107e2 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery.scrollTo", - "version": "1.4.14", + "version": "2.0.0", "description": "Easy element scrolling using jQuery.", "homepage": "https://github.com/flesler/jquery.scrollTo", "main": [ @@ -16,7 +16,7 @@ "scrollTo.jquery.json" ], "dependencies": { - "jquery": ">=1.4" + "jquery": ">=1.8" }, "keywords": [ "browser", "animated", "animation", "jquery", diff --git a/changes.txt b/changes.txt index 6baab79..addaf39 100644 --- a/changes.txt +++ b/changes.txt @@ -1,3 +1,17 @@ +2.0.0 +[Feature] +- All settings are passed to jQuery.animate() meaning it now supports even more settings +[Enhancement] +- $(window)._scrollable() is no longer needed, the element is always the window +- Delegating to jQuery the get/set of element/window scroll positions. +[Compat] +- Dropped support for $.scrollTo.window() and $(window)._scrollable() +[Fix] +- Now works consistenly on Chrome 40 +- Now works correctly on Windows Phone +- Now works correctly on Android Browsers +- Now works correctly on iOS Browsers + 1.4.14 [Misc] - Internal both() function will handle nulls correctly diff --git a/composer.json b/composer.json index 92a7604..c4173c2 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "components/jquery": ">=1.4" + "components/jquery": ">=1.8" }, "extra": { "component": { diff --git a/jquery.scrollTo.js b/jquery.scrollTo.js index 964ba3b..5cd7389 100644 --- a/jquery.scrollTo.js +++ b/jquery.scrollTo.js @@ -1,11 +1,11 @@ /*! * jQuery.scrollTo - * Copyright (c) 2007-2014 Ariel Flesler - afleslergmailcom | http://flesler.blogspot.com + * Copyright (c) 2007-2015 Ariel Flesler - afleslergmailcom | http://flesler.blogspot.com * Licensed under MIT * http://flesler.blogspot.com/2007/10/jqueryscrollto.html * @projectDescription Easy element scrolling using jQuery. * @author Ariel Flesler - * @version 1.4.14 + * @version 2.0.0 */ ;(function(define) { 'use strict'; @@ -21,30 +21,10 @@ limit:true }; - // Returns the element that needs to be animated to scroll the window. - // Kept for backwards compatibility (specially for localScroll & serialScroll) - $scrollTo.window = function() { - return $(window)._scrollable(); - }; - - // Hack, hack, hack :) - // Returns the real elements to scroll (supports window/iframes, documents and regular nodes) - $.fn._scrollable = function() { - return this.map(function() { - var elem = this, - isWin = !elem.nodeName || $.inArray(elem.nodeName.toLowerCase(), ['iframe','#document','html','body']) !== -1; - - if (!isWin) { - return elem; - } - - var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem; - - return /webkit/i.test(navigator.userAgent) || doc.compatMode === 'BackCompat' ? - doc.body : - doc.documentElement; - }); - }; + function isWin(elem) { + return !elem.nodeName || + $.inArray(elem.nodeName.toLowerCase(), ['iframe','#document','html','body']) !== -1; + } $.fn.scrollTo = function(target, duration, settings) { if (typeof duration === 'object') { @@ -71,14 +51,16 @@ settings.offset = both(settings.offset); settings.over = both(settings.over); - return this._scrollable().each(function() { + return this.each(function() { // Null target yields nothing, just like jQuery does if (target === null) return; - var elem = this, + var win = isWin(this), + elem = win ? this.contentWindow || window : this, $elem = $(elem), - targ = target, toff, attr = {}, - win = $elem.is('html,body'); + targ = target, + attr = {}, + toff; switch (typeof targ) { // A number will pass the regex @@ -89,8 +71,8 @@ // We are done break; } - // Relative/Absolute selector, no break! - targ = win ? $(targ) : $(targ, this); + // Relative/Absolute selector + targ = win ? $(targ) : $(targ, elem); if (!targ.length) return; /* falls through */ case 'object': @@ -107,11 +89,11 @@ var Pos = axis === 'x' ? 'Left' : 'Top', pos = Pos.toLowerCase(), key = 'scroll' + Pos, - old = elem[key], + prev = $(elem)[key](), max = $scrollTo.max(elem, axis); if (toff) {// jQuery / DOMElement - attr[key] = toff[pos] + (win ? 0 : old - $elem.offset()[pos]); + attr[key] = toff[pos] + (win ? 0 : prev - $elem.offset()[pos]); // If it's a dom element, reduce the margin if (settings.margin) { @@ -142,23 +124,27 @@ // Queueing axes if (!i && settings.queue) { // Don't waste time animating, if there's no need. - if (old !== attr[key]) { + if (prev !== attr[key]) { // Intermediate animation animate(settings.onAfterFirst); } // Don't animate this axis again in the next iteration. - delete attr[key]; + attr = {}; } }); animate(settings.onAfter); function animate(callback) { - $elem.animate(attr, duration, settings.easing, callback && function() { - callback.call(this, targ, settings); + var opts = $.extend({}, settings, { + duration: duration, + complete: callback && function() { + callback.call(elem, targ, settings); + } }); + $elem.animate(attr, opts); } - }).end(); + }); }; // Max scrolling position, works on quirks mode @@ -167,12 +153,13 @@ var Dim = axis === 'x' ? 'Width' : 'Height', scroll = 'scroll'+Dim; - if (!$(elem).is('html,body')) + if (!isWin(elem)) return elem[scroll] - $(elem)[Dim.toLowerCase()](); var size = 'client' + Dim, - html = elem.ownerDocument.documentElement, - body = elem.ownerDocument.body; + doc = elem.ownerDocument || elem.document, + html = doc.documentElement, + body = doc.body; return Math.max(html[scroll], body[scroll]) - Math.min(html[size], body[size]); }; @@ -181,6 +168,20 @@ return $.isFunction(val) || $.isPlainObject(val) ? val : { top:val, left:val }; } + // Add special hooks so that window scroll properties can be animated + $.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); + } + } + }; + // AMD requirement return $scrollTo; }); diff --git a/jquery.scrollTo.min.js b/jquery.scrollTo.min.js index 5f5e01e..d65f94c 100644 --- a/jquery.scrollTo.min.js +++ b/jquery.scrollTo.min.js @@ -1,7 +1,7 @@ /** - * Copyright (c) 2007-2014 Ariel Flesler - afleslergmailcom | http://flesler.blogspot.com + * Copyright (c) 2007-2015 Ariel Flesler - afleslergmailcom | 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)}})); \ No newline at end of file +;(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?this.contentWindow||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)}})); \ No newline at end of file diff --git a/package.json b/package.json index a547ece..55650c6 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "jquery.scrollto", - "version": "1.4.14", + "version": "2.0.0", "description": "Easy element scrolling using jQuery.", "main": "jquery.scrollTo.js", "license": "MIT", "ignore": ["**/.*","demo","tests","changes.txt","composer.json","bower.json","scrollTo.jquery.json"], - "dependencies": { "jquery": ">=1.4" }, + "dependencies": { "jquery": ">=1.8" }, "homepage": "https://github.com/flesler/jquery.scrollTo/", "bugs": "https://github.com/flesler/jquery.scrollTo/issues", "repository": "git://github.com/flesler/jquery.scrollTo", diff --git a/tests/ElemMaxY-compat.html b/tests/ElemMaxY-compat.html index 7a33609..b0f761b 100644 --- a/tests/ElemMaxY-compat.html +++ b/tests/ElemMaxY-compat.html @@ -11,7 +11,7 @@

jQuery.scrollTo - Test Element MaxY - Compat Mode

-
+
 
diff --git a/tests/ElemMaxY-quirks.html b/tests/ElemMaxY-quirks.html index c00f7db..1bffc74 100644 --- a/tests/ElemMaxY-quirks.html +++ b/tests/ElemMaxY-quirks.html @@ -10,7 +10,7 @@

jQuery.scrollTo - Test Element MaxY - Quirks Mode

-
+
 
diff --git a/tests/WinMaxY-compat.html b/tests/WinMaxY-compat.html index 555eb77..ad745fe 100644 --- a/tests/WinMaxY-compat.html +++ b/tests/WinMaxY-compat.html @@ -10,7 +10,7 @@

jQuery.scrollTo - Test Window MaxY - Compat Mode

-
+