Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
  • Loading branch information
flesler committed Jan 13, 2014
1 parent 249b156 commit bba156c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -39,7 +39,7 @@ If you want the latest stable version, get the latest release from the [releases

* 'margin' will only be valid, if the target is a selector, a DOM element, or a jQuery Object.

* The option 'offset' allows to scroll less or more than the actual target by a defined amount of pixels. Can be a number(both axes) or { top:x, left:y }.
* The option 'offset' allows to scroll less or more than the actual target by a defined amount of pixels. Can be a number(both axes), { top:x, left:y } or a function that returns an object with top & left.

* The option 'over' lets you add or deduct a fraction of the element's height and width from the final position. so over:0.5 will scroll to the middle of the object. can be specified with {top:x, left:y}

Expand Down
6 changes: 5 additions & 1 deletion changes.txt
@@ -1,5 +1,9 @@
1.4.9
[Enhancement]
- "offset" setting can now be a function as well (#60)

1.4.8
[Improvement]
[Enhancement]
- Added support for AMD

1.4.7
Expand Down
2 changes: 1 addition & 1 deletion component.json
@@ -1,6 +1,6 @@
{
"name": "jquery.scrollTo",
"version": "1.4.8",
"version": "1.4.9",
"description": "Easy element scrolling using jQuery.",
"homepage": "https://github.com/flesler/jquery.scrollTo",
"main": [
Expand Down
6 changes: 5 additions & 1 deletion demo/index.html
Expand Up @@ -8,7 +8,7 @@
<meta name="robots" content="index,follow" />
<link type="text/css" rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="../jquery.scrollTo.min.js"></script>
<script type="text/javascript" src="../jquery.scrollTo.js"></script>
<script type="text/javascript">
jQuery(function( $ ){
/**
Expand Down Expand Up @@ -115,6 +115,9 @@
$('#options-offset-hash').click(function(){
reset_o(); $paneOptions.scrollTo( 'li:eq(15)', 1000, { offset:{ top:-5,left:-30 } });
});
$('#options-offset-function').click(function(){
reset_o(); $paneOptions.scrollTo( 'li:eq(15)', 1000, {offset: function() { return {top:-30, left:-5}; }});
});
$('#options-over').click(function(){//same as { top:-50, left:-50 }
reset_o(); $paneOptions.scrollTo( 'li:eq(15)', 1000, { over:0.5 });
});
Expand Down Expand Up @@ -199,6 +202,7 @@ <h3>Options&nbsp;<span id="options-message" class="message">The examples shown h
<li><a title="$(...).scrollTo( 'li:eq(15)', 1000, {margin:true} );//deduct the margin and border from the final position" id="options-margin" href="#">margin</a></li>
<li><a title="$(...).scrollTo( 'li:eq(15)', 1000, {offset:-50} );//add or deduct from the final position" id="options-offset" href="#">offset</a></li>
<li><a title="$(...).scrollTo( 'li:eq(15)', 1000, {offset: {top:-5, left:-30} });" id="options-offset-hash" href="#">offset(hash)</a></li>
<li><a title="$(...).scrollTo( 'li:eq(15)', 1000, {offset: function() { return {top:-30, left:-5}; }});" id="options-offset-function" href="#">offset(function)</a></li>
<li><a title="$(...).scrollTo( 'li:eq(15)', 1000, {over:0.5} );//add or deduct a fraction of its height/width" id="options-over" href="#">over</a></li>
<li><a title="$(...).scrollTo( 'li:eq(15)', 1000, {over:{top:0.2, left:-0.5} );" id="options-over-hash" href="#">over(hash)</a></li>
<li><a title="$(...).scrollTo( 'li:eq(15)', 1600, {queue:true} );//scroll one axis, then the other" id="options-queue" href="#">queue</a></li>
Expand Down
37 changes: 19 additions & 18 deletions jquery.scrollTo.js
Expand Up @@ -5,7 +5,7 @@
* http://flesler.blogspot.com/2007/10/jqueryscrollto.html
* @projectDescription Easy element scrolling using jQuery.
* @author Ariel Flesler
* @version 1.4.8
* @version 1.4.9
*/

;(function (factory) {
Expand Down Expand Up @@ -44,9 +44,9 @@
return elem;

var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;

return /webkit/i.test(navigator.userAgent) || doc.compatMode == 'BackCompat' ?
doc.body :
doc.body :
doc.documentElement;
});
};
Expand All @@ -58,16 +58,16 @@
}
if (typeof settings == 'function')
settings = { onAfter:settings };

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

settings = $.extend( {}, $scrollTo.defaults, settings );
// Speed is still recognized for backwards compatibility
duration = duration || settings.duration;
// Make sure the settings are given right
settings.queue = settings.queue && settings.axis.length > 1;

if (settings.queue)
// Let's keep the overall duration
duration /= 2;
Expand Down Expand Up @@ -98,7 +98,7 @@
case 'object':
// DOMElement / jQuery
if (targ.is || targ.style)
// Get the real position of the target
// Get the real position of the target
toff = (targ = $(targ)).offset();
}
$.each( settings.axis.split(''), function( i, axis ) {
Expand All @@ -116,16 +116,17 @@
attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
}

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


var offset = $.isFunction(settings.offset) && settings.offset() || settings.offset;
attr[key] += offset[pos] || 0;

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) == '%' ?
attr[key] = val.slice && val.slice(-1) == '%' ?
parseFloat(val) / 100 * max
: val;
}
Expand All @@ -146,7 +147,7 @@
}
});

animate( settings.onAfter );
animate( settings.onAfter );

function animate( callback ) {
$elem.animate( attr, duration, settings.easing, callback && function() {
Expand All @@ -156,26 +157,26 @@

}).end();
};

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

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

var size = 'client' + Dim,
html = elem.ownerDocument.documentElement,
body = elem.ownerDocument.body;

return Math.max( html[scroll], body[scroll] )
return Math.max( html[scroll], body[scroll] )
- Math.min( html[size] , body[size] );
};

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

// AMD requirement
Expand Down
4 changes: 2 additions & 2 deletions jquery.scrollTo.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scrollTo.jquery.json
Expand Up @@ -3,7 +3,7 @@
"title": "Ariel Flesler's jQuery scrollTo",
"description": "Easy element scrolling using jQuery.",
"keywords": ["browser", "animated", "animation", "scrolling", "scroll", "links", "anchors"],
"version": "1.4.8",
"version": "1.4.9",
"author": {
"name": "Ariel Flesler",
"email": "aflesler@gmail.com",
Expand Down

0 comments on commit bba156c

Please sign in to comment.