Это более безопасный вариант переключателя: он не вмешивается в логику других скриптов, при этом дизайн самого переключателя остаётся прежним.
Код
$(function () {
const selector = $("#selector");
const bodyBox = $("#ajaxBody");
if (!bodyBox.length || !selector.length) return;
if (!selector.text().includes("»")) return;
let loading = false;
// Создание кнопки под всеми материалами (мой оригинальный HTML)
function addButton() {
$("#nextCont").remove(); // убираем старую кнопку если есть
bodyBox.append(`<div id="nextCont"><div class="btn-load">Подгрузить материалы</div></div>`);
}
addButton(); // создаём первую кнопку
// Клик по кнопке
$(document).on("click", "#nextCont .btn-load", function () {
if (loading) return;
loading = true;
$("#nextCont").html('<img src="https://bro.usite.pro/img/load.gif">');
let nextLink = selector.find("a").last().attr("href");
// fallback для старого uCoz (onclick)
if (!nextLink) {
let onclick = selector.find(".swchItem:contains('»')").attr("onclick");
if (!onclick) {
$("#nextCont").fadeOut();
loading = false;
return;
}
let num = onclick.match(/\d+/);
if (!num) {
$("#nextCont").fadeOut();
loading = false;
return;
}
nextLink = "/load/0-" + num[0];
}
$.get(nextLink, function (data) {
// Создаём объект из ответа
const $response = $("<div>").html(data);
// ────────────────────────────────────────────────
// ЗАЩИТА: удаляем дублирующиеся блоки ДО вставки
// ────────────────────────────────────────────────
$response.find("#user-photos").remove();
$response.find("#vkPhotosCount").remove();
$response.find(".vk-photos-header").remove();
$response.find(".vk-modern-post1").remove(); // если дублируется весь блок
$response.find(".bro-actions").remove(); // твои кнопки действий
// Очищенные новые материалы
const newItems = $response.find("#ajaxBody").children();
if (!newItems.length) {
$("#nextCont").fadeOut();
loading = false;
return;
}
// clearfix и анимация
newItems.addClass("clearfix").css({ opacity: 0 });
bodyBox.append(newItems);
newItems.animate({ opacity: 1 }, 300);
// обновляем пагинацию
selector.html($response.find("#selector").html());
// создаём кнопку снова или скрываем
if (selector.text().includes("»")) {
addButton();
} else {
$("#nextCont").fadeOut();
}
loading = false;
}).fail(function () {
// если запрос упал — возвращаем кнопку
$("#nextCont").html('<div class="btn-load">Подгрузить материалы</div>');
loading = false;
});
});
});
Просто можете вставить
Код
<!-- Скрытая пагинация -->
<div id="selector" style="display:none;">
$PAGE_SELECTOR$
</div>
<script src="https://bro.usite.pro/js/ajaxfotonew.js"></script>