幻灯片显示无法正常工作

时间:2016-08-12 10:30:43

标签: javascript html

我正在建立一个网站,其中在标题div我要添加幻灯片。我的代码拍摄并显示第一个图像,但滑块不起作用我将所有图像放在图像文件夹中.html文件是

<html>
<head>
    <title>slideshow</title>
</head>
<body>
<img scr ="image/slider1.jpg" id = "img"/>
<script language="javascript" type="text/javascript" scr="slideshow.js"></script>
</body>

和我的.js文件就是这个

var myImage = doucoment.getElementById("img");
var ImageArray = ["slider1.jpg", "slider2.jpg", "slider3.jpg"];
ImageIndex = 0;
function changeImage() {
    img.setAttribute("src", ImageArray[ImageIndex]);
    ImageIndex++;
    if (ImageIndex >= ImageArray.length){
        ImageIndex = 0;
    }
}
var ImageHandle = setInterval(changeImage, 3000);

3 个答案:

答案 0 :(得分:0)

您的HTMLJavaScript中有拼写错误 ..

更改

scrsrc

doucomentdocument

以下是经过修改的代码段

&#13;
&#13;
  var myImage = document.getElementById("img");
         //   ^^^^^^^ typo here.
  
    var ImageArray = ["https://www.google.com.pk/logos/doodles/2016/2016-doodle-fruit-games-day-8-5666133911797760.3-hp.gif", "http://reservations.life/css/images/bg-01.jpg"];
    ImageIndex = 0;
    function changeImage(){
    img.setAttribute("src", ImageArray[ImageIndex]);
    ImageIndex++;
    if(ImageIndex >= ImageArray.length){
    ImageIndex = 0;
  }
}
    var ImageHandle = setInterval(changeImage,3000);
&#13;
img{
  width:400px;
  height:200px;
}
&#13;
//   ||| typo here.
<img src ="https://www.google.com.pk/logos/doodles/2016/2016-doodle-fruit-games-day-8-5666133911797760.3-hp.gif" id = "img"/>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide,2000);

function nextSlide() {
    slides[currentSlide].className = 'slide';
    currentSlide = (currentSlide+1)%slides.length;
    slides[currentSlide].className = 'slide showing';
}
#slides {
    position: relative;
    height: 300px;
    padding: 0px;
    margin: 0px;
    list-style-type: none;
}

.slide {
    position: absolute;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    opacity: 0;
    z-index: 1;

    -webkit-transition: opacity 1s;
    -moz-transition: opacity 1s;
    -o-transition: opacity 1s;
    transition: opacity 1s;
}

.showing {
    opacity: 1;
    z-index: 2;
}
<ul id="slides">
    <li class="slide showing">Slide 1</li>
    <li class="slide">Slide 2</li>
    <li class="slide">Slide 3</li>
    <li class="slide">Slide 4</li>
    <li class="slide">Slide 5</li>
</ul>

答案 2 :(得分:0)

HTML:

scr ="image/slider1.jpg"
scr="slideshow.js"
转过来&#39; scr&#39;进入&#39; src&#39;

JS:

doucoment.getElementById("img")
转身&#39; doucoment&#39;进入&#39;文件&#39;