Component-Builder/media/uikit/js/core/smooth-scroll.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-11-12 00:33:10 +00:00
/*! UIkit 2.27.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2016-01-30 20:28:43 +00:00
(function(UI) {
"use strict";
UI.component('smoothScroll', {
boot: function() {
// init code
2017-11-12 00:33:10 +00:00
UI.$html.on('click.smooth-scroll.uikit', '[data-uk-smooth-scroll]', function(e) {
2016-01-30 20:28:43 +00:00
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!ele.data('smoothScroll')) {
var obj = UI.smoothScroll(ele, UI.Utils.options(ele.attr('data-uk-smooth-scroll')));
ele.trigger('click');
2016-01-30 20:28:43 +00:00
}
return false;
});
},
init: function() {
var $this = this;
2017-11-12 00:33:10 +00:00
this.on('click', function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
2017-11-12 00:33:10 +00:00
scrollToElement(UI.$(this.hash).length ? UI.$(this.hash) : UI.$('body'), $this.options);
2016-01-30 20:28:43 +00:00
});
}
});
function scrollToElement(ele, options) {
options = UI.$.extend({
duration: 1000,
transition: 'easeOutExpo',
offset: 0,
complete: function(){}
}, options);
// get / set parameters
var target = ele.offset().top - options.offset,
docheight = UI.$doc.height(),
winheight = window.innerHeight;
if ((target + winheight) > docheight) {
target = docheight - winheight;
}
// animate to target, fire callback when done
2017-11-12 00:33:10 +00:00
UI.$('html,body').stop().animate({scrollTop: target}, options.duration, options.transition).promise().done(options.complete);
2016-01-30 20:28:43 +00:00
}
UI.Utils.scrollToElement = scrollToElement;
if (!UI.$.easing.easeOutExpo) {
UI.$.easing.easeOutExpo = function(x, t, b, c, d) { return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b; };
}
2017-11-12 00:33:10 +00:00
})(UIkit2);