【问题标题】:Slider only slide once - WHY?滑块只滑动一次 - 为什么?
【发布时间】:2014-04-20 16:02:05
【问题描述】:

我在我的网站上添加了一个滑块,但它似乎只运行一次。它停在第二张图片上。 我希望它每 5 秒滑动一次。请帮帮我!

来源:http://codepen.io/zuraizm/pen/vGDHl

Javascript:

var slideCount = $('#slider ul li').length;
var slideWidth = $('#slider ul li').width();
var slideHeight = $('#slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;

$('#slider').css({ width: slideWidth, height: slideHeight });

$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });

$('#slider ul li:last-child').prependTo('#slider ul');

function moveLeft() {
    $('#slider ul').animate({
        left: + slideWidth
    }, 200, function () {
        $('#slider ul li:last-child').prependTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

function moveRight() {
    $('#slider ul').animate({
        left: - slideWidth
    }, 200, function () {
        $('#slider ul li:first-child').appendTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};


$('a.control_prev').click(function () {
    moveLeft();
});

$('a.control_next').click(function () {
    moveRight();
});

$(function(){
setInterval(function () {
    moveRight();
}, 5000);
});

【问题讨论】:

  • 对我来说似乎工作正常。
  • 包含$(document).ready()之后的代码 => setInterval(function () { moveRight(); }, 3000);
  • 忽略最后一个破折号 (-) ,Stackoverflow 自动包含它。
  • 请记住,我已调整为使代码在没有复选框的情况下自动播放。
  • 嘿Patrik,试过了,但它不起作用。很奇怪。现在在顶部看起来像这样:jQuery(document).ready(function ($) { setInterval(function () { moveRight(); }, 3000);

标签: javascript jquery html slider jquery-slider


【解决方案1】:

查找并更新您的代码:

删除复选框函数“$('#checkbox').change(function() ...”或替换为

setInterval(function () {
      moveRight();
  }, 5000);  

这样就解决了。

codepen

Demo

【讨论】:

  • 但是它不会自动滑动。我需要它每 5 秒自动滑动一次。我该怎么做?
  • “它不会自动滑动”是什么意思?你的意思是自动滑动而不点击复选框?
  • 当前,当您单击复选框时,自动滑动可以正常工作。 codepen.io/kzkpro/full/aHALy
  • 我的意思是我想删除复选框。它应该在没有任何复选框的情况下自动滑动。
  • 还是不行。我真的不明白为什么。它只滑动一次。然后它停止。我仍然可以使用左右按钮来滑动。
猜你喜欢
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
相关资源
最近更新 更多