【发布时间】:2016-07-27 10:33:35
【问题描述】:
我需要以下代码中的 CSS 进一步帮助 - 这是这篇文章的延续:
jQuery: Counter for draggables (again!)
因此,下面的代码旨在计算桌布上盘子的数量,如果您将一个盘子放在远离桌布的位置,那么计数器就会下降。
代码或多或少有效,但是,当您单击一个盘子拖动它时,该盘子会跳到不同的位置。当您将盘子放在桌布上时也会发生同样的情况:它不是落在您希望它着陆的地方,而是落在其他地方......非常烦人。 任何人都可以建议如何解决问题吗?
$(init);
function init() {
var j = 0;
$("#counter").find("h1").html("You keep: " + j);
$(".fish").draggable({
addClasses: true,
refreshPositions: true,
//When you start dragging you append to body mainly to remove
// from cloth div
start: function(e, ui) {
ui.helper.appendTo('body');
},
//You move your out logic to stop. So if the element hasn't been added
// to cloth, it won't be counter
stop: function(e, ui) {
j = $(".cloth .fish").length;
j = j + 0;
$("#counter").find("h1").html("You keep: " + j);
}
});
$(".cloth").droppable({
accept: ".fish",
tolerance: "fit",
drop: function(event, ui) {
//On drop you append fish to the cloth div so it's counted
$('.cloth').append(ui.helper.css({
position: 'absolute',
top: ui.helper.offset().top,
left: ui.helper.offset().left
}));
j = $(".cloth .fish").length;
j = j + 0;
$("#counter").find("h1").html("You keep: " + j);
},
});
}
.mybox {
width: 90%;
height: 10px;
position: absolute;
bottom: 10%;
margin-left: 5%;
text-align: center;
maring: auto;
background-color: orange;
}
#stop-top {
position: relative;
background-color: orange;
}
.fish {
margin: .5em;
font-size: 1.2em;
position: relative;
display: inline-block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
border-radius: 50%;
width: 50px;
height: 50px;
background: #BAB6B2;
float: left;
border: .3em solid #4B027C;
}
.cloth {
width: 90%;
height: 210px;
border-radius: 15px;
border: 5% solid rgba(200, 0, 0, .5);
margin: 0 auto;
background-color: white;
background-image: linear-gradient(90deg, rgba(200, 0, 0, .5) 50%, transparent 50%), linear-gradient(rgba(200, 0, 0, .5) 50%, transparent 50%);
background-size: 20px 20px;
}
.ui-draggable-dragging {
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div class='mybox' style='top:5%;height:70%;'>
<div id="counter" style="margin-left:5%;">
<h1>You keep: 0</h1>
</div>
<div class="cloth"></div>
<div class="fish" style="margin-left: 5%;"></div>
<div class="fish"></div>
<div class="fish"></div>
<div class="fish"></div>
<div class="fish"></div>
<div class="fish"></div>
</div>
<div class='mybox' style='height:20%; background-color:blue;'>
<button type="button" class="btn btn-primary " name="buttonSend">
<span class="glyphicon glyphicon-ok"></span> Ok
</button>
</div>
感谢您的帮助! 最好的,
【问题讨论】:
标签: javascript jquery css jquery-ui