【问题标题】:How to prevent click while scrolling horizontally如何在水平滚动时防止点击
【发布时间】:2023-03-18 09:25:01
【问题描述】:

我正在使用带有链接的卡片列表,它可以使用鼠标和箭头水平滚动。 我想在向左或向右滚动/拖动项目时防止单击(标签)。 但是如果我不拖动项目,点击应该可以工作。

这是我在 Javascript 中使用的。 代码

var instance = $(".hs__wrapper");
$.each( instance, function(key, value)
{
    var arrows = $(instance[key]).find(".arrow"),
      prevArrow = arrows.filter('.arrow-prev'),
      nextArrow = arrows.filter('.arrow-next'),
      box = $(instance[key]).find(".hs"), 
      x = 0,
      mx = 0,
      maxScrollWidth = box[0].scrollWidth - (box[0].clientWidth / 2) - (box.width() / 2);

      $(arrows).on('click', function() {
          
        if ($(this).hasClass("arrow-next")) {
          x = ((box.width() / 2)) + box.scrollLeft() - 10;
          box.animate({
            scrollLeft: x,
          })
        } else {
          x = ((box.width() / 2)) - box.scrollLeft() -10;
          box.animate({
            scrollLeft: -x,
          })
        }
          
      });
    
  $(box).on({
    mousemove: function(e) {
      var mx2 = e.pageX - this.offsetLeft;
      if(mx) this.scrollLeft = this.sx + mx - mx2;
    },
    mousedown: function(e) {
      this.sx = this.scrollLeft;
      mx = e.pageX - this.offsetLeft;
    },
    scroll: function() {
      toggleArrows();
    }
  });

  $(document).on("mouseup", function(){
    mx = 0;
  });
  
  function toggleArrows() {
    if(box.scrollLeft() > maxScrollWidth - 10) {
        // disable next button when right end has reached 
        nextArrow.addClass('disabled');
      } else if(box.scrollLeft() < 10) {
        // disable prev button when left end has reached 
        prevArrow.addClass('disabled')
      } else{
        // both are enabled
        nextArrow.removeClass('disabled');
        prevArrow.removeClass('disabled');
      }
  }

});

【问题讨论】:

    标签: javascript jquery scroll slider scrollbar


    【解决方案1】:

    尝试向链接添加单击事件处理程序,以防止滚动时的默认浏览器行为。然后,删除事件处理程序,检测滚动何时停止使用例如this method.

    var instance = $(".hs__wrapper");
    $.each( instance, function(key, value)
    {
        var arrows = $(instance[key]).find(".arrow"),
          prevArrow = arrows.filter('.arrow-prev'),
          nextArrow = arrows.filter('.arrow-next'),
          box = $(instance[key]).find(".hs"), 
          x = 0,
          mx = 0,
          maxScrollWidth = box[0].scrollWidth - (box[0].clientWidth / 2) - (box.width() / 2);
    
          $(arrows).on('click', function() {
              
            if ($(this).hasClass("arrow-next")) {
              x = ((box.width() / 2)) + box.scrollLeft() - 10;
              box.animate({
                scrollLeft: x,
              })
            } else {
              x = ((box.width() / 2)) - box.scrollLeft() -10;
              box.animate({
                scrollLeft: -x,
              })
            }
              
          });
        
      $(box).on({
        mousemove: function(e) {
          var mx2 = e.pageX - this.offsetLeft;
          if(mx) this.scrollLeft = this.sx + mx - mx2;
        },
        mousedown: function(e) {
          this.sx = this.scrollLeft;
          mx = e.pageX - this.offsetLeft;
        },
        scroll: function() {
          clearTimeout($.data(this, 'scrollTimer'));
          $.data(this, 'scrollTimer', setTimeout(function() {
             $(box).find('a').off('click');
          }, 250));
    
          toggleArrows();
          $(box).find('a').on('click', function(e) {
             e.preventDefault();
          });
        }
      });
    
      $(document).on("mouseup", function(){
        mx = 0;
      });
      
      function toggleArrows() {
        if(box.scrollLeft() > maxScrollWidth - 10) {
            // disable next button when right end has reached 
            nextArrow.addClass('disabled');
          } else if(box.scrollLeft() < 10) {
            // disable prev button when left end has reached 
            prevArrow.addClass('disabled')
          } else{
            // both are enabled
            nextArrow.removeClass('disabled');
            prevArrow.removeClass('disabled');
          }
      }
    
    });
    

    【讨论】:

    • @sbgib 您能否添加一些修改为:如果显示的项目列表较少(当没有溢出时,没有滚动),那么箭头应该被隐藏。此外,如果项目列表位于初始位置,则应隐藏前一个箭头。
    • @sbgib 你能回答我最近的问题吗?
    • @AnkitJaiSwal 我现在不确定你的意思。如果您可以提出问题,展示您的代码并展示您迄今为止为解决此问题所做的尝试,那么我会尽力提供帮助。
    • @RiyaSingh 关于 PWA 安装按钮?最好简单地使用 CSS 来防止任何 CLS,因为在 beforeinstallprompt 运行之前您不知道是否应该显示按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2019-08-17
    相关资源
    最近更新 更多