基于其位置的可拖动 DIV 样式
Draggable-DIV styling based on its position
我正在尝试拖动一个div,在拖动过程中更改其样式:如果拖动的div 有一个"左边框",其位置<其容器宽度的 是=" 有一个="左边框">其容器宽度的 50%)只有"右边框"是 8px。这是一个演示。其容器宽度的>
网页代码:
<div id="container">
<div class="draggable right">
<span id="border_left_position_percentage"></span>
</div>
</div>
CSS代码:
body {
border: 0;
margin: 0;
padding: 0;
}
#container {
position: relative;
left: 0;
right: 0;
width: 100%;
}
.draggable {
width: 100px;
height: 100px;
background-color: yellow;
}
.right {
position: absolute;
float: right;
right: 0;
border-right: 8px solid blue;
border-radius: 10px 0 0 10px;
}
#border_left_position_percentage {
padding: 20px;
display: none;
}
JavaScript 代码:
$(function () {
$(".draggable").draggable();
if ($(".draggable").draggable()) {
$(".right").css("cursor", "all-scroll");
} else {
$(".right").css("cursor", "normal");
}
$(".right").on('dragstart', function () {
$(this).on("mousemove", function () {
var percentuale = parseInt((($(this).position().left / $(this).parent().width()) * 100), 10);
$("#border_left_position_percentage").html(percentuale + "%");
if (percentuale < 50) {
$(this).css({
"border-right": "none",
"border-left": "8px solid blue",
"border-radius": "0 10px 10px 0"
});
$("#border_left_position_percentage").css({
"float": "right",
"display": "block"
});
} else {
$(this).css({
"border-right": "8px solid blue",
"border-left": "none",
"border-radius": "10px 0 0 10px"
});
$("#border_left_position_percentage").css({
"float": "left",
"display": "block"
});
}
});
}).on("dragstop", function () {
var percentuale = parseInt((($(this).position().left / $(this).parent().width()) * 100), 10);
if (percentuale < 50) {
$(this).animate({
"position": "absolute",
"float": "left",
"left": "0",
"border-radius": "0 10px 10px 0"
}, 2000, function () {
// Animation complete.
$("#border_left_position_percentage").css("display", "none");
});
} else {
$(this).animate({
"position": "absolute",
"float": "right",
"right": "0",
"border-radius": "10px 0 0 10px"
}, 2000, function () {
// Animation complete.
$("#border_left_position_percentage").css("display", "none");
});
}
});
});
我只想在拖动过程中显示"左边框"位置的百分比,将其隐藏在"拖动停止"(释放时),在其容器的左边框或右边框上绑定"可拖动div",始终基于"左边框"位置......
我不知道为什么,但是在我的演示中,"draggable-div"始终在容器的左侧绑定自己,如果百分比为>50%,并且百分比在鼠标悬停时显示,如果我使用"显示:无;",发布后也是如此。
多亏了你
我不知道
为什么jQuery不把它动画化到右边。 但是您可以通过按此量向左添加动画来模拟行为:
$('body').width() - $(this).width() - 8
数字 8 表示 8px 边框。
鼠标悬停时百分比重新出现的原因是由于以下代码:
$(this).on("mousemove", function () {
...
$("#border_left_position_percentage").css({
...
"display": "block"
});
即使它是在 dragstart
事件中定义的,它也会附加到元素,因此每当鼠标移到元素上时,它总是被触发。
在我的代码中,我已将mousemove
事件移到dragstart
事件之外,并且不更改显示 CSS:
$('.right').on('mousemove', function() {
var percentuale = parseInt((($(this).position().left / $(this).parent().width()) * 100), 10);
$("#border_left_position_percentage").html(percentuale + "%");
});
我还将mousemove
事件的逻辑/CSS移动到dragstop
事件。
dragstart
事件现在很简单:
$('#border_left_position_percentage').css('display','block');
$(this).stop();
这将显示元素,然后在拖动时由于mousemove
事件而更新该元素。 它还可以停止任何正在进行的动画,因此您可以在盒子移动时抓住它。
小提琴
而不是:
"left": $('body').width() - $(this).width() - 8,
可以使用:
'left': $('body').width() - $(this).outerWidth(true),
"true"表示还包括水平边距(资源)。
JSFiddle
相关文章:
- HTML5 DIV在Chrome和Firefox中不可拖动
- Div元素并没有引导点击以启用拖动功能
- 无法拖动动态添加的 DIV
- 使用 JavaScript 鼠标事件拖动 DIV 会快速移动
- 可拖动的 JQuery DIV 不起作用
- 通过拖动移动动态创建的 DIV
- 使用 javascript 的可拖动 DIV
- 选定的 HTML DIV 元素可拖动 JQueryUi 可拖动
- 基于其位置的可拖动 DIV 样式
- Div 在可拖动的 Div 内
- jQuery 拖动调整相邻 DIV 的大小
- 动态添加的 DIV,具有拖动和调整大小功能
- 同时拖动多个DIV,无需单击或按Ctrl单击
- 绑定在knockoutjs中拖动DIV元素属性到对象属性
- 当我拖动一个DIV时,如何传递一个id的值给一个函数
- 使DIV关闭图标与DIV一起拖动
- 保存可拖动Div位置
- 当拖动包含数千个子节点的Div元素时,提高浏览器性能
- 如何拖动DIV,使其宽度和高度为负值
- IE兼容拖动可滚动的Div