-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.softLanding.js
33 lines (32 loc) · 1.42 KB
/
jquery.softLanding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//softLanding v0.3
$(document).ready(function(){
// Take an item, if the viewport scrolls past it's position, pin it to the top of the viewport
//** This version of the code by [email protected]
//** http://linowski.ca/experiments/03_sticky_floating_nav/assets/core.js
$(window).scroll(function(){
if($(window).scrollTop() > 320) {
$('#nav').css({'top':'3px','position':'fixed'});
}
else {
$('#nav').css({'position':'absolute','top':'320px'});
}
});
// Here we bind to clicks on the anchors, animate the movement, and prevent a sudden stop.
//** by Phil Banks http://customcreative.co.uk
$('a[href^="#"]').click(function(e){
//Get the offset of the top of the element just clicked
var dest = $(this.hash).offset().top;
var dHeight = $(document).height(); // height of document
var vHeight = $(window).height(); // height of browser viewport
//If the destination coordinates are not in the last viewport's worth of document height then scroll to them.
if (dest < (dHeight-vHeight)) {
$('html,body').stop().animate({scrollTop: dest},1500,'easeOutCirc');
}
//Otherwise define 'dest' as the top of the last viewport's worth of document height and scroll there. This prevents the animation trying to scroll past the end of the document and causing a sudden stop.
else {
var dest = (dHeight-vHeight);
$('html,body').stop().animate({scrollTop: dest},1500,'easeOutCirc');
}
e.preventDefault();
});
});