Component-Builder/admin/custom/uikit-v2/js/core/scrollspy.js

210 lines
6.9 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";
var $win = UI.$win,
$doc = UI.$doc,
scrollspies = [],
checkScrollSpy = function() {
for(var i=0; i < scrollspies.length; i++) {
window.requestAnimationFrame.apply(window, [scrollspies[i].check]);
}
};
UI.component('scrollspy', {
defaults: {
2017-11-12 00:33:10 +00:00
target : false,
cls : 'uk-scrollspy-inview',
initcls : 'uk-scrollspy-init-inview',
topoffset : 0,
leftoffset : 0,
repeat : false,
delay : 0
2016-01-30 20:28:43 +00:00
},
boot: function() {
// listen to scroll and resize
2017-11-12 00:33:10 +00:00
$doc.on('scrolling.uk.document', checkScrollSpy);
$win.on('load resize orientationchange', UI.Utils.debounce(checkScrollSpy, 50));
2016-01-30 20:28:43 +00:00
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-scrollspy]', context).each(function() {
2016-01-30 20:28:43 +00:00
var element = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!element.data('scrollspy')) {
var obj = UI.scrollspy(element, UI.Utils.options(element.attr('data-uk-scrollspy')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var $this = this, inviewstate, initinview, togglecls = this.options.cls.split(/,/), fn = function(){
var elements = $this.options.target ? $this.element.find($this.options.target) : $this.element,
delayIdx = elements.length === 1 ? 1 : 0,
toggleclsIdx = 0;
elements.each(function(idx){
var element = UI.$(this),
inviewstate = element.data('inviewstate'),
inview = UI.Utils.isInView(element, $this.options),
2017-11-12 00:33:10 +00:00
toggle = element.attr('data-uk-scrollspy-cls') || togglecls[toggleclsIdx].trim();
2016-01-30 20:28:43 +00:00
if (inview && !inviewstate && !element.data('scrollspy-idle')) {
if (!initinview) {
element.addClass($this.options.initcls);
$this.offset = element.offset();
initinview = true;
2017-11-12 00:33:10 +00:00
element.trigger('init.uk.scrollspy');
2016-01-30 20:28:43 +00:00
}
element.data('scrollspy-idle', setTimeout(function(){
2017-11-12 00:33:10 +00:00
element.addClass('uk-scrollspy-inview').toggleClass(toggle).width();
element.trigger('inview.uk.scrollspy');
2016-01-30 20:28:43 +00:00
element.data('scrollspy-idle', false);
element.data('inviewstate', true);
}, $this.options.delay * delayIdx));
delayIdx++;
}
if (!inview && inviewstate && $this.options.repeat) {
if (element.data('scrollspy-idle')) {
clearTimeout(element.data('scrollspy-idle'));
2016-03-19 01:51:35 +00:00
element.data('scrollspy-idle', false);
2016-01-30 20:28:43 +00:00
}
2017-11-12 00:33:10 +00:00
element.removeClass('uk-scrollspy-inview').toggleClass(toggle);
2016-01-30 20:28:43 +00:00
element.data('inviewstate', false);
2017-11-12 00:33:10 +00:00
element.trigger('outview.uk.scrollspy');
2016-01-30 20:28:43 +00:00
}
toggleclsIdx = togglecls[toggleclsIdx + 1] ? (toggleclsIdx + 1) : 0;
});
};
fn();
this.check = fn;
scrollspies.push(this);
}
});
var scrollspynavs = [],
checkScrollSpyNavs = function() {
for(var i=0; i < scrollspynavs.length; i++) {
window.requestAnimationFrame.apply(window, [scrollspynavs[i].check]);
}
};
UI.component('scrollspynav', {
defaults: {
2017-11-12 00:33:10 +00:00
cls : 'uk-active',
closest : false,
topoffset : 0,
leftoffset : 0,
smoothscroll : false
2016-01-30 20:28:43 +00:00
},
boot: function() {
// listen to scroll and resize
2017-11-12 00:33:10 +00:00
$doc.on('scrolling.uk.document', checkScrollSpyNavs);
$win.on('resize orientationchange', UI.Utils.debounce(checkScrollSpyNavs, 50));
2016-01-30 20:28:43 +00:00
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-scrollspy-nav]', context).each(function() {
2016-01-30 20:28:43 +00:00
var element = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!element.data('scrollspynav')) {
var obj = UI.scrollspynav(element, UI.Utils.options(element.attr('data-uk-scrollspy-nav')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var ids = [],
2017-11-12 00:33:10 +00:00
links = this.find("a[href^='#']").each(function(){ if(this.getAttribute('href').trim()!=='#') ids.push(this.getAttribute('href')); }),
2016-01-30 20:28:43 +00:00
targets = UI.$(ids.join(",")),
clsActive = this.options.cls,
clsClosest = this.options.closest || this.options.closest;
var $this = this, inviews, fn = function(){
inviews = [];
for (var i=0 ; i < targets.length ; i++) {
if (UI.Utils.isInView(targets.eq(i), $this.options)) {
inviews.push(targets.eq(i));
}
}
if (inviews.length) {
var navitems,
scrollTop = $win.scrollTop(),
target = (function(){
for(var i=0; i< inviews.length;i++){
2017-11-12 00:33:10 +00:00
if (inviews[i].offset().top - $this.options.topoffset >= scrollTop){
2016-01-30 20:28:43 +00:00
return inviews[i];
}
}
})();
if (!target) return;
if ($this.options.closest) {
2016-03-19 01:51:35 +00:00
links.blur().closest(clsClosest).removeClass(clsActive);
2017-11-12 00:33:10 +00:00
navitems = links.filter("a[href='#"+target.attr('id')+"']").closest(clsClosest).addClass(clsActive);
2016-01-30 20:28:43 +00:00
} else {
navitems = links.removeClass(clsActive).filter("a[href='#"+target.attr("id")+"']").addClass(clsActive);
}
2017-11-12 00:33:10 +00:00
$this.element.trigger('inview.uk.scrollspynav', [target, navitems]);
2016-01-30 20:28:43 +00:00
}
};
if (this.options.smoothscroll && UI.smoothScroll) {
links.each(function(){
UI.smoothScroll(this, $this.options.smoothscroll);
});
}
fn();
2017-11-12 00:33:10 +00:00
this.element.data('scrollspynav', this);
2016-01-30 20:28:43 +00:00
this.check = fn;
scrollspynavs.push(this);
}
});
2017-11-12 00:33:10 +00:00
})(UIkit2);