| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function start() { |
| 5 |
// The bar is printed on wp_footer, which can run after this script: |
| 6 |
// wait for the document before looking for it. |
| 7 |
var bar = document.querySelector('.king-addons-satc'); |
| 8 |
if (!bar) { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
var cfg = window.kingAddonsStickyAddToCart || {}; |
| 13 |
var offset = parseInt(cfg.offset, 10); |
| 14 |
if (isNaN(offset)) { |
| 15 |
offset = 400; |
| 16 |
} |
| 17 |
|
| 18 |
// Prefer the real add-to-cart form: the bar earns its place exactly when |
| 19 |
// the buy button has scrolled out of reach. Without a form on the page, |
| 20 |
// fall back to a plain scroll offset. |
| 21 |
var form = document.querySelector('form.cart, .ka-woo-product-atc, #ka-satc-form-anchor'); |
| 22 |
|
| 23 |
// Variable products link the bar to #ka-satc-form-anchor. That id is |
| 24 |
// printed by the Woo Add To Cart widget; a variations form alone never |
| 25 |
// had it, so the button jumped nowhere. |
| 26 |
if (form && !document.getElementById('ka-satc-form-anchor')) { |
| 27 |
form.id = 'ka-satc-form-anchor'; |
| 28 |
} |
| 29 |
|
| 30 |
var button = bar.querySelector('.king-addons-satc__button'); |
| 31 |
if (button && (button.getAttribute('href') || '').indexOf('#ka-satc-form-anchor') === 0) { |
| 32 |
button.addEventListener('click', function (event) { |
| 33 |
var target = document.getElementById('ka-satc-form-anchor') || form; |
| 34 |
if (!target) { |
| 35 |
return; |
| 36 |
} |
| 37 |
event.preventDefault(); |
| 38 |
target.scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
bar.hidden = false; |
| 43 |
|
| 44 |
function update() { |
| 45 |
var visible; |
| 46 |
|
| 47 |
if (form) { |
| 48 |
visible = form.getBoundingClientRect().bottom < 0; |
| 49 |
} else { |
| 50 |
visible = window.pageYOffset > offset; |
| 51 |
} |
| 52 |
|
| 53 |
bar.classList.toggle('is-visible', visible); |
| 54 |
} |
| 55 |
|
| 56 |
var ticking = false; |
| 57 |
function onScroll() { |
| 58 |
if (ticking) { |
| 59 |
return; |
| 60 |
} |
| 61 |
ticking = true; |
| 62 |
window.requestAnimationFrame(function () { |
| 63 |
update(); |
| 64 |
ticking = false; |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
window.addEventListener('scroll', onScroll, { passive: true }); |
| 69 |
window.addEventListener('resize', onScroll, { passive: true }); |
| 70 |
update(); |
| 71 |
} |
| 72 |
|
| 73 |
if ('loading' === document.readyState) { |
| 74 |
document.addEventListener('DOMContentLoaded', start); |
| 75 |
} else { |
| 76 |
start(); |
| 77 |
} |
| 78 |
})(); |
| 79 |
|