jQuery Draggable / Droppable 定位组件

发布于 2024-11-01 16:34:25 字数 1522 浏览 1 评论 0原文

我有一个可拖动/可放置的库存设置。它基本上是一个拼图,但是是一个简单的拼图,正确的碎片会卡在正确的区域。我把折断和拼图碎片都放下来了。然而,当我将片段 1 拖到位置 2 上时,它并没有显示为无效。如何区分目标区域?

HTML:

<div id="drag">
    <div id="piece-drag">
        <div id="piece1">
            <img src="anim/hoffmann3-anklebridge/pin 1.png" />
        </div>
        <div id="piece2">
            <img src="anim/hoffmann3-anklebridge/pin 2.png" />
        </div>
    </div>
    <div id="place-drop" style="background:url(puzzleshape.png) no-repeat;">
        <div id="piece1drop" style="width:103px; height:36px; position:absolute; z-index:50; top:346px; left:241px;"></div>
        <div id="piece2drop" style="width:103px; height:36px; position:absolute; z-index:50; top:333px; left:228px;"></div>
    </div>
</div>

JQuery:

$(function(){
            $("#piece1").draggable({ revert: "invalid" });
            $("#piece1drop").droppable({
                drop: function() { 
                    $('#piece1').hide();
                    $('#piece1drop').css('background-image', 'url("piece1placed.png")');
                }
            });
            $("#piece2").draggable({ revert: "invalid" });
            $("#piece2drop").droppable({
                drop: function() { 
                    $('#piece2').hide();
                    $('#pin2drop').css('background-image', 'url("piece2placed.png")');
                }
            });
        });

I have a stock setup for draggable/droppable. It's basically going to be a jigsaw, but an easy one where the correct pieces snap to the correct areas. I have the snapping and the jigsaw pieces down. However, when I drag piece 1 over place 2 it doesn't show it as being invalid. How do I differentiate between targeting zones?

HTML:

<div id="drag">
    <div id="piece-drag">
        <div id="piece1">
            <img src="anim/hoffmann3-anklebridge/pin 1.png" />
        </div>
        <div id="piece2">
            <img src="anim/hoffmann3-anklebridge/pin 2.png" />
        </div>
    </div>
    <div id="place-drop" style="background:url(puzzleshape.png) no-repeat;">
        <div id="piece1drop" style="width:103px; height:36px; position:absolute; z-index:50; top:346px; left:241px;"></div>
        <div id="piece2drop" style="width:103px; height:36px; position:absolute; z-index:50; top:333px; left:228px;"></div>
    </div>
</div>

JQuery:

$(function(){
            $("#piece1").draggable({ revert: "invalid" });
            $("#piece1drop").droppable({
                drop: function() { 
                    $('#piece1').hide();
                    $('#piece1drop').css('background-image', 'url("piece1placed.png")');
                }
            });
            $("#piece2").draggable({ revert: "invalid" });
            $("#piece2drop").droppable({
                drop: function() { 
                    $('#piece2').hide();
                    $('#pin2drop').css('background-image', 'url("piece2placed.png")');
                }
            });
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-11-08 16:34:25

添加 accept: "#selector" 作为 droppable 插件的选项:

$(function(){
            $("#piece1").draggable({ revert: "invalid" });
            $("#piece1drop").droppable({
                drop: function() { 
                    $('#piece1').hide();
                    $('#piece1drop').css('background-image', 'url("piece1placed.png")');
                },
                accept: "#piece1"
            });
            $("#piece2").draggable({ revert: "invalid" });
            $("#piece2drop").droppable({
                drop: function() { 
                    $('#piece2').hide();
                    $('#pin2drop').css('background-image', 'url("piece2placed.png")');
                },
                accept: "#piece2"
            });
    });

这是 jQuery 演示

Add accept: "#selector" as an option to the droppable plugin:

$(function(){
            $("#piece1").draggable({ revert: "invalid" });
            $("#piece1drop").droppable({
                drop: function() { 
                    $('#piece1').hide();
                    $('#piece1drop').css('background-image', 'url("piece1placed.png")');
                },
                accept: "#piece1"
            });
            $("#piece2").draggable({ revert: "invalid" });
            $("#piece2drop").droppable({
                drop: function() { 
                    $('#piece2').hide();
                    $('#pin2drop').css('background-image', 'url("piece2placed.png")');
                },
                accept: "#piece2"
            });
    });

Here's the jQuery Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文