112 lines
3.8 KiB
JavaScript
112 lines
3.8 KiB
JavaScript
/**
|
|
* صفحات ماژول — انیمیشن اسکرول و شمارنده
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function prefersReducedMotion() {
|
|
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
}
|
|
|
|
function initReveal() {
|
|
var els = document.querySelectorAll('.hx-reveal');
|
|
if (!els.length) {
|
|
return;
|
|
}
|
|
if (prefersReducedMotion() || !('IntersectionObserver' in window)) {
|
|
els.forEach(function (el) {
|
|
el.classList.add('is-visible');
|
|
});
|
|
document.querySelectorAll('.hx-rich-chart__panel').forEach(function (p) {
|
|
p.classList.add('is-visible');
|
|
});
|
|
document.querySelectorAll('.hx-rich-flow__connector').forEach(function (c) {
|
|
c.classList.add('is-visible');
|
|
});
|
|
return;
|
|
}
|
|
var io = new IntersectionObserver(
|
|
function (entries) {
|
|
entries.forEach(function (entry) {
|
|
if (!entry.isIntersecting) {
|
|
return;
|
|
}
|
|
entry.target.classList.add('is-visible');
|
|
io.unobserve(entry.target);
|
|
});
|
|
},
|
|
{ root: null, rootMargin: '0px 0px -8% 0px', threshold: 0.12 }
|
|
);
|
|
els.forEach(function (el) {
|
|
io.observe(el);
|
|
});
|
|
document.querySelectorAll('.hx-rich-chart__panel').forEach(function (panel) {
|
|
io.observe(panel);
|
|
});
|
|
document.querySelectorAll('.hx-rich-flow__connector').forEach(function (conn) {
|
|
io.observe(conn);
|
|
});
|
|
}
|
|
|
|
function initCounters() {
|
|
if (prefersReducedMotion()) {
|
|
return;
|
|
}
|
|
var counters = document.querySelectorAll('.hx-rich-counter');
|
|
if (!counters.length || !('IntersectionObserver' in window)) {
|
|
return;
|
|
}
|
|
var io = new IntersectionObserver(
|
|
function (entries) {
|
|
entries.forEach(function (entry) {
|
|
if (!entry.isIntersecting) {
|
|
return;
|
|
}
|
|
var el = entry.target;
|
|
var target = parseInt(el.getAttribute('data-target') || '0', 10);
|
|
if (!target || target > 500) {
|
|
io.unobserve(el);
|
|
return;
|
|
}
|
|
var start = 0;
|
|
var duration = 1200;
|
|
var startTime = null;
|
|
function step(ts) {
|
|
if (!startTime) {
|
|
startTime = ts;
|
|
}
|
|
var p = Math.min(1, (ts - startTime) / duration);
|
|
var eased = 1 - Math.pow(1 - p, 3);
|
|
el.textContent = String(Math.round(start + (target - start) * eased));
|
|
if (p < 1) {
|
|
requestAnimationFrame(step);
|
|
} else {
|
|
el.textContent = String(target);
|
|
}
|
|
}
|
|
requestAnimationFrame(step);
|
|
io.unobserve(el);
|
|
});
|
|
},
|
|
{ threshold: 0.3 }
|
|
);
|
|
counters.forEach(function (c) {
|
|
var t = parseInt(c.getAttribute('data-target') || '0', 10);
|
|
if (t > 0 && t <= 500) {
|
|
c.textContent = '0';
|
|
io.observe(c);
|
|
}
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
initReveal();
|
|
initCounters();
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|