2017-06-15 4 views
-1

私はあるセクションのレイアウトを選択し、そのレイアウトのコンテンツエリアに別の段落、ボタン、imgなどをドロップすると、並べ替えのエディタを作ろうとしています。 問題は、最初のdivの「レイアウト」をドロップしてそのdivにアイテムをドロップしようとすると、Zインデックスが間違っているように「背後」に配置されているため間違った廃棄可能エリアが選択されることです正しい領域が表示されます。divを落として、もう一度droppableにしてください

iはI'amコンソールにドロップ可能な領域のIDとドロップされた項目のログ記録、iはコードの例としてjsfiddle製: https://jsfiddle.net/g9aragsp/3/

私は「のいずれかの項目を追加するだろう"を"エディタ "の代わりに" insidedrop "に追加します。私はテスト目的のために最初のレイアウトでid "insidedrop"を設定します。


私のhtml:

<div class="container-fluid"> 
    <div class="row content"> 
     <div class="col-sm-8 col-sm-offset-2"> 
      <div class="row htmlEditor"> 
       <div class="col-sm-3" style="background-color: gray; min-height: 800px;"> 
        <p style="border-bottom: 5px solid black">Layouts</p> 

        <ul id="layouts" style="list-style-type: none;"> 
         <li id="layout1" class="PickerButton">layout1</li> 
         <li id="layout2" class="PickerButton">layout2</li> 
         <li id="layout3" class="PickerButton">layout3</li> 
        </ul> 

        <p style="border-bottom: 5px solid black">Items</p> 
        <ul id="items" style="list-style-type: none;"> 
         <li id="item1" class="PickerButton">button</li> 
         <li id="item2" class="PickerButton">img</li> 
         <li id="item3" class="PickerButton">text</li> 
        </ul> 

       </div> 

       <div id="editor" class="col-sm-9" style="background-color: lightgray; min-height: 800px; padding: 20px;"> 

       </div> 
      </div> 
     </div> 
    </div> 
</div> 


マイJavascriptを: $(関数(){

 var $layouts = $("#layouts"), 
      $items = $("#items"), 
      $insideDrop = $("#insideDrop"), 
      $trash = $("#editor"); 


     $("li", $layouts).draggable({ 
      cancel: "button", // these elements won't initiate dragging 
      revert: "invalid", // when not dropped, the item will revert back to its initial position 
      containment: "document", 
      helper: "clone", 
      cursor: "move" 
     }); 

     $("li", $items).draggable({ 
      cancel: "button", // these elements won't initiate dragging 
      revert: "invalid", // when not dropped, the item will revert back to its initial position 
      containment: "document", 
      helper: "clone", 
      cursor: "move" 
     }); 


     $trash.droppable({ 
      //accept: "#list1 > li", 
      drop: function (event, ui) { 
       console.log(ui.draggable.attr("id")); 
       console.log($(this).attr("id")); 
       if (ui.draggable.attr("id") == "layout1") 
        $("#editor").append("<div class='layout1Outer' style='min-height:400px; margin-bottom: 15px;'><div class='layout1imageplaceholder' style='background-color:#8a2be2;height:150px'></div><div class='layout1content' id='insideDrop' style='background-color:bisque;min-height:250px'></div></div>"); 

       if (ui.draggable.attr("id") == "layout2") 
        $("#editor").append("<div class='layout2Outer' style='min-height:400px; margin-bottom: 15px;'><div class='layout2imageplaceholder' style='background-color:#8a2be2;float:right;height:400px;width:40%'></div><div class='layout2content' style='background-color:bisque;float:left;min-height:400px;width:60%'></div></div>"); 

       if (ui.draggable.attr("id") == "layout3") 
        $("#editor").append("<div class='layout3Outer' style='min-height:400px; margin-bottom: 15px;'><div class='layout3imageplaceholder' style='background-color:#8a2be2;float:left;height:400px;width:40%'></div><div class='layout3content' style='background-color:bisque;float:right;min-height:400px;width:60%'></div></div>"); 


      } 
     }); 

     $insideDrop.droppable({ 
      drop: function (event, ui) { 
       if (ui.draggable.attr("id") == "item1") 
        $("#insideDrop").append("<span id='button' style='background-color:#dc143c;padding: 10px; width: 50px'>BUTTON</span>"); 

       if (ui.draggable.attr("id") == "item2") 
        $("#insideDrop").append("<img src='http://via.placeholder.com/50x50'>"); 

       if (ui.draggable.attr("id") == "item3") 
        $("#insideDrop").append("<p style='background- color: #284B63; padding: 10px; width: 200px; color: white;'>Insert text here</p>"); 


       console.log(ui.draggable.attr("id")); 
      } 
     }); 



    }); 
