简体   繁体   English

JavaScript / jQuery图像滑块问题

[英]JavaScript/jQuery image slider issue

Fiddle: https://jsfiddle.net/8na2n5L2/ 小提琴: https//jsfiddle.net/8na2n5L2/

I'm trying to create an image slider, but for some reason just can't wrap my head around why this isn't working. 我正在尝试创建图像滑块,但是由于某些原因,无法解决为什么它不起作用的问题。 I'm basically trying to create a slider where images come in from the right (off screen), and the center image gets pushed to the left (off screen). 我基本上是在尝试创建一个滑块,其中图像从右侧(屏幕外)进入,而中心图像被推到左侧(屏幕外)。 Then, when it comes time for that left image to go to the center again, it quickly goes back to the right again before going to the center. 然后,当该左图像再次回到中心时,它又迅速回到右,然后再回到中心。

jQuery(document).ready (function () { jQuery(document).ready(function(){

var images = [];

//store the images in an array
jQuery('.main-image-slider').each( function () {
    images.push(jQuery(this));
});

var i = 0;
var max = images.length - 1;

setInterval( function () {
    if ( i > max) {
        i = 0;
    }

    images[i].removeClass('main-image-slider-left').addClass('main-image-slider-right').delay(100).queue(function () {
        images[i].removeClass('main-image-slider-right').addClass('main-image-slider-center').dequeue();
        i++;
    });


    if (images[i - 1]) {
        images[i - 1].removeClass('main-image-slider-center').addClass('main-image-slider-left');
    }


}, 3000);

}); });

I get this console error "Uncaught TypeError: Cannot read property 'removeClass' of undefined". 我收到此控制台错误“ Uncaught TypeError:无法读取未定义的属性'removeClass'”。

This error is because the code will run i++ first, then run images[i].removeClass('main-image-slider-right')... . 此错误是因为代码将首先运行i++ ,然后运行images[i].removeClass('main-image-slider-right')...

images[i].removeClass('main-image-slider-left').addClass('main-image-slider-right').delay(100).queue(function() {
  // the 'i' is already added 1, so it may equal images[max], so you got that error
  images[i].removeClass('main-image-slider-right').addClass('main-image-slider-center').dequeue();
});

You can move i++ into queue : 您可以将i++移入queue

images[i].removeClass('main-image-slider-left').addClass('main-image-slider-right').delay(100).queue(function() {
  images[i].removeClass('main-image-slider-right').addClass('main-image-slider-center').dequeue();
  // after all the animation is done, at the end of queue update 'i'. 
  i++;
});

You can try this. 你可以试试看

Note: don't forget to remove the i++ at the end of code. 注意:不要忘记在代码末尾删除i++

Update 更新资料

Update the jsfiddle code 更新jsfiddle代码

At the edge of slides it will not update the class with if (images[i - 1]) , wee need always update the class of previous slide. 在幻灯片的边缘,它将不会使用if (images[i - 1])更新类,因此我们需要始终更新前一张幻灯片的类。 So I changed the code to 所以我将代码更改为

images[i == 0 ? images.length - 1 : i - 1].removeClass('main-image-slider-center').addClass('main-image-slider-left');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM