jquery: animate scrollLeft

后端 未结 2 1036
醉酒成梦
醉酒成梦 2020-12-05 04:12

I\'m quite new to jquery and can\'t seem to figure out why my code isn\'t working. I have a horizontal layout and want to use the scrollLeft() function (which works perfect

相关标签:
2条回答
  • 2020-12-05 04:20

    First off I should point out that css animations would probably work best if you are doing this a lot but I ended getting the desired effect by wrapping .scrollLeft inside .animate

    $('.swipeRight').click(function()
    {
    
        $('.swipeBox').animate( { scrollLeft: '+=460' }, 1000);
    });
    
    $('.swipeLeft').click(function()
    {
        $('.swipeBox').animate( { scrollLeft: '-=460' }, 1000);
    });
    

    The second parameter is speed, and you can also add a third parameter if you are using smooth scrolling of some sort.

    0 讨论(0)
  • 2020-12-05 04:33

    You'll want something like this:

    
    $("#next").click(function(){
          var currentElement = currentElement.next();
          $('html, body').animate({scrollLeft: $(currentElement).offset().left}, 800);
          return false;
       }); 
    
    I believe this should work, it's adopted from a scrollTop function.

    0 讨论(0)
提交回复
热议问题