【问题标题】:Drag and Drop not working using jquery-ui使用 jquery-ui 拖放不起作用
【发布时间】:2016-10-13 09:12:52
【问题描述】:

您好,我正在创建一个 html 页面,我在其中使用 jQuery-ui 和 jquery-ui-punch 将一些 buttons 拖入一个 Div。但拖放不会发生。

我不明白为什么它不起作用。 sourcePopover 是弹出窗口,其中有我想在fav_id 中拖动的按钮。

这是我的 HTML/JavaScript 代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<meta name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="stylesheet" href="bootstrap-3.3.6-dist/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-3.3.6-dist/css/bootstrap-theme.min.css">
<!--Font Awesome -->


<script src="js/jquery-2.1.4.min.js"></script>
<script src="bootstrap-3.3.6-dist/js/bootstrap.min.js"></script>            
<script src="js/jquery-ui.min.js"></script> 
<link href="css/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="js/jquery.ui.touch-punch.min.js"></script>


<script type="text/javascript">     
/*Function to show popover when select button click*/
$(function(){   


    // Enables popover #2
    $("#btn_select_source").popover({       
        container: 'body',
        animation:true,
        html : true, 
        content: function() {
        return $("#sourcePopover").html();
        },
        title: function() {
        return $("#sourcePopoverTitle").html();
        }
    })
});

$(function(){   
    $("#sourcePopover button").draggable({
            revert: "invalid",
            refreshPositions: true,
            drag: function (event, ui) {
                ui.helper.addClass("draggable");
            },
            stop: function (event, ui) {
                ui.helper.removeClass("draggable");
                var image = this.src.split("/")[this.src.split("/").length - 1];
                if ($.ui.ddmanager.drop(ui.helper.data("draggable"), event)) {
                    alert(image + " dropped.");
                }
                else {
                    alert(image + " not dropped.");
                }
            }
        });
        $("#fav_div").droppable({
            drop: function (event, ui) {
                if ($("#fav_div button").length == 0) {
                    $("#fav_div").html("");
                }
                ui.draggable.addClass("dropped");
                $("#fav_div").append(ui.draggable);
            }
        });     
});

</script>
<style type="text/css"> 
    .draggable
    {
        filter: alpha(opacity=60);
        opacity: 0.6;
    }
    .dropped
    {
        position: static !important;
    }

</style>
</head>
<body style="background-color:#080808;" onload="init()">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  grid-padding-5px margin-top-10px">
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 remove-grid-padding" id="fav_div">
  <a data-toggle="popover" data-trigger="focus"  id="a_select_source">
     <button type="button" class="btn btn-secondary" id="btn_select_source" onclick="buttonSourcePressed(this.id)"> Select<br/>Source</button></a>                          
 </div>

 <div id="sourcePopover" class="container">
 <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 remove-grid-padding margin-2px" >
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 padding-2px" >
        <button class="btn btn-secondary  btn-popup-sources center-block" id="source_btn_disc" >
            <img src="images/DISC.png" class="img-responsive popup-source-image" style="padding:5px" > <br/>
            <span class="popup-source-text"> Disc </span>
        </button>       
        <button class="btn btn-secondary btn-popup-sources center-block" id="source_btn_BT">
            <img src="images/BT.png" class="img-responsive popup-source-image" > <br/>
            <span class="popup-source-text"> Bluetooth </span>
        </button>
     </div>
   </div>
 </div>
</body>
</html>

请给我提示或参考。

