| 1 |
/* Scroll To Top — frontend */ |
| 2 |
( function () { |
| 3 |
function init() { |
| 4 |
document.querySelectorAll('.bkstt-wrap[data-trigger]').forEach(function(wrap) { |
| 5 |
if (wrap._bksttInit) return; |
| 6 |
wrap._bksttInit = true; |
| 7 |
|
| 8 |
var btn = wrap.querySelector('.bkstt-btn'); |
| 9 |
if (!btn) return; |
| 10 |
|
| 11 |
var trigger = parseInt(wrap.getAttribute('data-trigger'),10) || 400; |
| 12 |
var smooth = wrap.getAttribute('data-smooth') !== '0'; |
| 13 |
var ticking = false; |
| 14 |
|
| 15 |
function update() { |
| 16 |
var scrollY = window.scrollY || window.pageYOffset; |
| 17 |
if (scrollY > trigger) { |
| 18 |
wrap.classList.add('bkstt-visible'); |
| 19 |
} else { |
| 20 |
wrap.classList.remove('bkstt-visible'); |
| 21 |
} |
| 22 |
ticking = false; |
| 23 |
} |
| 24 |
|
| 25 |
window.addEventListener('scroll', function() { |
| 26 |
if (!ticking) { |
| 27 |
requestAnimationFrame(update); |
| 28 |
ticking = true; |
| 29 |
} |
| 30 |
}, {passive:true}); |
| 31 |
|
| 32 |
btn.addEventListener('click', function() { |
| 33 |
window.scrollTo({ top: 0, behavior: smooth ? 'smooth' : 'auto' }); |
| 34 |
}); |
| 35 |
|
| 36 |
// Initial check |
| 37 |
update(); |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
if (document.readyState === 'loading') { |
| 42 |
document.addEventListener('DOMContentLoaded', init); |
| 43 |
} else { |
| 44 |
init(); |
| 45 |
} |
| 46 |
}() ); |
| 47 |
|