2016-07-16 2 views
1

получил немного проблемы, пытаясь использовать JQuery UI Drag & Drop, когда я падение перемещаемого элемента, это происходит далеко далеко, в правом нижнем углу, даже не я окно ...JQuery UI Drag & падение элемент прыжок

codepen

$(function() { 
    var $gameBox = $(".gameBox"), 
    $newElemWrapper = $(".newElemWrapper"), 
    $trash = $(".trash"), 
    $list = $(".sortElements"), 
    $newElem = null, 
    colors = ["red", "limeGreen", "crimson", "white", "yellow"], 

    randNum = function() { 
     return Math.floor(Math.random() * colors.length); 
    }, 

    $addElem = function() { 
     $newElem = $("<li></li>") 
     .addClass("newElem") 
     .css("display", "none") 
     .css("background-color", colors[randNum()]) 
     .appendTo($newElemWrapper) 
     .fadeIn(); 

     return $newElem; 
    }, 

    $moveElem = function($elem) { 
     $elem.fadeOut(function() { 
     $elem.appendTo($list).fadeIn(); 
     }); 
    }; 

    (function go() { 

    $addElem().draggable({ 
     revert: "invalid", 
     containment: $gameBox, 
     stack: $trash 
    }); 

    $trash.droppable({ 
     accept: $(".newElemWrapper > li"), 
     drop: function(evt, ui) { 
     $moveElem(ui.draggable); 
     setTimeout(go, 500); 
     } 
    }); 
    }()); 
}); 

Если кто-то поможет, будьте очень благодарны. Сan't понять, что я делаю неправильно

ответ

1

Добавить:

position: fixed; 

в классе newElem.

Отрывок:

$(function() { 
 
    var $gameBox = $(".gameBox"), 
 
     $newElemWrapper = $(".newElemWrapper"), 
 
     $trash = $(".trash"), 
 
     $list = $(".sortElements"), 
 
     $newElem = null, 
 
     colors = ["red", "limeGreen", "crimson", "white", "yellow"], 
 

 
     randNum = function() { 
 
     return Math.floor(Math.random() * colors.length); 
 
     }, 
 

 
     $addElem = function() { 
 
     $newElem = $("<li></li>") 
 
     .addClass("newElem") 
 
     .css("display", "none") 
 
     .css("background-color", colors[randNum()]) 
 
     .appendTo($newElemWrapper) 
 
     .fadeIn(); 
 

 
     return $newElem; 
 
     }, 
 

 
     $moveElem = function($elem) { 
 
     $elem.fadeOut(function() { 
 
      $elem.appendTo($list).fadeIn(); 
 
     }); 
 
     }; 
 

 
    (function go() { 
 

 
    $addElem().draggable({ 
 
     revert: "invalid", 
 
     containment: $gameBox, 
 
     stack: $trash 
 
    }); 
 

 
    $trash.droppable({ 
 
     accept: $(".newElemWrapper > li"), 
 
     drop: function(evt, ui) { 
 
     $moveElem(ui.draggable); 
 
     setTimeout(go, 500); 
 
     } 
 
    }); 
 
    }()); 
 
});
.gameBox { 
 
    width: 90%; 
 
    height: 500px; 
 
    background: cornflowerblue; 
 
    border-radius: 5px; 
 
    margin: 0 auto; 
 
    position: relative; 
 
} 
 

 
.newElemWrapper { 
 
    position: absolute; 
 
    top: 50px; 
 
    left: 50px; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
.newElem { 
 
    position: fixed; 
 
    display: block; 
 
    width: 80px; 
 
    height: 80px; 
 
    border-radius: 5px; 
 
} 
 

 
.trash { 
 
    width: 300px; 
 
    height: 100px; 
 
    background: lightcoral; 
 
    border-radius: 5px; 
 
    position: absolute; 
 
    bottom: 50px; 
 
    right: 50px; 
 
}
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
 

 

 
<div class="gameBox"> 
 
    <ul class="newElemWrapper"></ul> 
 

 
    <div class="trash"> 
 
     <ul class="sortElements"> 
 
      <li class="newElem"></li> 
 
      <li class="newElem"></li> 
 
      <li class="newElem"></li> 
 
     </ul> 
 
    </div> 
 
</div>

+0

это работает! Спасибо большое! – vanless

+0

@vaniaDrach Большое спасибо и приветствую на SO – gaetanoM

+0

done) спасибо еще раз =) – vanless

Смежные вопросы