hesabixWpTheme/assets/js/helo-page.js

208 lines
5.6 KiB
JavaScript

/**
* صفحهٔ مهاجرت از هلو — FAQ، reveal، روتیتور نقل‌قول، شمارنده
*/
(function () {
'use strict';
function toPersianDigits(str) {
return String(str).replace(/\d/g, function (d) {
return '۰۱۲۳۴۵۶۷۸۹'[d];
});
}
function initFaq() {
var items = document.querySelectorAll('.hx-helo-faq__item');
items.forEach(function (item) {
var btn = item.querySelector('.hx-helo-faq__q');
var panel = item.querySelector('.hx-helo-faq__a');
if (!btn || !panel) return;
btn.addEventListener('click', function () {
var open = btn.getAttribute('aria-expanded') === 'true';
items.forEach(function (other) {
var ob = other.querySelector('.hx-helo-faq__q');
var op = other.querySelector('.hx-helo-faq__a');
if (!ob || !op) return;
ob.setAttribute('aria-expanded', 'false');
op.hidden = true;
});
if (!open) {
btn.setAttribute('aria-expanded', 'true');
panel.hidden = false;
}
});
});
}
function initReveal() {
var nodes = document.querySelectorAll('.hx-helo-reveal');
if (!nodes.length) return;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
nodes.forEach(function (n) {
n.classList.add('is-visible');
});
return;
}
if (!('IntersectionObserver' in window)) {
nodes.forEach(function (n) {
n.classList.add('is-visible');
});
return;
}
var io = new IntersectionObserver(
function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add('is-visible');
io.unobserve(entry.target);
}
});
},
{ rootMargin: '0px 0px -8% 0px', threshold: 0.12 }
);
nodes.forEach(function (n) {
io.observe(n);
});
}
function initQuotes() {
var stage = document.querySelector('[data-helo-quotes]');
if (!stage) return;
var lines = Array.prototype.slice.call(stage.querySelectorAll('[data-helo-quote]'));
var dots = Array.prototype.slice.call(document.querySelectorAll('[data-helo-quote-go]'));
if (lines.length < 2) return;
var index = 0;
var timer = null;
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
function show(i) {
index = (i + lines.length) % lines.length;
lines.forEach(function (line, li) {
line.classList.toggle('is-active', li === index);
});
dots.forEach(function (dot, di) {
var on = di === index;
dot.classList.toggle('is-active', on);
dot.setAttribute('aria-selected', on ? 'true' : 'false');
});
}
function next() {
show(index + 1);
}
function start() {
if (reduce || timer) return;
timer = window.setInterval(next, 4500);
}
function stop() {
if (!timer) return;
window.clearInterval(timer);
timer = null;
}
dots.forEach(function (dot) {
dot.addEventListener('click', function () {
var go = parseInt(dot.getAttribute('data-helo-quote-go'), 10);
if (isNaN(go)) return;
stop();
show(go);
start();
});
});
var root = stage.closest('.hx-helo-quotes');
if (root) {
root.addEventListener('mouseenter', stop);
root.addEventListener('mouseleave', start);
root.addEventListener('focusin', stop);
root.addEventListener('focusout', start);
}
show(0);
start();
}
function initCounters() {
var nodes = document.querySelectorAll('[data-helo-count]');
if (!nodes.length) return;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
nodes.forEach(function (el) {
var target = parseFloat(el.getAttribute('data-helo-count'));
var suffix = el.getAttribute('data-helo-suffix') || '';
if (isNaN(target)) return;
var text = Number.isInteger(target) ? String(target) : target.toFixed(1);
el.textContent = toPersianDigits(text) + suffix;
});
return;
}
function animate(el) {
var target = parseFloat(el.getAttribute('data-helo-count'));
var suffix = el.getAttribute('data-helo-suffix') || '';
if (isNaN(target)) return;
var duration = 1200;
var start = null;
var isInt = Number.isInteger(target);
function frame(ts) {
if (start === null) start = ts;
var t = Math.min(1, (ts - start) / duration);
var eased = 1 - Math.pow(1 - t, 3);
var value = target * eased;
var text = isInt ? String(Math.round(value)) : value.toFixed(1);
el.textContent = toPersianDigits(text) + suffix;
if (t < 1) {
window.requestAnimationFrame(frame);
}
}
window.requestAnimationFrame(frame);
}
if (!('IntersectionObserver' in window)) {
nodes.forEach(animate);
return;
}
var io = new IntersectionObserver(
function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
animate(entry.target);
io.unobserve(entry.target);
}
});
},
{ threshold: 0.4 }
);
nodes.forEach(function (n) {
io.observe(n);
});
}
function ready(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
ready(function () {
if (!document.querySelector('.hx-helo')) return;
initFaq();
initReveal();
initQuotes();
initCounters();
});
})();