Skip to content

Commit

Permalink
WIP 1.4.7 #53
Browse files Browse the repository at this point in the history
  • Loading branch information
flesler committed Oct 11, 2013
1 parent 83776e5 commit 5081476
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
14 changes: 10 additions & 4 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
1.4.7
[Feature]
- Added setting "", when true, the scrolling animation will end if the user scrolls manually
[Misc]
- Changed spacing

1.4.6
[Fix]
Fixed first argument of onAfter and onAfterFirst was original target and should be "parsed" target
- Fixed first argument of onAfter and onAfterFirst was original target and should be "parsed" target

1.4.5
[Fix]
Fixed passing a negative scroll value crashes
- Fixed passing a negative scroll value crashes

1.4.4
[Change]
Re-released as 1.4.4 to avoid issues with bower
- Re-released as 1.4.4 to avoid issues with bower

1.4.3.1
[Fix]
Fixed $.scrollTo(0) broken on 1.4.3
- Fixed $.scrollTo(0) broken on 1.4.3

1.4.3
[Enhancement]
Expand Down
72 changes: 36 additions & 36 deletions jquery.scrollTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @projectDescription Easy element scrolling using jQuery.
* http://flesler.blogspot.com/2007/10/jqueryscrollto.html
* @author Ariel Flesler
* @version 1.4.6
*
* @version 1.4.7
* @id jQuery.scrollTo
* @id jQuery.fn.scrollTo
* @param {String, Number, DOMElement, jQuery, Object} target Where to scroll the matched elements.
Expand All @@ -23,6 +23,7 @@
* @param {Object,Function} settings Optional set of settings or the onAfter callback.
* @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
* @option {Number, Function} duration The OVERALL length of the animation.
* @option {Boolean} interrupt If true, the scrolling animation will stop on user scroll
* @option {String} easing The easing method for the animation.
* @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
* @option {Object, Number} offset Add/deduct from the end position. One number for both axes or { top:x, left:y }.
Expand All @@ -33,28 +34,27 @@
* @return {jQuery} Returns the same jQuery object, for chaining.
*
* @desc Scroll to a fixed position
* @example $('div').scrollTo( 340 );
* @example $('#container').scrollTo( 340 );
*
* @desc Scroll relatively to the actual position
* @example $('div').scrollTo( '+=340px', { axis:'y' } );
* @example $('#container').scrollTo( '+=340px', { axis:'y' } );
*
* @desc Scroll using a selector (relative to the scrolled element)
* @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
* @example $(window).scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
*
* @desc Scroll to a DOM element (same for jQuery object)
* @example var second_child = document.getElementById('container').firstChild.nextSibling;
* $('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
* @example $(window).scrollTo( document.getElementById('element'), { duration:500, axis:'x', onAfter:function() {
* alert('scrolled!!');
* }});
*
* @desc Scroll on both axes, to different values
* @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
*/

;(function( $ ){
;(function( $ ) {

var $scrollTo = $.scrollTo = function( target, duration, settings ){
$(window).scrollTo( target, duration, settings );
var $scrollTo = $.scrollTo = function( target, duration, settings ) {
return $(window).scrollTo( target, duration, settings );
};

$scrollTo.defaults = {
Expand All @@ -65,18 +65,18 @@

// Returns the element that needs to be animated to scroll the window.
// Kept for backwards compatibility (specially for localScroll & serialScroll)
$scrollTo.window = function( scope ){
$scrollTo.window = function( scope ) {
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(){
$.fn._scrollable = function() {
return this.map(function() {
var elem = this,
isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;

if( !isWin )
if (!isWin)
return elem;

var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
Expand All @@ -87,15 +87,15 @@
});
};

$.fn.scrollTo = function( target, duration, settings ){
if( typeof duration == 'object' ){
$.fn.scrollTo = function( target, duration, settings ) {
if (typeof duration == 'object') {
settings = duration;
duration = 0;
}
if( typeof settings == 'function' )
if (typeof settings == 'function')
settings = { onAfter:settings };

if( target == 'max' )
if (target == 'max')
target = 9e9;

settings = $.extend( {}, $scrollTo.defaults, settings );
Expand All @@ -104,13 +104,13 @@
// Make sure the settings are given right
settings.queue = settings.queue && settings.axis.length > 1;

if( settings.queue )
if (settings.queue)
// Let's keep the overall duration
duration /= 2;
settings.offset = both( settings.offset );
settings.over = both( settings.over );

return this._scrollable().each(function(){
return this._scrollable().each(function() {
// Null target yields nothing, just like jQuery does
if (target == null) return;

Expand All @@ -119,11 +119,11 @@
targ = target, toff, attr = {},
win = $elem.is('html,body');

switch( typeof targ ){
switch (typeof targ) {
// A number will pass the regex
case 'number':
case 'string':
if( /^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ) ){
if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) {
targ = both( targ );
// We are done
break;
Expand All @@ -133,32 +133,32 @@
if (!targ.length) return;
case 'object':
// DOMElement / jQuery
if( targ.is || targ.style )
if (targ.is || targ.style)
// Get the real position of the target
toff = (targ = $(targ)).offset();
}
$.each( settings.axis.split(''), function( i, axis ){
$.each( settings.axis.split(''), function( i, axis ) {
var Pos = axis == 'x' ? 'Left' : 'Top',
pos = Pos.toLowerCase(),
key = 'scroll' + Pos,
old = elem[key],
max = $scrollTo.max(elem, axis);

if( toff ){// jQuery / DOMElement
if (toff) {// jQuery / DOMElement
attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );

// If it's a dom element, reduce the margin
if( settings.margin ){
if (settings.margin) {
attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
}

attr[key] += settings.offset[pos] || 0;

if( settings.over[pos] )
if(settings.over[pos])
// Scroll to a fraction of its width/height
attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
}else{
} else {
var val = targ[pos];
// Handle percentage values
attr[key] = val.slice && val.slice(-1) == '%' ?
Expand All @@ -167,14 +167,14 @@
}

// Number or 'number'
if( settings.limit && /^\d+$/.test(attr[key]) )
if (settings.limit && /^\d+$/.test(attr[key]))
// Check the limits
attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );

// Queueing axes
if( !i && settings.queue ){
if (!i && settings.queue) {
// Don't waste time animating, if there's no need.
if( old != attr[key] )
if (old != attr[key])
// Intermediate animation
animate( settings.onAfterFirst );
// Don't animate this axis again in the next iteration.
Expand All @@ -184,8 +184,8 @@

animate( settings.onAfter );

function animate( callback ){
$elem.animate( attr, duration, settings.easing, callback && function(){
function animate( callback ) {
$elem.animate( attr, duration, settings.easing, callback && function() {
callback.call(this, targ, settings);
});
};
Expand All @@ -195,11 +195,11 @@

// Max scrolling position, works on quirks mode
// It only fails (not too badly) on IE, quirks mode.
$scrollTo.max = function( elem, axis ){
$scrollTo.max = function( elem, axis ) {
var Dim = axis == 'x' ? 'Width' : 'Height',
scroll = 'scroll'+Dim;

if( !$(elem).is('html,body') )
if (!$(elem).is('html,body'))
return elem[scroll] - $(elem)[Dim.toLowerCase()]();

var size = 'client' + Dim,
Expand All @@ -210,7 +210,7 @@
- Math.min( html[size] , body[size] );
};

function both( val ){
function both( val ) {
return typeof val == 'object' ? val : { top:val, left:val };
};

Expand Down

0 comments on commit 5081476

Please sign in to comment.