This commit is contained in:
66
js/index.js
66
js/index.js
@@ -30,10 +30,10 @@ $(function () {
|
||||
if (urlParams.recommend_c) {
|
||||
setCookie("recommend_c", urlParams.recommend_c);
|
||||
}
|
||||
}
|
||||
}
|
||||
isRecommend();
|
||||
let bannerSwiper = null;
|
||||
let bannerData = [];
|
||||
let bannerTextTimer = null;
|
||||
|
||||
const updateBannerText = (index) => {
|
||||
if (!Array.isArray(bannerData) || bannerData.length === 0) {
|
||||
@@ -57,59 +57,35 @@ $(function () {
|
||||
}
|
||||
};
|
||||
|
||||
const initBannerSwiper = () => {
|
||||
if (bannerSwiper) {
|
||||
bannerSwiper.destroy(true, true);
|
||||
}
|
||||
bannerSwiper = new Swiper(".swiper", {
|
||||
loop: true,
|
||||
autoplay: true,
|
||||
pagination: {
|
||||
el: ".swiper-pagination",
|
||||
clickable: true,
|
||||
},
|
||||
on: {
|
||||
slideChange: function () {
|
||||
// realIndex 对应原始数据下标
|
||||
updateBannerText(this.realIndex || 0);
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const renderBannerSlides = (list = []) => {
|
||||
const wrapper = document.getElementById("bannerWrapper");
|
||||
if (!wrapper) {
|
||||
// 简单的文案轮播(不再使用 Swiper 图片,仅轮播标题/描述/按钮)
|
||||
const startBannerTextLoop = () => {
|
||||
if (!Array.isArray(bannerData) || bannerData.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(list) || list.length === 0) {
|
||||
initBannerSwiper();
|
||||
let current = 0;
|
||||
// 初始化先展示第一条
|
||||
updateBannerText(current);
|
||||
// 清理旧定时器
|
||||
if (bannerTextTimer) {
|
||||
clearInterval(bannerTextTimer);
|
||||
bannerTextTimer = null;
|
||||
}
|
||||
// 只有一条时不需要轮播
|
||||
if (bannerData.length === 1) {
|
||||
return;
|
||||
}
|
||||
wrapper.innerHTML = "";
|
||||
list.forEach((item) => {
|
||||
const slide = document.createElement("div");
|
||||
slide.className = "swiper-slide";
|
||||
const targetAttr = item.blank ? "_blank" : "_self";
|
||||
const imgHtml = `<img class="img-responsive img center-block" src="${
|
||||
item.img || ""
|
||||
}" alt="${item.alt || item.title || "banner"}">`;
|
||||
const mediaBlock = item.url
|
||||
? `<a href="${item.url}" target="${targetAttr}">${imgHtml}</a>`
|
||||
: imgHtml;
|
||||
slide.innerHTML = mediaBlock;
|
||||
wrapper.appendChild(slide);
|
||||
});
|
||||
initBannerSwiper();
|
||||
bannerTextTimer = setInterval(() => {
|
||||
current = (current + 1) % bannerData.length;
|
||||
updateBannerText(current);
|
||||
}, 5000);
|
||||
};
|
||||
|
||||
// 设置首页函数
|
||||
function setIndexData() {
|
||||
const commentObj = JSON.parse(sessionStorage.commentData);
|
||||
bannerData = commentObj.banner || [];
|
||||
renderBannerSlides(bannerData);
|
||||
// 初始化时使用第一张轮播图的文案
|
||||
updateBannerText(0);
|
||||
// 仅轮播文案
|
||||
startBannerTextLoop();
|
||||
if (commentObj.honor.length > 0) {
|
||||
commentObj.honor.forEach((item) => {
|
||||
$("#certBox").append(`<div class="cert-item">
|
||||
|
||||
Reference in New Issue
Block a user