javascript - 如何创建 2 个并排自动播放的图像幻灯片

标签 javascript jquery html css

我想在旁边添加另一个包含不同图片的幻灯片。

我的图片有不同的尺寸。

<html>
<head>
    <style>
        .container{
            position:relative;
            width:100%;
            height:300px;
            border-radius:5px;
            border:1px solid red;
            overflow:hidden;
        }
    </style>
</head>
<body>
<div id="imgGallary" class="container">
    <img src="http://www.examiningcalvinism.com/kingdavid.jpg" alt=""           width="100%" height="300" />
    <img src="http://www.kingjamesbibleonline.org/1611-Bible/1611-King-James-Bible-Introduction.jpg" alt="" width="100%" height="300" />
    <img src="http://biblestudyoutlines.org/wp-content/uploads/2012/07/the-triump-of-david-over-king-hadadezer-of-zobah-1024x729.jpg" alt="" width="100%"   height="300" />
    <img src="http://www.cgu.edu/Images/news/Flame%20Winter12/KJV-BibleBW.jpg" alt="" width="100%" height="300" />
</div>
<script>
(function(){
        var imgLen = document.getElementById('imgGallary');
        var images = imgLen.getElementsByTagName('img');
        var counter = 1;

        if(counter <= images.length){
            setInterval(function(){
                images[0].src = images[counter].src;
                console.log(images[counter].src);
                counter++;

                if(counter === images.length){
                    counter = 1;
                }
            },4000);
        }
})();
</script>
</body>
</html>

最佳答案

这里的关键概念是幻灯片是一个包含一些图像的区域,该区域可以在页面的任何地方重复,但 JavaScript 代码应该在特定区域工作。下面的代码显示了一些可能的解决方案之一。

 (function () {
    function slideshow(container) {
        var imgLen = document.getElementById(container);
        var images = imgLen.getElementsByTagName('img');
        var counter = 1;

        if (counter <= images.length) {
            setInterval(function () {
                images[0].src = images[counter].src;
                console.log(images[counter].src);
                counter++;

                if (counter === images.length) {
                    counter = 1;
                }
            }, 4000);
        }
    }

    slideshow('imgGallary1');
    slideshow('imgGallary2');
})();
 .container {
   display: inline-flex;
 }
 .sildeshow {
   position: relative;
   width: 45%;
   height: 300px;
   border-radius: 5px;
   border: 1px solid red;
   overflow: hidden;
 }
<div class="container">
  <div id="imgGallary1" class="sildeshow">
    <img src="http://www.examiningcalvinism.com/kingdavid.jpg" alt="" width="100%" height="300" />
    <img src="http://www.kingjamesbibleonline.org/1611-Bible/1611-King-James-Bible-Introduction.jpg" alt="" width="100%" height="300" />
    <img src="http://biblestudyoutlines.org/wp-content/uploads/2012/07/the-triump-of-david-over-king-hadadezer-of-zobah-1024x729.jpg" alt="" width="100%" height="300" />
    <img src="http://www.cgu.edu/Images/news/Flame%20Winter12/KJV-BibleBW.jpg" alt="" width="100%" height="300" />
  </div>
  <div id="imgGallary2" class="sildeshow">
    <img src="http://www.examiningcalvinism.com/kingdavid.jpg" alt="" width="100%" height="300" />
    <img src="http://www.kingjamesbibleonline.org/1611-Bible/1611-King-James-Bible-Introduction.jpg" alt="" width="100%" height="300" />
    <img src="http://biblestudyoutlines.org/wp-content/uploads/2012/07/the-triump-of-david-over-king-hadadezer-of-zobah-1024x729.jpg" alt="" width="100%" height="300" />
    <img src="http://www.cgu.edu/Images/news/Flame%20Winter12/KJV-BibleBW.jpg" alt="" width="100%" height="300" />
  </div>
</div>

关于javascript - 如何创建 2 个并排自动播放的图像幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34457055/

相关文章:

javascript - 用图像替换 SVG

javascript - 将带有resolve和reject的promise转换为异步函数的正确方法(puppeteer)

jquery - 简单的php ajax请求url

html - 为什么这个 Twitter 关注按钮不会下移?

不同浏览器中的javascript Ajax方法有不同的结果

javascript - 在具有 Opera Mini 的功能手机上,选择要上传的文件是否可以在没有 JavaScript 的情况下触发 UI 更改?

javascript - 更改单击下拉菜单时打开的图像

Jquery 验证日期范围失败

php - websocket的服务器端需要什么?

html - Phone Gap 中输入字段表单的问题