首页 > web前端 > js教程 > 正文

随机显示轮播图中的指定数量幻灯片

DDD
发布: 2025-08-06 19:44:25
原创
823人浏览过

随机显示轮播图中的指定数量幻灯片

第一段引用上面的摘要:

本文旨在提供一种解决方案,用于在网页加载时从一组幻灯片中随机选择并显示指定数量的幻灯片,同时隐藏未被选中的幻灯片。通过使用JavaScript和CSS,可以实现动态地展示幻灯片内容,提升用户体验。文章将提供详细的代码示例和步骤说明,帮助开发者快速实现该功能。

实现步骤

  1. HTML 结构准备:

首先,确保你的 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>
登录后复制
  1. CSS 样式定义:

默认情况下,隐藏所有幻灯片。然后,创建一个 CSS 类(例如 show)来显示选定的幻灯片。

来画数字人直播
来画数字人直播

来画数字人自动化直播,无需请真人主播,即可实现24小时直播,无缝衔接各大直播平台。

来画数字人直播0
查看详情 来画数字人直播
.slogan {
  display: none;
}

.slogan.show {
  display: block;
}
登录后复制
  1. JavaScript 代码实现:

使用 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))
  );
}
登录后复制

代码解释:

  • getRandomNumber(count): 生成一个 0 到 count-1 之间的随机整数。
  • randomNumbers(len, count): 生成一个包含 len 个不重复的随机整数的数组,这些整数的范围是 0 到 count-1。使用 Set 数据结构确保生成的数字不重复。
  • document.querySelectorAll('.slogan'): 获取所有类名为 slogan 的元素,并将结果转换为数组。
  • slogans.filter(slogan => slogan.textContent.trim() !== ''): 过滤掉内容为空的幻灯片。
  • randomNumbers(3, nonEmptySlogans.length): 生成一个包含 3 个随机索引的数组,这些索引对应于要显示的幻灯片。
  • slogans.forEach((slogan, i) => slogan.classList.toggle('show', showList.includes(i))): 遍历所有幻灯片,如果当前幻灯片的索引包含在 showList 数组中,则添加 show 类,否则移除 show 类。 classList.toggle() 方法可以根据第二个参数的布尔值来添加或移除指定的类名。

完整示例

<!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>
登录后复制

注意事项

  • 确保 JavaScript 代码在 DOM 加载完成后执行。可以将代码放在 </body> 标签之前,或者使用 DOMContentLoaded 事件监听器。
  • 可以根据需要调整随机选择的幻灯片数量。修改 randomNumbers(3, nonEmptySlogans.length) 中的第一个参数即可。
  • 如果幻灯片内容是从服务器动态加载的,需要在内容加载完成后再执行 JavaScript 代码。

总结

通过结合 HTML、CSS 和 JavaScript,可以轻松实现随机显示指定数量幻灯片的功能。这种方法可以用于创建动态的、个性化的用户体验,例如在网站首页随机展示不同的产品或服务。 开发者可以根据自己的需求调整代码,例如添加动画效果、自定义样式等,以实现更丰富的效果。

以上就是随机显示轮播图中的指定数量幻灯片的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号