长按的JQuery UI Draggable适用于mousedown但不适用于touchstart

时间:2017-06-02 17:13:17

标签: jquery jquery-ui jquery-mobile touch touch-event

以下在桌面设备(mousedown)上运行正常,但如果您在移动设备(touchstart)上试用,则无法拖动。

var t;
$(document).on('touchstart mousedown','.menu-item', function (event) {
  var self = this;
  if ($(self).hasClass('draggable')) return;
  t = setTimeout(function () {
    $(self).draggable({
      revert: true,
      appendTo: 'body'
    }).draggable('enable').addClass('draggable');
    $(self).trigger(event)
  }, 800);
});

$(document).on("touchend mouseup", function () {
  clearTimeout(t);
  $('.draggable').draggable( 'disable' ).removeClass('draggable');
});
.menu-item {
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  margin: 5px;
}
.menu-item.draggable {
  background-color: orange;
  transform: scale(1.1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<div class="menu-item">1</div>
<div class="menu-item">2</div>
<div class="menu-item">3</div>

如何让它兼具?

1 个答案:

答案 0 :(得分:2)

touchpunch替换jquerymobile解决了问题,无需更改代码。