jQuery scrollRight?

前端 未结 4 1790
陌清茗
陌清茗 2020-11-30 00:41

I have the following code that seems to scroll a div on click all the way to the left. I am wondering if:

  1. there is a way to get it to get it to scroll only 200
4条回答
  •  暖寄归人
    2020-11-30 01:18

    scrollLeft IS scrollRight. Sort of. All it does is set the amount of horizontal scroll. If you set it to zero then it will be all the way left. If you set it to something greater than zero then it will move to the right!

    As far as making it go in increments, you would have to get the current scrollLeft distance and then subtract 200.

    $(".leftArrow").click(function () { 
      var leftPos = $('.innerWrapper').scrollLeft();
      $(".innerWrapper").animate({scrollLeft: leftPos - 200}, 800);
    });
    
    $(".rightArrow").click(function () { 
      var leftPos = $('.innerWrapper').scrollLeft();
      $(".innerWrapper").animate({scrollLeft: leftPos + 200}, 800);
    });
    

提交回复
热议问题