【问题讨论】:

    标签: javascript android html jquery-ui drag-and-drop


    【解决方案1】:

    @pritishvaidya 已经回答你应该在可拖动中添加属性cancel:false,这是绝对正确的。

    除此之外,我还更改了一些不起作用的代码。下面列出了我所做的更改。

    1.

    在可拖动的stop 函数中,您试图获取图像名称,但它不起作用。这也可以防止拖放。

    2.

    您试图通过调用 drop 函数并传递 ui.helper.data("draggable") 来检查图像是否已被删除,但我已将其更改为
    ui.helper.data("ui-draggable")。此更改可能不是必需的,因为它取决于您的 jquery-ui 版本。

    单击下面的运行代码 sn-p 以找到您的拖放工作。

    编辑:

    每次显示popover时你必须绑定draggable所以你可以使用popover的shown.bs.popover事件。放下按钮时,我也会隐藏弹出框。请查看更新的代码

    $(function(){   
       $("#btn_select_source").popover({       
         container: 'body',
         animation:true,
         html : true, 
         content: function() {
    	    return $("#sourcePopover").html();
         },
         title: function() {
            return $("#sourcePopoverTitle").html();
         }
       });
      
      $('#btn_select_source').on('shown.bs.popover', function () {
    		makeButtonDraggable();
      });  
      
    });
    
    function init(){}
    function buttonSourcePressed(c){}
    
    function makeButtonDraggable(){  
      $(".popover-content button").draggable({
        cancel :false,
        revert: "invalid",
        refreshPositions: true,
        drag: function (event, ui) {
    	  ui.helper.addClass("draggable");
        },
        stop: function (event, ui) {
    	  ui.helper.removeClass("draggable");
          var image = $(this).find('img').attr('src').split("/")[$(this).find('img').attr('src').split("/").length - 1];
          if ($.ui.ddmanager.drop(ui.helper.data("ui-draggable"), event)) {
    	    alert(image + " dropped.");
            $("#btn_select_source").popover('hide')
          }
          else {
    	    alert(image + " not dropped.");
          }
       }
      });
            
      $("#fav_div").droppable({
        drop: function (event, ui) {
          if ($("#fav_div button").length == 0) {
            $("#fav_div").html("");
          }
          ui.draggable.addClass("dropped");
          $("#fav_div").append(ui.draggable);
       }
     });
    }
    .draggable{
      filter: alpha(opacity=60);
      opacity: 0.6;
    }
    .dropped{
      position: static !important;
    }
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset=utf-8>
      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap-theme.min.css">
      
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>            
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
     
    </head>
    <body  style="background-color:#080808;" onload="init()" >
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  grid-padding-5px margin-top-10px">
        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 remove-grid-padding" id="fav_div" style="border:1px solid blue;">
          <a data-toggle="popover" data-trigger="focus"  id="a_select_source">
            <button type="button" class="btn btn-secondary" id="btn_select_source" onclick="buttonSourcePressed(this.id)"> Select<br/>Source</button>
          </a>                          
        </div>
        <div id="sourcePopover" class="container ">    
          <button class="btn btn-secondary  btn-popup-sources center-block" id="source_btn_disc" >
    	    <img src="images/DISC.png" class="img-responsive popup-source-image" style="padding:5px" > <br/>
            <span class="popup-source-text"> Disc </span>
          </button>       
          <button class="btn btn-secondary btn-popup-sources center-block" id="source_btn_BT">
            <img src="images/BT.png" class="img-responsive popup-source-image" > <br/>
            <span class="popup-source-text"> Bluetooth </span>
          </button>
    	</div>
      </div>
    </body>

    【讨论】:

    • 是的,但它不适用于弹出窗口。我有打开 sourcePopover 的选择源按钮。所以,从sourcePopover 我需要将img/button 拖到fav_div
    • @SandipArmalPatil ,我已经更新了我的答案。看看更新并运行代码 sn-p。
    • 感谢您的快速回复 :-) 现在投票回答。但奇怪的是它仍然无法在这里工作。现在我收到一个警告Ignored attempt to cancel a touchstart event with cancelable=false, for example because scrolling is in progress and cannot be interrupted.
    • 当我们在移动调试模式下打开屏幕时,它可以在浏览器上工作,但不能在 chrome 上工作。那么触摸打孔在这里可能不起作用吗?
    • 看看这个stackoverflow.com/questions/26478267/…。这可能会对您有所帮助。
    【解决方案2】:

    Buttons 可以被draggabledroppable添加到 draggable 类中,如图所示。

    通过使用cancel:false

    所以你的函数在实现后会是这个样子-

    $(function () {
        $("#dvSource button").draggable({
            cancel :false,
            revert: "invalid",
            refreshPositions: true,
            drag: function (event, ui) {
                ui.helper.addClass("draggable");
            },
    

    这里是取消事件的documentation,因为它不会触发第一次点击事件

    这是我在没有任何引导的情况下使用的代码,所以我根据上面显示的编码风格省略了类。

    <html>
      <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.8.24/jquery-ui.min.js" type="text/javascript"></script>
    <link href="http://code.jquery.com/ui/1.8.24/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
    
    
    </head>
    <body>
    <div>
    <style type="text/css">
    body
    {
        font-family: Arial;
        font-size: 10pt;
    }
    img
    {
        height: 100px;
        width: 100px;
        margin: 2px;
    }
    .draggable
    {
        filter: alpha(opacity=60);
        opacity: 0.6;
    }
    .dropped
    {
        position: static !important;
    }
    #dvSource, #dvDest
    {
        border: 5px solid #ccc;
        padding: 5px;
        min-height: 100px;
        width: 430px;
    }
    </style>
    
    
    <script type="text/javascript">
    $(function () {
        $("#dvSource button").draggable({
            cancel :false,
            revert: "invalid",
            refreshPositions: true,
            drag: function (event, ui) {
                ui.helper.addClass("draggable");
            },
            stop: function (event, ui) {
                ui.helper.removeClass("draggable");
                /*var image = this.src.split("/")[this.src.split("/").length - 1];*/
                /*if ($.ui.ddmanager.drop(ui.helper.data("draggable"), event)) {
                    alert(image + " dropped.");
                }
                else {
                    alert(image + " not dropped.");
                }*/
            }
        });
        $("#dvDest").droppable({
            drop: function (event, ui) {
                if ($("#dvDest button").length == 0) {
                    $("#dvDest").html("");
                }
                ui.draggable.addClass("dropped");
                $("#dvDest").append(ui.draggable);
            }
        });
    });
    </script>
    <div id="dvSource">
    
    <a data-toggle="popover"   id="a_select_source">
    
            <button type="button" class="btn btn-secondary" id="btn_select_source">Select<br/>Source</button>
    
            </a>
    
    </div>
    <hr />
    <div id="dvDest" >
            <button class="btn btn-secondary  btn-popup-sources center-block" id="source_btn_disc" >
                <img src="images/DISC.png" class="img-responsive popup-source-image" style="padding:5px" > <br/>
                <span class="popup-source-text"> Disc </span>
            </button>
            <button class="btn btn-secondary  btn-popup-sources center-block" id="source_btn_disc" >
                <img src="images/DISC.png" class="img-responsive popup-source-image" style="padding:5px" > <br/>
                <span class="popup-source-text"> Disc </span>
            </button>
            <button class="btn btn-secondary  btn-popup-sources center-block" id="source_btn_disc" >
                <img src="images/DISC.png" class="img-responsive popup-source-image" style="padding:5px" > <br/>
                <span class="popup-source-text"> Disc </span>
            </button>
        </div>
        </div>
        </body>
        </html>
    

    您的代码中的另一个错误是您没有指定此 sn-p 执行的操作会抛出 undefined error,因此 不需要。您可以仍然可以通过将 src 属性更改为所需类型来使用它。

    /*  var image = this.src.split("/")[this.src.split("/").length - 1];
                    if ($.ui.ddmanager.drop(ui.helper.data("draggable"), event)) {
                        alert(image + " dropped.");
                    }
                    else {
                        alert(image + " not dropped.");
                    } */
    

    此代码用于跟踪从 div 中拖放的图像,而不用于按钮。

    工作代码的屏幕截图可以在这里看到 -

    【讨论】:

    • 小提琴在哪里?在排除 jquery.ui-touch-punch 的情况下如何支持移动设备?
    • 只需在上面的代码中添加 cancel:false 并让所有 remians 保持相同,因为问题不是 jquery.ui-touch-punch 而是按钮可拖动类,因为它略有不同跨度>
    • 你能帮我做一个小提琴吗?我会努力的。
    【解决方案3】:

    对于使用 jquery 的移动设备中的触摸事件,您必须在下面包含类似文件

    <script src="file:///android_asset/jquery/jquery.ui.touch-punch.min.js" type="text/javascript"></script>
    

    你可以从这里得到这个文件:https://github.com/furf/jquery-ui-touch-punch

    确保在 jquery-ui.min.js 文件之后包含此内容

    【讨论】:

    • 您将其应用到 div 尝试将其应用到 div 内的任何项目中。
    【解决方案4】:

    您正在尝试拖动按钮,然后您必须阻止按钮默认行为。像:按钮、选择标签等具有默认行为,当您尝试通过单击或触摸执行某些操作时,它将触发该默认事件。因此,您只需在 draggable() 上使用 cancel: false 来防止该默认事件,请参阅有关 cancel 的更多信息

    在使用 cancel: false 之后,这里的工作链接 https://jsfiddle.net/L3j9qy4h/1/ 与您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      相关资源
      最近更新 更多