【问题标题】:jQuery: Finish dragging without triggering click eventjQuery:在不触发点击事件的情况下完成拖动
【发布时间】:2019-08-04 23:27:58
【问题描述】:

我正在尝试设置以下页面:

  1. 如果单击按钮,您可以看到一个 div。
  2. 如果单击 div,可以看到下一个 div。
  3. 如果您移动按钮,则不会出现 »click«(所需行为)

我遇到的问题是,如果您移动 div,则会出现下一个 div - 这不是我想要的。 “下一个 div”不应在拖动事件完成后显示。

这是我的代码:

$(function() {
  $("#button").draggable({
    stack: 'div',
    containment: "body"
  });
});

$('#button').on('mouseup', function() {
  if (!$(this).hasClass('ui-draggable-dragging')) {
    // click function
    $("#content").toggle();
  }
});

$(function() {
  $("#content").draggable({
    stack: 'div',
    containment: "body"
  });
});

let containers = $('.trip').hide();
let firstContainer = containers.first().show();

containers.on('click', function() {
  //Get current element and next sibling
  let elem = $(this);
  let next = elem.next('.trip');

  //Does sibling exist?
  if (next.length) {
    next.show();
  } else {
    firstContainer.show();
  }
  elem.hide();
});
body {
  width: 100vw;
  height: 100vh;
  padding: 0;
  margin: 0;
  left: 0;
  top: 0;
  background-color: grey;
}

#button {
  width: 100px;
  height: 100px;
  background-color: cyan;
}

#content {
  display: none;
  cursor: all-scroll;
  top: 10%;
  left: 10%;
  position: absolute;
}

.trip {
  width: 200px;
  height: 200px;
  background-color: blue;
  color: white;
}
<div id="button">Button</div>

<div id="content">
  <div class="trip">div 1</div>
  <div class="trip">div 2</div>
  <div class="trip">div 3</div>
</div>

<script src="https://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="https://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
<script src="https://raw.githubusercontent.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>

有没有办法解决这个问题? :)
(一个可能的问题是纯 JavaScript 与 jQuery 混合)
谢谢

【问题讨论】:

    标签: javascript jquery draggable clickable


    【解决方案1】:

    这里要解决的主要问题是将#content 上的常规click 事件与其他在拖动元素完成期间也触发的“类似点击事件”区分开来。

    您的代码目前有一种方法可以用来实现所需的行为:

    if (! $(this).hasClass('ui-draggable-dragging')) {
       /* This was a "regular click event"
    }
    

    所以对于你的代码,你可以修改如下:

    $(function() {
    
      /* Combine on ready logic into one place */
    
      $("#button").draggable({
        stack: 'div',
        containment: "body"
      });
      
      $("#content").draggable({
        stack: 'div',
        containment: "body"
      });
      
      /* Hide all trip elements except for first */
      $('.trip', '#content').not(':first').hide();
    });
    
    $('#button').on('mouseup', function() {
      if (!$(this).hasClass('ui-draggable-dragging')) {
        $("#content").toggle();
      }
    });
    
    $('#content').on('mouseup', function() {
    
      /* Reuse same logic in #button mouseup handler */
      if (!$(this).hasClass('ui-draggable-dragging')) {
    
          /* 
          If content element if not dragging, treat mouse up as conclusion
          of click event and rotate visibility of trip elements like this
          */
          let trip = $('.trip:visible', '#content');
          let next = trip.next().length === 0 ? 
              $('.trip:first', '#content') : trip.next();
          
          trip.hide();
          next.show(); 
      }
    });
    body {
      width: 100vw;
      height: 100vh;
      padding: 0;
      margin: 0;
      left: 0;
      top: 0;
      background-color: grey;
    }
    
    #button {
      width: 100px;
      height: 100px;
      background-color: cyan;
    }
    
    #content {
      display: none;
      cursor: all-scroll;
      top: 10%;
      left: 10%;
      position: absolute;
    }
    
    .trip {
      width: 200px;
      height: 200px;
      background-color: blue;
      color: white;
    }
    <div id="button">Button</div>
    
    <div id="content">
      <div class="trip">div 1</div>
      <div class="trip">div 2</div>
      <div class="trip">div 3</div>
    </div>
    
    <script src="https://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="https://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
    <script src="https://raw.githubusercontent.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>

    【讨论】:

    • "Uncaught SyntaxError: missing ) after argument list" 考虑修复这个问题,以便我们可以使用现场演示,谢谢。
    • @DacreDenny 我的眼里含着泪水!非常感谢你!最后一个问题:这个可拖动的东西在手机上也能用吗?
    • @DacreDenny 好的,我检查过了:它不起作用。为什么? :(
    • @LinaMcClanahan 不客气 - 很高兴我能帮上忙 :) 手机可能有点棘手 - 让我知道这是否适用于你:)
    • @LinaMcClanahan 似乎对带有 jqueryui 的移动设备的支持有限,但是我发现这似乎在移动设备上工作得更好——这对你来说是一个选择吗? jqueryscript.net/other/Mobile-Drag-Drop-Plugin-jQuery.html
    【解决方案2】:

    其实,你只需要这段可读的代码,

    • 为所有可拖动元素分配一个 .draggable
    • 使用新类作为stack: '.draggable'
    • 注册点击你的'#container',而不是你的.trip元素
    • 只使用一个事件,"click" 一个:
    • 隐藏所有.trip,但首先使用CSS .trip~.trip {display:none;}

    jQuery( $ => {
    
      const $cont = $('#content');
      const $bttn = $('#button');
      const $trip = $('.trip');
      const tripL = $trip.length;
      let   i = 0;
    
      $('.draggable').draggable({stack:'div', containment:'body'});
      
      $bttn.on('click', function() {
        if (!$(this).hasClass('ui-draggable-dragging')) $cont.toggle();
      });
    
      $cont.on('click', function() {
        $trip.eq(++i % tripL).show().siblings($trip).hide();
      });
    
    });
    html, body { height:100%; margin:0;}
    
    body {
      background-color: grey;
    }
    
    #button {
      width: 100px;
      height: 100px;
      background-color: cyan;
    }
    
    #content {
      display: none;
      cursor: all-scroll;
      top: 10%;
      left: 10%;
      position: absolute;
    }
    
    .trip {
      width: 200px;
      height: 200px;
      background-color: blue;
      color: white;
    }
    
    .trip ~ .trip {display: none;}
    <!-- PS: Add class="draggable" to draggable elements -->
    
    <div id="button" class="draggable">Button</div>
    <div id="content" class="draggable">
      <div class="trip" id="a">div 1</div>
      <div class="trip" id="b">div 2</div>
      <div class="trip" id="c">div 3</div>
    </div>
    
    <script src="https://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="https://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
    <script src="https://raw.githubusercontent.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>

    编辑

    更多内容看这个答案:https://stackoverflow.com/a/57351517/383904

    【讨论】:

    • 您好,谢谢您的回答。最后移动或单击的对象应始终位于顶部。你的代码可以做到吗?
    • @LinaMcClanahan 当然,我不小心删除了处理 z-index 的 stack:'div',。我已经把它加回来了:)
    • 好吧,有趣!这也能解决我的“新问题”吗?链接:stackoverflow.com/questions/57351370/…
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多