Jquery .animate()用于左右滚动在Firefox中不起作用,但在Chrome中很好

Jquery .animate() for scrolling left and right not working in Firefox but fine in Chrome

本文关键字:不起作用 但在 很好 Chrome Firefox animate 用于 滚动 左右 Jquery      更新时间:2023-09-26

下面的jquery不能在firefox中工作,但在chrome中可以完美地工作。请看这里的小提琴例子。什么好主意吗?

$('#next').click(function() {
          event.preventDefault();
          $('#imageSlider').animate({
            marginLeft: "-=200px"
          }, "fast");
       });
$('#prev').click(function() {
      event.preventDefault();
      $('#imageSlider').animate({
        marginLeft: "+=200px"
      }, "fast");
   });

您不需要删除event.preventDefault()。你在Firefox中得到一个错误:

事件未定义

,因为您没有将event作为参数传递给回调函数。Chrome只是通过不停止JavaScript的执行来容错。

代码应该像这样:

$('#next').click(function(event) {  // Notice how I passed event as a parameter
          event.preventDefault();
          $('#imageSlider').animate({
            marginLeft: "-=200px"
          }, "fast");
       });

尝试删除

event.preventDefault();

直接删除event.preventDefault();

Remove event.preventDefault();因此,它将工作。

下面是更新后的代码:

$('#next').click(function() {
    $('#imageSlider').animate({
        marginLeft: "-=200px"
    }, "fast");
});
$('#prev').click(function() {
  $('#imageSlider').animate({
    marginLeft: "+=200px"
  }, "fast");
});
相关文章: