🕹 스크립트

jquery pc에서 scroll:auto 부분 드래그 되도록 만들기

(。θᗨθ。) 2024. 5. 23. 13:23
$(document).ready(function() {
  var $content = $('.forScroll');
  var isDown = false;
  var startX, startY, scrollLeft, scrollTop;

  $content.on('mousedown', function(e) {
      isDown = true;
      $content.css('cursor', 'grabbing');
      startX = e.pageX - $content.offset().left;
      startY = e.pageY - $content.offset().top;
      scrollLeft = $content.scrollLeft();
      scrollTop = $content.scrollTop();
  });

  $(document).on('mouseup', function() {
      isDown = false;
      $content.css('cursor', 'grab');
  });

  $(document).on('mouseleave', function() {
      isDown = false;
      $content.css('cursor', 'grab');
  });

  $content.on('mousemove', function(e) {
      if (!isDown) return;
      e.preventDefault();
      var x = e.pageX - $content.offset().left;
      var y = e.pageY - $content.offset().top;
      var walkX = (x - startX) * 1; //scroll-fast
      var walkY = (y - startY) * 1; //scroll-fast
      $content.scrollLeft(scrollLeft - walkX);
      $content.scrollTop(scrollTop - walkY);
  });
});

 

 

728x90