
本文旨在提供一种使用 JavaScript 从轮播图中随机选择并显示指定数量 slides 的方法。通过修改 HTML 结构和添加 CSS 样式,并结合 JavaScript 代码,可以实现每次页面加载时,轮播图随机展示预设数量 slides 的效果,而其余 slides 则会被隐藏。本文将提供详细的代码示例和步骤说明,帮助开发者轻松实现这一功能。
确保 HTML 结构包含一个容器元素(例如 <div>)包裹所有的 slide 元素(例如 <div> 或 <li>)。每个 slide 元素都应该有一个唯一的类名(例如 slogan)。
<div class="container">
<section id="testim" class="testim">
<div class="testim-cover">
<div class="wrap">
<span id="right-arrow" class="arrow right fa fa-chevron-right"></span>
<span id="left-arrow" class="arrow left fa fa-chevron-left "></span>
<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>首先,将所有 slide 元素默认隐藏。然后,创建一个 CSS 类(例如 show),用于显示选中的 slide 元素。
.slogan {
display: none;
}
.slogan.show {
display: block;
}使用 JavaScript 代码实现以下功能:
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); // get 3 of how many found
slogans.forEach((slogan,i) => slogan.classList.toggle("show",showList.contains(i)))
}代码解释:
将上述 HTML、CSS 和 JavaScript 代码整合在一起,即可实现随机展示轮播图中的指定数量 slides 的功能。
<!DOCTYPE html>
<html>
<head>
<title>Random Slides</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">
<span id="right-arrow" class="arrow right fa fa-chevron-right"></span>
<span id="left-arrow" class="arrow left fa fa-chevron-left "></span>
<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>注意事项:
通过本文的介绍,您已经掌握了如何使用 JavaScript 从轮播图中随机选择并显示指定数量 slides 的方法。 这种方法可以应用于各种场景,例如展示随机推荐内容、突出显示特定主题等。 通过灵活运用这些技术,可以为用户提供更加个性化和动态的体验。
以上就是随机展示轮播图中的指定数量的 Slides的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号