javascript - 如何让我的内容幻灯片在单击后重置其自动滚动计时器?

标签 javascript jquery html css

我正在制作内容幻灯片 -
目前在http://harddrive.co.za/internet-solutions-provider/

这是 javascript

jQuery(document).ready(function($) {
  setInterval(function() {
    moveRight();
  }, 8000);
  var slideCount = $('#slider-isp ul li').length;
  var slideWidth = $('#slider-isp ul li').width();
  var slideHeight = $('#slider-isp ul li').height();
  var sliderUlWidth = slideCount * slideWidth;
  $('#slider-isp').css({
    width: slideWidth,
    height: slideHeight
  });

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

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

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

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

我查看了另一篇文章来寻找答案
How do I get my image slider to reset its auto-scroll timer after a click?

我想应用类似的东西
不破坏我的代码

请帮忙

最佳答案

由于我们这里没有可验证的示例,因此我建议执行以下步骤:

  • setInterval 保存到变量中。
  • 在点击事件上,清除该间隔(以便它不再执行)。
  • 在同一变量上再次设置间隔。

这将确保您点击后会经过相同的时间间隔。

但是,这需要稍微更改您的代码(未测试,因为我没有相应的 HTML):

jQuery(document).ready(function ($) {
    
    // save the timer
    var tick = setInterval(function () {
        moveRight();
        }, 8000);

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

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

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

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

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

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

        $('a.control_prev').click(function () {
            clearInterval(tick); // discard the timer
            moveLeft(); // do the intended stuff for click
            
            // set the timer again
            tick = setInterval(function () {
              moveRight();
              }, 8000);
        });

        $('a.control_next').click(function () {
            clearInterval(tick); // discard the timer
            moveRight(); // do the intended stuff for click
            
            // set the timer again
            tick = setInterval(function () {
              moveRight();
              }, 8000);
        });

    });

关于javascript - 如何让我的内容幻灯片在单击后重置其自动滚动计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902246/

相关文章:

javascript - MS 机器人框架 : Is There a Command to Open an URL?

javascript - 在单页应用程序上捕获 DOM 重新渲染

jquery - 选择 select with jQuery 样式和颜色

javascript - Jquery 不能与 ajax 一起工作?

html - 如何降低 WordPress 2017 主题中的标题图片高度

jquery - 一次 CSS 动画图像

javascript - 在 jQuery 中更改背景颜色

javascript - 像加载一个文件一样加载多个 JS 文件?

javascript - 如何将同一属性上的数组和对象 "merge/concat"转换为平面数组(使用 Javascript)?

javascript - 这个简单的 Safari 扩展代码有什么问题?