Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Forked from dbushell/gist:4131104
Created November 24, 2012 07:43
Show Gist options
  • Save ScottPhillips/4138804 to your computer and use it in GitHub Desktop.
Save ScottPhillips/4138804 to your computer and use it in GitHub Desktop.
Animate scrolling to an element or offset
/*!
* requires jQuery
* usage:
* Scroller.to({ offset: 100 });
* Scroller.to({ target: $('#main') });
*
* advanced usage (with additional easing function not provided here)
* Scroller.to({ target: $('#main'), delay: 500, multiplier: 1.5, easing: 'easeOutQuad' });
*/
var Scroller = (function()
{
var scroll = {
options: {
el: $('html:not(:animated),body:not(:animated)'),
multiplier: 1,
offset: undefined,
easing: 'swing',
delay: 0
},
_scrolling: false
};
scroll.to = function(options)
{
var height = {
top: $(window).scrollTop(),
doc: $(document).height(),
win: $(window).height()
};
if (height.doc <= height.win) {
return;
}
var config = $.extend({}, scroll.options);
config = $.extend(config, options || {});
config.offset = isNaN(config.offset) ? config.target.offset().top : config.offset;
var offset = config.offset,
distance = Math.abs(height.top - offset);
if (height.top < offset) {
var max = height.doc - height.win;
if (offset > max) {
distance -= offset - max;
}
}
if (distance) {
if (scroll._scrolling) {
scroll.stop();
}
scroll._scrolling = true;
scroll.timeout = window.setTimeout(function() {
config.el.animate({ scrollTop: Math.floor(offset) }, distance * config.multiplier, config.easing, function()
{
scroll._scrolling = false;
});
}, config.delay);
}
};
scroll.stop = function()
{
if (scroll.timeout) {
window.clearTimeout(scroll.timeout);
scroll.timeout = null;
}
scroll.options.el.stop();
scroll._scrolling = false;
};
scroll.isScrolling = function()
{
return scroll._scrolling;
};
return scroll;
})();
@robertjohnsonn12
Copy link

These are such great ideas and ways to help me improve bloxd io and solve my problems. This online game is my new product and it will bring you to 9+ environments to survive and fight in the pixel world. Are you ready to join it? Let's go!

@bmxmehul5
Copy link

The Ziunx Android Emulator is a cutting-edge, free application designed to replicate the Nintendo Switch experience on Android and iOS devices. This emulator allows gamers to enjoy their favorite Switch titles without needing the actual console, bringing the Nintendo Switch experience directly to your mobile device.

@apkgstore
Copy link

You can download latest games and apps free from https://apkgstore.net// Give it a try right now.

@kibbebodytype
Copy link

Nice implementation of smooth scrolling! This kind of functionality really enhances user experience. If you’re into advanced mobile solutions or need a phone service that’s both efficient and cutting-edge, you might want to check out Tesla. It offers some great features for tech enthusiasts.

@Steve5463
Copy link

Methylated B12 might be the missing puzzle piece in your health journey, especially if you’ve been dealing with unexplained fatigue or neurological symptoms. Ready to feel the difference? Click here for access to premium-grade supplements.

@Steve5463
Copy link

Whether on Android or other platforms, Fire Kirin APK provides a seamless gaming experience.

@Steve5463
Copy link

The low taper fade haircut is a classic go-to for a smooth and professional appearance. It starts just above the ear and gradually blends into the neckline. Perfect for those who want a low-maintenance yet polished style.

@Steve5463
Copy link

MB Whatsapp frequently updates with new features that are not available in the original version.

@Steve5463
Copy link

The honista 6.0 apk download is not available on Play Store.
You must download it from trusted third-party sites.
Always check sources to avoid malware.

@Steve5463
Copy link

Updates to toca boca mod apk often include new features. This keeps players engaged and encourages continued exploration. It’s a constantly growing digital universe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment