
第一段引用上面的摘要:
本文旨在提供一种解决方案,用于在网页加载时从一组幻灯片中随机选择并显示指定数量的幻灯片,同时隐藏未被选中的幻灯片。通过使用JavaScript和CSS,可以实现动态地展示幻灯片内容,提升用户体验。文章将提供详细的代码示例和步骤说明,帮助开发者快速实现该功能。
首先,确保你的 HTML 结构中包含包含所有幻灯片的容器,以及每个幻灯片的元素(例如 div)。每个幻灯片都应该有一个唯一的类名,例如 slogan。
<div class="container">
<section id="testim" class="testim">
<div class="testim-cover">
<div class="wrap">
<ul id="testim-dots" class="dots">
<li class="dot active"></li>
<li class="dot"></li>
<li class="dot"></li>
<li class="dot"></li>
<li class="dot"></li>
</ul>
<div id="testim-content" class="cont">
<div class="slogan">
<p>"How does visual identity design help business/product value grow?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"How can we analyze ourselves, audience, competitors, and market and help business progress/grow?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"How can I differentiate my business from others?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"What is the best and latest business model and plan for me?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"How will innovative targeting be achieved at each stage of business?"</p>
<h2>MINE</h2>
</div>
</div>
</div>
</div>
</section>
</div>默认情况下,隐藏所有幻灯片。然后,创建一个 CSS 类(例如 show)来显示选定的幻灯片。
.slogan {
display: none;
}
.slogan.show {
display: block;
}使用 JavaScript 代码来随机选择幻灯片并应用 show 类。
const getRandomNumber = (count) => Math.floor(Math.random() * count);
const randomNumbers = (len, count) => {
const numbers = new Set();
while (numbers.size < len) numbers.add(getRandomNumber(count));
return [...numbers];
};
const slogans = [...document.querySelectorAll('.slogan')];
const nonEmptySlogans = slogans.filter(
(slogan) => slogan.textContent.trim() !== ''
);
if (nonEmptySlogans.length >= 3) {
const showList = randomNumbers(3, nonEmptySlogans.length); // get 3 of how many found
slogans.forEach((slogan, i) =>
slogan.classList.toggle('show', showList.includes(i))
);
}代码解释:
<!DOCTYPE html>
<html>
<head>
<title>随机幻灯片</title>
<style>
.slogan { display: none; }
.slogan.show { display: block; }
</style>
</head>
<body>
<div class="container">
<section id="testim" class="testim">
<div class="testim-cover">
<div class="wrap">
<ul id="testim-dots" class="dots">
<li class="dot active"></li>
<li class="dot"></li>
<li class="dot"></li>
<li class="dot"></li>
<li class="dot"></li>
</ul>
<div id="testim-content" class="cont">
<div class="slogan">
<p>"How does visual identity design help business/product value grow?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"How can we analyze ourselves, audience, competitors, and market and help business progress/grow?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"How can I differentiate my business from others?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"What is the best and latest business model and plan for me?"</p>
<h2>MINE</h2>
</div>
<div class="slogan">
<p>"How will innovative targeting be achieved at each stage of business?"</p>
<h2>MINE</h2>
</div>
</div>
</div>
</div>
</section>
</div>
<script>
const getRandomNumber = count => Math.floor(Math.random() * count);
const randomNumbers = (len, count) => {
const numbers = new Set();
while (numbers.size < len) numbers.add(getRandomNumber(count));
return [...numbers];
};
const slogans = [...document.querySelectorAll('.slogan')];
const nonEmptySlogans = slogans.filter(slogan => slogan.textContent.trim() !== '');
if (nonEmptySlogans.length >= 3) {
const showList = randomNumbers(3, nonEmptySlogans.length); // get 3 of how many found
slogans.forEach((slogan,i) => slogan.classList.toggle("show",showList.includes(i)))
}
</script>
</body>
</html>通过结合 HTML、CSS 和 JavaScript,可以轻松实现随机显示指定数量幻灯片的功能。这种方法可以用于创建动态的、个性化的用户体验,例如在网站首页随机展示不同的产品或服务。 开发者可以根据自己的需求调整代码,例如添加动画效果、自定义样式等,以实现更丰富的效果。
以上就是随机显示轮播图中的指定数量幻灯片的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号