简体   繁体   English

jQuery滑块 - 最后一次转换

[英]jQuery slider - last to first transition

I created this slider (didn't want to use plugins): 我创建了这个滑块(不想使用插件):

function slider(sel, intr, i) {
  var _slider = this;
  this.ind = i;
  this.selector = sel;
  this.slide = [];
  this.slide_active = 0;
  this.amount;
  this.selector.children().each(function (i) {
    _slider.slide[i] = $(this);
    $(this).hide();
  })
  this.run();
}
slider.prototype.run = function () {
  var _s = this;
  this.slide[this.slide_active].show();
  setTimeout(function () {
    _s.slide[_s.slide_active].hide()
    _s.slide_active++;
    _s.run();
  }, interval);
}
var slides = [];
var interval = 1000
$('.slider').each(function (i) {
  slides[i] = new slider($(this), interval, i);
})

The problem I have is that I don´t know how to get it after the last slide(image), it goes back to the first slide again. 我遇到的问题是我不知道如何在最后一张幻灯片(图片)之后得到它,它又回到了第一张幻灯片。 Right now, it just .hide and .show till the end and if there is no image it just doesn´t start again. 现在,它只是.hide和.show直到结束,如果没有图像,它就不会重新开始。

Can someone help me out with a code suggestion to make it take the .length of the slider(the number of images on it) and if it is the last slide(image), then goes back to the first slide(image)... like a cycle. 有人可以帮我提出一个代码建议 ,让它取滑块的.length(上面的图像数量),如果它是最后一张幻灯片(图像),那么就回到第一张幻灯片(图片)。像一个循环。

Edit: Slider markup 编辑:滑块标记

    <div class="small_box top_right slider">
        <img class="fittobox" src="img/home10.jpg" alt="home10" width="854" height="592">
        <img class="fittobox" src="img/home3.jpg" alt="home3" width="435" height="392">
        <img class="fittobox" src="img/home4.jpg" alt="home4" width="435" height="392">
    </div>

Take a look at this Fiddle link, this will help you create the slider in a cyclic way.If the slider reaches the last image it will start again from the first image. 看看这个Fiddle链接,这将帮助您以循环方式创建滑块。如果滑块到达最后一个图像,它将从第一个图像再次开始。

var index = $selector.index();   
if (index == (length - 1)) {   
    $('img').first().removeClass('invisible').addClass('visible');  
}

I hope this will help you more. 我希望这会对你有所帮助。 All the best. 祝一切顺利。

Created a fixed version for you here . 在这里为你创建了一个固定版本

The easiest way to do this is to run a simple maths operation where you currently have 最简单的方法是运行当前的简单数学运算

_s.slide_active++;

Instead, I get _s.slide_active , add 1 , then run that through modulus ( % ) to the total length — which gives the remainder: 相反,我得到_s.slide_active ,加1 ,然后通过模数( % )运行到总长度 - 这给出了余数:

_s.slide_active = (_s.slide_active + 1) % _s.slide.length;

You need to get to 0 after length-1. 你需要在长度为1后达到0。 One simple way to do that is to work modulo length: 一种简单的方法是以模数长度工作:

_s.slide_active++;
_s.slide_active %= length;

not tested but hope helpful : 没有测试,但希望有用:

function slider(sel, intr , i){
   ...
   this.count = this.selector.children().length;
   this.run();
}

slider.prototype.run = function(){
    var _s = this;
    this.slide[this.slide_active].show();
    setTimeout(function(){
        _s.slide[_s.slide_active].hide()
        if(_s.slide_active == this.count)
             _s.slide_active = 0;  
        else
             _s.slide_active++;
        _s.run();
    }, interval);
}  

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

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