jquery拖动的元素不会停留在其放置的位置
jquery dragged element does not stay where it is dropped
尝试查看以下jsFiddle
http://jsfiddle.net/TtSub/1/
当我拖动"splitter"元素时,它不会停留在原地。
我在这里错过了什么?
Html
<div id="start"></div>
<div id="stop"></div>
<div id="container">
<div id="index" class="float"></div>
<div id="splitter" class="float"> </div>
<div id="content" class="float"></div>
</div>
Css
#container
{
width:600px;
height:400px;
}
#index
{
width:200px;
height:400px;
background-color:#dedede;
}
#splitter
{
width:5px;
height:400px;
cursor:w-resize;
background-color:#fff;
}
#content
{
width:395px;
height:400px;
background-color:#d1d1d1;
}
.float
{
float:left;
}
Javascript
jQuery(document).ready(function () {
$("#splitter").draggable({
axis: "x",
start: function (event, ui) {
// Show start dragged position of image.
var Startpos = $(this).position();
var startLeft = (Startpos.left - $("#container").position().left);
var startRight = (startLeft + $("#splitter").outerWidth());
$("#start").text("START: 'nLeft: " + startLeft + "'nTop: " + startRight);
},
stop: function (event, ui) {
// Show dropped position.
var Stoppos = $(this).position();
var stopLeft = (Stoppos.left - $("#container").position().left);
var stopRight = (stopLeft + $("#splitter").outerWidth());
$("#stop").text("STOP: 'nLeft: " + stopLeft + "'nTop: " + stopRight);
$("#index").css({ "width": stopLeft });
$("#content").css({ "width": ($("#container").outerWidth() - stopRight) });
}
});
});
我找到了一个解决方案,将css更改为绝对位置,并将html和javascript更改一点
Javascript
<script type="text/javascript">
jQuery(document).ready(function () {
$("#splitter").draggable({
axis: "x",
containment: "parent",
start: function (event, ui) {
// Show start dragged position of image.
var Startpos = $(this).position();
var startLeft = ($("#container").position().left - Startpos.left);
var startRight = (startLeft + $("#splitter").outerWidth());
$("#start").text("START: 'nLeft: " + startLeft + "'nTop: " + startRight);
},
stop: function (event, ui) {
// Show dropped position.
var Stoppos = $(this).position();
var stopLeft = (Stoppos.left);
var stopRight = (stopLeft + $("#splitter").outerWidth());
$("#stop").text("STOP: 'nLeft: " + stopLeft + "'nTop: " + stopRight);
$("#index").css({ "width": stopLeft });
$("#splitter").css({ "left": stopLeft });
$("#content").css({ "width": ($("#container").outerWidth() - stopRight), "left": stopRight });
}
});
});
</script>
Css
<style>
#container
{
width:1200px;
height:600px;
position:relative;
}
#index
{
width:200px;
height:600px;
position:absolute;
left:0;
background-color:#dedede;
}
#splitter
{
width:5px;
height:600px;
cursor:w-resize;
position:absolute;
left:200px;
background-color:#fff;
z-index:1;
}
#content
{
width:995px;
height:600px;
position:absolute;
left:205px;
background-color:#d1d1d1;
}
</style>
Html
<div id="start"></div>
<div id="stop"></div>
<div id="container">
<div id="index"></div>
<div id="splitter"></div>
<div id="content"></div>
</div>
相关文章:
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- Firefox,如何提交表单触发文件下载,但停留在网页上
- Jssor滑块停留在第一个图像上,直到我单击它
- 在位置更改时为阵列项目制作动画
- 在浏览器关闭时,单击确认对话框中的“停留在页面上”,执行一个方法
- 可以'在html中,没有任何东西可以停留在图片上方
- 如何在D3中添加一个强制拖动事件,并使节点停留在我离开它的地方
- javascript onmouseover/omouseout停留在前一个内容中,直到悬停/触发下一个内容
- 如果用户由于第二个处理程序中的对话框而停留在页面上,则跳过加载前处理程序
- 当结果点击值停留在文本框中时,自动完成搜索
- 使用Window_handles在位置弹出窗口中单击按钮,并带有守夜功能
- 使用JS暂时禁用在位置/元素处滚动
- 停留在页面上取决于值jquery
- Jquery:图像获胜'点击后不会改变,而是停留在悬停图像上.
- 我想在页面刷新时停留在当前光标位置
- 如何滚动并停留在特定元素所在的位置
- 在提交表单时停留在当前位置
- jquery拖动的元素不会停留在其放置的位置
- 如何使图像停留在它自己的位置,鼠标在上面
- jQuery 可拖动克隆不会停留在丢弃的位置