Jquery 拖放
您好,我是 jQuery 新手,正在尝试进行概念验证。我在这里发布了示例代码,我的要求如下。
我有 2 只可掉落的狗/羊,我想将它们放在衬衫上。
我将羊/狗放在衬衫上,但一旦掉落,它就会到达 div 的左下位置。
我尝试做 $('[id$="apparel"]').append(dropElem);
而不是 $(this).append(dropElem);
可以一些请帮助我吗?
这是我的 javascript
$(".items").draggable({helper: 'clone'});
$(".droparea").droppable({
accept: ".items",
hoverClass: 'dropareahover',
tolerance: 'touch',
drop: function(ev, ui) {
var dropElemId = ui.draggable.attr("id");
var dropElem = ui.draggable.html();
$(this).append(dropElem);
alert("drop done");
}
});
和 HTML
<div class="wrap">
<div class="sourcearea">
<span class="items" id="itemsheep">
<img src="sheep.jpg" width="100">
</span>
<span class="items" id="itemdog">
<img src="dog.jpg" width="100">
</span>
</div>
<div class ="droparea">
<img src="RedShirt.jpg" width="300" height="300" id="apparel">
</div>
</div>
Hi I am new to jQuery and trying work on a proof of concept. I have posted the example code here and my requirement is as follows.
I have 2 droppables dog/sheep and I want to drop them on the shirt.
I drop the sheep/dog on the shirt but as soon as drop it goes to the down-left position of the div.
I tried doing $('[id$="apparel"]').append(dropElem);
instead of $(this).append(dropElem);
Can some one please help me in this?
this is my javascript
$(".items").draggable({helper: 'clone'});
$(".droparea").droppable({
accept: ".items",
hoverClass: 'dropareahover',
tolerance: 'touch',
drop: function(ev, ui) {
var dropElemId = ui.draggable.attr("id");
var dropElem = ui.draggable.html();
$(this).append(dropElem);
alert("drop done");
}
});
And the HTML
<div class="wrap">
<div class="sourcearea">
<span class="items" id="itemsheep">
<img src="sheep.jpg" width="100">
</span>
<span class="items" id="itemdog">
<img src="dog.jpg" width="100">
</span>
</div>
<div class ="droparea">
<img src="RedShirt.jpg" width="300" height="300" id="apparel">
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您真正打算在这里做什么,但似乎您正在尝试将“ITEM”类的内容附加到可删除类中。我这么说是因为这就是你的代码现在实际上正在做的事情。
如果是这种情况,您应该在“item”类上放置一个“float:left”属性。这将使它们从左侧开始水平并排整齐地堆叠(您可能还需要在每个项目上留一点右边距,以防止它们的边缘相互接触)。如果新项目的大小小于可用的水平空间,它将向下滚动到可放置的左边缘。
如果您希望项目的“克隆”“保持在放置它们的位置”,您可以使用“ui.position”属性来保存它们相对于可放置的位置。
如果您希望实际的可拖动对象“粘”到您的可放置对象上,请删除这些行:
完成了!您可能还想删除该项目的可拖动属性以“修复”衬衫内的项目。
希望有帮助! ;-)
I'm not sure what you're really planning to do here but it seems you are trying to append the content of the "ITEM" classes inside your droppable class. I said that because that is what your code is actually doing right now.
If that is that case, you should put a "float:left" property on your "item" class. This will neatly stack them horizontally side by side starting on the left (You may also want to put a little right margin with each item to prevent their edges from touching each other). If the size for new item is less than the available horizontal space it will wrap down to the left edge of your droppable.
If you want the "clone" of your items to "stay in place" where you dropped them, you can use the "ui.position" property to save their position relative to the droppable.
If you want the actual draggables to "stick" to your droppable, remove these lines:
And it's done! You may also want to remove the draggable property of the item to "fix" the item inside your shirt.
Hope that helps! ;-)
放下后查看DIV上的样式。它是否绝对定位,是否浮动等等?
另外,请查看我几个月前遇到的类似问题(加上解决方案)Stack Overflow 上。它讨论了将放置的元素“捕捉”到容器的不同解决方案。
Check out the style on the DIV after dropping. It it positioned absolute, does it float, etc?
Also, check out a similar problem (plus solution) I had a few months ago here on Stack Overflow. It talks about a different solution to "snapping" the dropped element to a container.