我一直在审查jQuery和Javascript很长一段时间了 - 但我很难进入编程的思维模式。在找到之前的stackoverflow线程并将其修改为我为此学习练习所考虑的规范之后,我很难将每个div转换为可排序/可拖动的元素。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>seed</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
<style type='text/css'>
.redbox {
position: relative;
display: block;
width: 780px;
height: auto;
background: red;
}
.togglecontainer
{
width: 780px;
height: auto;
}
.handler {
width: 780px;
height: auto;
background: yellow;
}
.container {
position: relative;
width: 780px;
height: 780px;
background: blue;
}
.complete { text-decoration: line-through; color: grey;
}
input { padding:2px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; font-size:20px;}
</style>
<script type='text/javascript'>
// taken from http://jsfiddle.net/davidThomas/XsCAN/6/
// http://stackoverflow.com/questions/4639487/remove-specific-box-with-jquery
$(function(){
var counter = 0;
$('#creaters').click(function() {
counter++;
var redbox = $('<div class="redbox"><div class="handler"> ' + (counter) + ')<input name ="thestep" type="text" value="" maxlength="255" size="65" /></div><div class="togglecontainer"></div></div>').appendTo('.container'),
appendbtn = $('<button class="appendbtn">Add a link to a pic/vid</button>').appendTo(redbox.find('.togglecontainer')),
togglebtn = $('<button class="togglebtn">Hide options</button>').appendTo(redbox.find('.handler')),
deletelistbtn = $('<button class="deletelistbtn">X</button>').appendTo(redbox.find('.handler'));
redbox.draggable({
handle: '.handler'
});
appendbtn.click(function() {
var elems = $('<div><input value="some value" type="text" class="inputfield' + (counter) + '"/>' + '<input type="button" class="removebtn" value="x" id="removebtn"' + (counter) + '"/>' + '</div>').insertBefore(redbox.find('.togglecontainer'));
});
$('.removebtn').live('click', function() {
$(this).parent().remove();
});
$('.deletelistbtn').live('click', function() {
$(this).closest('.redbox').remove();
});
togglebtn.click(function() {
var that = this;
$('.togglecontainer', redbox).toggle(10, function() {
var txt = $(that).html();
$(that).html((txt == 'Hide options') ? 'add media' : 'Hide options');
});
});
togglebtn.trigger("click");
});
});
</script>
</head>
<body>
<button id="creaters">Add a Step</button>
<div class="container"></div>
</body>
</html>
目前代码将每个.redbox类转换为可拖动的jquery元素,但它们不会“捕捉”到位,保持在容器div的范围内,或者在被拖入新位置后移动它们下面的周围元素
到目前为止,我已经尝试在容器div中启动一个元素并将每个redbox元素转换为
我还尝试了draggable()和sortable()方法,但遇到了困难。
非常感谢任何帮助!
谢谢你!答案 0 :(得分:1)
好吧,我没有看到你在可拖动功能中添加一个快照和包含元素选项。
redbox.draggable({
handle: '.handler',
snap : '.container',
containment : '.container'
});
答案 1 :(得分:1)
请参阅here。
这是你的意思吗?
我使用了sortable:
$(function(){
var counter = 0;
$('.container').sortable();
$('#creaters').click(function() {