开发者问题收集

Javascript 幻灯片数组问题

2017-06-08
53

我对 Javascript 还很陌生,现在想自己制作幻灯片。只是在我创建了从中获取图片的数组后,我现在卡住了。它只显示白屏。

我的 Javascript

 $(function () {
var counter = 0;
var defaultSettings = {
    "sliderContainer": "#slider"
    , "pauseWithMouse": true
    , "sliderSpeed": 2000
    , "transitionSpeed": 1500
};

function cycleImages() {
    counter++;
if (counter >= images.Length) {
    counter = 0;
}
document.getElementById("#slider img").src = MyImages[counter];

var images = $('#slider img')
    , now = images.filter(':visible')
    , next = now.next().length ? now.next() : images.first()
    , speed = 1500; //Transition speed
now.fadeOut(speed);
next.fadeIn(speed);
 }

var theInterval; // Slide speed

var = images = [];
// if myImages exists then
images = myImages;
if (images.length > 0) {
    $(defaultSettings.sliderContainer).append('<img id="sliderImage" src="' + images[0] + '" />');
    startSlide();
}
var startSlide = function () {
    setInterval(cycleImages, defaultSettings.sliderSpeed); // Set interval
};
var stopSlide = function () {
    if (defaultSettings.pauseWithMouse) {
        clearInterval(theInterval);
    }
};
startSlide();
$('#slider img').on('mouseover', function () {
    stopSlide();
});
$('#slider img').on('mouseout', function () {
    startSlide();
});
});

和我的 HTML:

  <!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Javascript Slider</title>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <link rel="stylesheet" href="css/style.css">
  <script src="js/slideshow.js"></script>

<script>
  var myImages = ["slide1.jpg", "slide2.jpg", "bg/slide3.jpg"];
 </script>
</head>

<body>
<main>
<div id="slider">
</div>
</main> 
</body>
</html> 
1个回答
  1. 此处变量声明很奇怪

    var = images = [];

  2. 还需要移动此部分

    var startSlide = function() {
    setInterval(cycleImages, defaultSettings.sliderSpeed); // 设置间隔
    };
    

    之前

    if (images.length > 0) {
    $(defaultSettings.sliderContainer).append('<img id="sliderImage" src="' + images[0] + '" />');
    startSlide();
    }
    
  3. 此处变量名称 (MyImages) 错误

    document.getElementById("#slider img").src = MyImages[counter];
    

首先尝试调试您的代码

Kirill Staruhin
2017-06-08