<!-- https://mkotelnikov.ru/script/running-text -->
<script>
var running_line = {
data: {
timeStep: 10,
realX: 0,
pause: false,
timer: null,
selector: null,
time: null,
deltaX: null,
elements: null,
width_type: 4,
width_updated: false,
},
f_init: function (selector = '.running-line',
deltaX = [],
time = []) {
this.data.selector = selector.trim();
this.data.time = time;
this.data.deltaX = deltaX;
this.data.elements = document.querySelectorAll(this.data.selector + " .tn-atom");
// Слушатели
this.f_listeners();
// Первая итерация
this.f_iterate();
// Итерации по времени
this.data.timer = setInterval(
() => {
this.f_iterate();
}, this.data.timeStep
);
},
f_iterate: function () {
// Получение ширины экрана и установка типа
let w = $(window).width();
if (w >= 1200) {
if (this.data.width_type != 4) {
this.data.width_updated = false;
width_type = 4;
this.f_listeners();
}
} else if (w >= 960) {
if (this.data.width_type != 3) {
this.data.width_updated = false;
width_type = 3;
this.f_listeners();
}
} else if (w >= 640) {
if (this.data.width_type != 2) {
this.data.width_updated = false;
width_type = 2;
this.f_listeners();
}
} else if (w >= 480) {
if (this.data.width_type != 1) {
this.data.width_updated = false;
width_type = 1;
this.f_listeners();
}
} else {
if (this.data.width_type != 0) {
this.data.width_updated = false;
width_type = 0;
this.f_listeners();
}
}
// Пауза, если наведена мышь
if (running_line.data.pause && this.data.width_updated) return;
this.data.width_updated = true;
if (!running_line.data.pause) {
// Рассчёт сдвига
this.data.realX = (this.data.realX + (this.data.deltaX[this.data.width_type] * this.data.timeStep / this.data.time[this.data.width_type])) % this.data.deltaX[this.data.width_type];
// Установка сдвига для каждого элемента
this.data.elements.forEach((element) => {
element.style.transform = "translateX(" + this.data.realX + "px)"
});
}
},
f_listeners: function () {
this.data.elements.forEach((element) => {
element.removeEventListener('mouseover', this.f_over);
element.removeEventListener('mouseout', this.f_out);
element.addEventListener('mouseover', this.f_over);
element.addEventListener('mouseout', this.f_out);
});
},
f_over: function () {
running_line.data.pause = true;
},
f_out: function () {
running_line.data.pause = false;
},
};
</script>