Is there a way to make horizontal scrolling smoother

后端 未结 6 2074
长情又很酷
长情又很酷 2021-02-05 17:52

This fiddle is almost what I\'m looking for, I got it from MDN. The only thing missing is that I want to make it smoother. Is there a way to do that without using jQuery or any

6条回答
  •  渐次进展
    2021-02-05 18:27

    You can also use the .scrollTo method. I've modified the jsFidlle with this javascript :

    https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo

    var container = document.getElementById('container');
    var input = document.getElementById('content');
    var scrollAmount = 0;
    var scrollMin = 0
    var scrollMax = input.clientWidth
    
    document.getElementById('slide').onclick = function () {
      container.scrollTo({
        top: 0,
        left: Math.max(scrollAmount += 500, scrollMax),
        behavior: 'smooth'
      });
    };
    
    
    document.getElementById('slideBack').onclick = function () {
      container.scrollTo({
        top: 0,
        left: Math.min(scrollAmount -= 500, scrollMin),
        behavior: 'smooth'
      });
    };
    

提交回复
热议问题