jquery ui draggable限制div在收容内上下拖动

时间:2016-04-15 14:11:44

标签: javascript jquery-ui

我最近一直在学习Javascript。作为练习,我想创建一个Domino板。我试图在电路板上拖一块瓷砖。我采用的方法是在电路板内创建一个磁贴,并使用jquery ui将其设置为draggable。 然而,不知何故,阻力仅限于上下运动。为什么???

HTML

   <div id="board">
      <div id="tile_1-0" > 
        <div class="dominoe"> </div>
      </div>
   </div>

的CSS:

.dominoe {
/* Dominoe shape */
    position: relative;
    height:60px;
    width:30px;
    background-color:white;
    border: 2px solid black;

/* Rounded Corners */
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    padding:2px;    
}

#board {
        margin: auto;
        width: 200px;
        height: 200px;
        background-color: #0A9D2D;
}

的javascript:

$( "#tile_1-0" ).draggable({containment:'#board'});

请看一下嵌入式小提琴: https://jsfiddle.net/totoorozco/t5nnd95j/

2 个答案:

答案 0 :(得分:0)

因为它所在的div占据了“板”的整个宽度。通过将该div的显示设置为内联块来修复此问题:

#tile_1-0 {
  display: inline-block;
}

<强> jsFiddle example

答案 1 :(得分:0)

将此添加到您的css:

#tile_1-0{
   position: absolute;
}