Pau*_*aul 7 javascript jquery jquery-ui draggable
我有两个要素:
1.身高固定的父母,overflow:hidden
2.它的孩子更大的固定高度.
<style type="text/css">
.myList {list-style:none; margin:0px; padding:1px;}
.myList li{ height:50px; margin:4px; padding:2px;}
.dragPanel {height:230px; border:solid; overflow:hidden;}
</style>
<div class="dragPanel">
<ul class="myList">
<li>1</li>
<li>2</li>
... ....
<li>8</li>
<li>9</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
我希望能够在父div中上下拖动列表.我使用jquery ui draggable插件来实现Verticle拖动,但我不确定如何约束父div中的列表.
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
<script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.draggable.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.dragPanel').addClass("hover");
$('.myList').draggable({ axis: 'y' });
});
</script>
Run Code Online (Sandbox Code Playgroud)
我如何为列表的位置设置Verticle上限和下限?
使用包含选项:
$('.myList').draggable({
axis: 'y',
containment: 'parent'
});
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,答案对我没有帮助.一些javascript以后我认为以下是一个更好的解决方案.
(有人知道如何把它变成一个jQuery插件吗?)
<!DOCTYPE html>
<html>
<head>
<title>Draggable limited to the container</title>
<style type="text/css">
#container {
cursor: move;
overflow: hidden;
width: 300px;
height: 300px;
border: 1px solid black;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$.globalVars = {
originalTop: 0,
originalLeft: 0,
maxHeight: $("#draggable").height() - $("#container").height(),
maxWidth: $("#draggable").width() - $("#container").width()
};
$("#draggable").draggable({
start: function(event, ui) {
if (ui.position != undefined) {
$.globalVars.originalTop = ui.position.top;
$.globalVars.originalLeft = ui.position.left;
}
},
drag: function(event, ui) {
var newTop = ui.position.top;
var newLeft = ui.position.left;
if (ui.position.top < 0 && ui.position.top * -1 > $.globalVars.maxHeight) {
newTop = $.globalVars.maxHeight * -1;
}
if (ui.position.top > 0) {
newTop = 0;
}
if (ui.position.left < 0 && ui.position.left * -1 > $.globalVars.maxWidth) {
newLeft = $.globalVars.maxWidth * -1;
}
if (ui.position.left > 0) {
newLeft = 0;
}
ui.position.top = newTop;
ui.position.left = newLeft;
}
});
});
</script>
</head>
<body>
<div id="container">
<img id="draggable" src="avatar.jpg" />
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
好的,
这是我走过的路线...
加载页面后,我在可拖动列表周围添加一个容器 div。我将其高度设置为列表高度的两倍,然后将其在页面上稍微定位:
<script type="text/javascript">
$(document).ready(function() {
CreateContainerDiv();
$('.dragPanel').addClass("hover");
$('.myList').draggable({ axis: 'y', containment: 'parent' });
});
function CreateContainerDiv() {
var dragPanel = $('.dragPanel');
var dragTarget = $('.myList');
// add the container panel
var newPanelHeight = dragTarget.outerHeight() * 2 - dragPanel.outerHeight();
dragTarget.wrap(document.createElement("div"));
// set its height and put it in the right place
dragTarget.parent().css("height", newPanelHeight);
dragTarget.parent().css("position", "relative");
var newPanelPosition = ((dragTarget.outerHeight() * -1) / 2);
dragTarget.parent().css("top", newPanelPosition);
}
</script>
Run Code Online (Sandbox Code Playgroud)
然后该列表包含在这个新元素中。
这似乎可以完成工作,但是如果列表应用了任何填充/边距,则定位会有点不稳定。任何对此的想法将不胜感激!
- - - 编辑 - - - - -
好的,我想我已经解决了放置问题。我已将其变成一个插件,这样其他人就不必花时间创建此功能。
Github 上的 jQuery Drag-gable 列表
| 归档时间: |
|
| 查看次数: |
27073 次 |
| 最近记录: |