首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作多张图片的JS幻灯片?

问如何制作多张图片的JS幻灯片?
EN

Stack Overflow用户
提问于 2019-01-14 11:55:07
回答 1查看 56关注 0票数 0

我目前有一个‘功能’的JS幻灯片/轮播,但我有一个问题,我有一个非常宽的幻灯片容器,它一次只能显示一个缩略图。

我想创建一组8个默认图像,显示在一次。如果你点击箭头,我希望它能再显示8张图片。除此之外,我希望它能告诉您您正在查看的图像。(例如:显示图像1到8)

这个用红色圈出的区域是我的整个幻灯片容器

​

​

但是你可以看到它只显示了一个图像和一个箭头。

我如何才能一次显示更多的图像,并根据需要继续?

代码语言:javascript
复制
 <div class="w3-content w3-display-container">
      <?php foreach ($imageResult as $im): ?>
        <?php if($im['type'] == 'content'){?>

          <img class="mySlides" src="<?php echo $im['url']; ?>" style="max-width:200px; max-height:200px;">

        <?php } ?>
     <?php endforeach?>
    <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
    <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1)">&#10095;</button>
  </div>

 <script>
 var slideIndex = 1;
 showDivs(slideIndex);

 function plusDivs(n) {
 showDivs(slideIndex += n);
 }

 function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
  }
  x[slideIndex-1].style.display = "block";  
 }
 </script>
EN

回答 1

Stack Overflow用户

发布于 2019-01-14 14:58:54

也许可以试试这样的东西:

代码语言:javascript
复制
 <script>
 var slideIndex = 1; // This may skip the first image... maybe change to 0?

 showDivs(slideIndex);

 function plusDivs(n) {
  showDivs(slideIndex += n);
 }

 function showDivs(n) {
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}

  // Assuming we can show the next 8 images...
  var maxCount = slideIndex + 8;

  // However, there may not be 8 more images... 
  if(slideIndex + 8 > x.length){
    maxCount = slideIndex + (x.length - slideIndex);
  }

  // Hide all images...
  x.style.display = "none";   

  // Show the next 8 or so images...
  for (i = slideIndex; i <= maxCount; i++) {
    x[i].style.display = "block";
  } 
 }
 </script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54175597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档