+0

'$ insideDrop'には要素がありません。 jqueryコレクションは公開されていません。 –

+0

あなたのコメントが正しいと分かっていれば、レイアウトの作成時に初期化する必要がありますか? –

+0

はい。しかし、その後、それはまだ動作しませんでした。しかし、少なくともそれは正しい方向への一歩です。現在、エディタのドロップ可能な部分がレイアウト内の部分よりも優先されるようになっています。 –

答えて

0

私は問題の解決策を考え出した代わりに、ネストされた "ドロップ可能" を標的とします。コンテナ。私は同じ関数を再利用し、ホバリングで現在のホバリングされたオブジェクトを返すjavacriptを作成し、次にその特定のオブジェクトに項目を追加します:

var hoveredObj; 

     function getid(obj) { 
      hoveredObj = obj; 
    } 


     var $layouts = $("#layouts"), 
      $items = $("#items"), 
      $insideDrop = $("#insideDrop"), 
      $editor = $("#editor"); 


     $("li", $layouts).draggable({ 
      cancel: "button", // these elements won't initiate dragging 
      revert: "invalid", // when not dropped, the item will revert back to its initial position 
      containment: "document", 
      helper: "clone", 
      cursor: "move" 
     }); 

     $("li", $items).draggable({ 
      cancel: "button", // these elements won't initiate dragging 
      revert: "invalid", // when not dropped, the item will revert back to its initial position 
      containment: "document", 
      helper: "clone", 
      cursor: "move" 
     }); 


     $editor.droppable({ 
      //accept: "#list1 > li", 
      drop: function (event, ui) { 

       console.log(ui.draggable.attr("id")); 
       console.log($(this).attr("id")); 
       if (ui.draggable.attr("id") == "layout1") 
        $("#editor").append("<div class='layout1Outer' style='min-height:400px; margin-bottom: 15px;'><div class='layout1imageplaceholder' style='background-color:#8a2be2;height:150px'></div><div class='layout1content' id='insideDrop' onmouseover='getid(this)' style='background-color:bisque;min-height:250px'></div></div>"); 

       if (ui.draggable.attr("id") == "layout2") 
        $("#editor").append("<div class='layout3Outer' style='min-height:400px; margin-bottom: 15px;'><div class='layout3imageplaceholder' style='background-color:#8a2be2;float:left;height:400px;width:40%'></div><div class='layout3content' style='background-color:bisque;float:right;min-height:400px;width:60%'></div></div>"); 

       if (ui.draggable.attr("id") == "layout3") 
        $("#editor").append("<div class='layout2Outer' style='min-height:400px; margin-bottom: 15px;'><div class='layout2imageplaceholder' style='background-color:#8a2be2;float:right;height:400px;width:40%'></div><div class='layout2content' style='background-color:bisque;float:left;min-height:400px;width:60%'></div></div>"); 


       if (ui.draggable.attr("id") == "item1") 
        $(hoveredObj).append("<span id='button' style='background-color:#dc143c;padding: 10px; width: 50px'>BUTTON</span>"); 

       if (ui.draggable.attr("id") == "item2") 
        $(hoveredObj).append("<img src='http://via.placeholder.com/50x50'>"); 

       if (ui.draggable.attr("id") == "item3") 
        $(hoveredObj).append("<p style='background- color: #284B63; padding: 10px; width: 200px; color: white;'>Insert text here</p>"); 

      } 
     }); 
関連する問題