2016-10-13 9 views
3

こんにちは私はbuttonsを1つのDivにドラッグしているhtmlページをjQuery-uiとjquery-ui-punchを使って作成しています。しかし、ドラッグアンドドロップは起こらない。jquery-uiを使用してドラッグ&ドロップが動作しない

なぜ動作しないのかわかりません。 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> 

私にヒントまたは参考にしてください。ファイルのような

あなたは以下の含める必要が携帯使用jqueryのタッチイベントの

答えて

2

@pritishvaidyaあなたはそれが絶対的に正しい、ドラッグ可能に属性cancel:falseを追加すべきであると答えました。

これ以外にも、動作していないコードがいくつか変更されています。私が行った変更は以下の通りです。

1。

stopの中で、機能はドラッグ可能なイメージ名を取得しようとしていますが、機能しません。これはドラッグアンドドロップも防止します。

2.

あなたは画像がドロップ関数を呼び出すと、(「ドラッグ」)ui.helper.dataを渡すことによって落とされたかどうかをチェックしようとしていたが、私は
ui.helperにそれを変更しました。データ(「ui-draggable」)。この変更は、jquery-uiのバージョンに依存するため、必要ではありません。

以下のコードを実行するをクリックすると、ドラッグアンドドロップが実行されます。

編集:

あなたはポップオーバーのshown.bs.popoverイベントを使用できるようにポップオーバーをするたびに表示されたときにドラッグ可能に結合しなければなりません。私はまた、ボタンがドロップされたときにポップオーバーを隠しています。その後、あなたはボタンのデフォルトの動作を阻止する必要があなたのボタンをドラッグしようとしている更新されたコード

$(function(){ 
 
    $("#btn_select_source").popover({  
 
    container: 'body', 
 
    animation:true, 
 
    html : true, 
 
    content: function() { 
 
\t  return $("#sourcePopover").html(); 
 
    }, 
 
    title: function() { 
 
     return $("#sourcePopoverTitle").html(); 
 
    } 
 
    }); 
 
    
 
    $('#btn_select_source').on('shown.bs.popover', function() { 
 
\t \t makeButtonDraggable(); 
 
    }); 
 
    
 
}); 
 

 
function init(){} 
 
function buttonSourcePressed(c){} 
 

 
function makeButtonDraggable(){ 
 
    $(".popover-content button").draggable({ 
 
    cancel :false, 
 
    revert: "invalid", 
 
    refreshPositions: true, 
 
    drag: function (event, ui) { 
 
\t ui.helper.addClass("draggable"); 
 
    }, 
 
    stop: function (event, ui) { 
 
\t 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)) { 
 
\t  alert(image + " dropped."); 
 
     $("#btn_select_source").popover('hide') 
 
     } 
 
     else { 
 
\t  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" > 
 
\t  <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> 
 
\t </div> 
 
    </div> 
 
</body>

+0

はい、それはポップアップではうまくいきません。私は 'sourcePopover'を開くソースボタンを選択しています。だから 'sourcePopover'から' fav_div'のimg/buttonをドラッグする必要があります –

+0

@SandipArmalPatil、私は自分の答えを更新しました。アップデートを見て、コードスニペットを実行してください。 –

+0

あなたのクイックレスポンスに感謝します:-)今のところアップ投票の答えです。しかし、奇妙なここではまだ働いていない。例えば、スクロールが進行中で中断することができないなど、cancelable = falseのタッチスタートイベントを取り消そうとする試みは無視されます。 –

0

<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ファイル

+0

がすでに追加されています。 –

+0

divに適用すると、div内の項目に適用してみてください。 –

1

後にこれを含めるようにしてくださいButtonsdraggabledroppableにすることができます。シンプルな機能図のようにのにドラッグ可能なクラスを割り当てます。だからあなたの関数が実装した後、このようになりますcancel:false

を使用することにより

- それはここで最初のクリックイベント

をトリガーdoesntのため、ここで

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

は、cancelイベントに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> 

あなたのコードで別のエラーは、このスニペットは。あなたはまだあなたとsrc属性を変更することにより、それを使用することができますので、それはを必要とされていないundefined errorがスローされますどのません何を指定していないということでした必要なタイプ。

/* 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からドロップされた画像を追跡するために使用され、ボタンには使用されません。

作業コードのためのスクリーンショットをここで見ることができます -

img2 img1

+0

どこがフィドルですか? jquery.ui-touch-punchを除外して、どのようにモバイルデバイスをサポートしていますか? –

+0

jquery.ui-touch-punchではなく、少し違ったボタンでドラッグ可能なクラスを使用しているので、上記のコードにfalseを追加してすべてのレシピを同じままにしてください –

+0

これは機能しません –

-1

に見てみてください。 like:button、select tagなどはデフォルトの動作をしています。クリックして何かをしようとすると、デフォルトのイベントが発生します。だから、あなただけcancel

についての詳細を見ると、ここだけcancel: falseをuseingした後、あなたのコードでリンクhttps://jsfiddle.net/L3j9qy4h/1/作業draggable()cancel: falseを使用して、そのデフォルトのイベントを防ぐために持っています。

関連する問題