Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在每次单击链接时向左滚动div?_Jquery_Css - Fatal编程技术网

Jquery 如何在每次单击链接时向左滚动div?

Jquery 如何在每次单击链接时向左滚动div?,jquery,css,Jquery,Css,我从某处获取了这段代码,并对其进行了修改。我已经设法向左滚动了400px。但我想它滚动左,每次我点击链接。我使用的脚本是: $('.masternav a').bind('click',function(event){ var $anchor = $(this); $('html, body ').stop().animate({ scrollLeft: (400)}, 1000); event.preventDefault(); }); 有人能帮忙吗?您可能需要: $('.m

我从某处获取了这段代码,并对其进行了修改。我已经设法向左滚动了400px。但我想它滚动左,每次我点击链接。我使用的脚本是:

$('.masternav a').bind('click',function(event){
  var $anchor = $(this);
  $('html, body ').stop().animate({
  scrollLeft: (400)}, 1000);
  event.preventDefault();
});

有人能帮忙吗?

您可能需要:

$('.masternav a').bind('click',function(event){
            var $anchor = $(this);
            $('html, body ').stop().animate({
                scrollLeft: '+=40'}, 1000);
            event.preventDefault();
        });

每次单击时将滚动40 px。

如果某个值带有前导+=或-=字符序列,则通过从属性的当前值中添加或减去给定数字来计算目标值。因此,请尝试以下方法:

$('.masternav a').bind('click',function(event){
   var $anchor = $(this);
   $('html, body ').stop().animate({
            scrollLeft: "+=400")}, 1000);
            event.preventDefault();
});
祝你好运