gpt4 book ai didi

Javascript幻灯片数组问题

转载 作者:行者123 更新时间:2023-12-03 04:08:29 25 4
gpt4 key购买 nike

我对 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. 这里有奇怪的变量声明

    var = 图片=[];

  2. 您还需要移动这部分

    var startSlide = function() {
    setInterval(cycleImages, defaultSettings.sliderSpeed); // Set interval
    };

    之前

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

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

首先尝试调试您的代码

关于Javascript幻灯片数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44437781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com