简体   繁体   English

JqueryUI,多个ID和其他关于droppable的语句,如何使它们工作?

[英]JqueryUI, multiple ID's and else if statements on droppable, how do I get them to work?

I have a interface where I have 3 cards, that when dragged on a board individually change something about the view on the website. 我有一个界面,其中有3张卡片,将其拖到板上时,会分别更改网站上的视图。 Its meant as a high fidelity prototype mock up. 它的意思是模拟高保真原型。

I can however not get more than one card to work as intended. 但是,我不能获得一张以上的卡才能正常工作。 I cannot see why it does not work, Ive tried with multiple functions and now multiple else if statements, nothing works as intended 我看不到为什么它不起作用,我尝试了多个功能,现在尝试了多个其他if语句,但没有按预期工作

The HTML looks like this; HTML看起来像这样;

<div class="cholder">
    <div class="card" id="1">
        <div class="cardid">TURN ON LIGHTS</div>
    </div>
    <div class="card" id="2">
        <div class="cardid">TURN ON TV</div>
    </div>
    <div class="card" id="3">
        <div class="cardid">OPEN DOOR</div>
    </div>
</div>

and the jquery as this; 和jQuery这样的;

$(".card").draggable({
    revert: function (event, ui) {

        $(this).data("uiDraggable").originalPosition = {
            top: 0,
            left: 0
        };
        return !event;
    }
});
$('.board').data('outside', 1).droppable({
    accept: '.card',
    out: function () {
        $(this).data('outside', 1);
    },
    over: function () {
        $(this).data('outside', 0);
    }
});

$('body').droppable({
    accept: '.card',
    drop: function (event, ui) {
        if ($('.board').data('outside') == 1) {
            if ($(this).is('#1')) {

                $('#my_content').toggle();
                $(".lamp").removeClass('toggled');
                $(".main").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });
            } else if ($(this).is('#2')) {

                $('#my_content').toggle();
                $(".tv").removeClass('toggled');
                $(".main").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });
            } else if ($(this).is('#3')) {

                $('#my_content').toggle();
                $(".door").removeClass('toggled');
                $(".main").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });
            }
        } else if ($('.board').data('outside') == 0) {
            if ($(this).is('#1')) {

                $('#my_content').toggle();
                $(".lamp").addClass('toggled');
                $(".main").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });;
            }
            if ($(this).is('#2')) {

                $('#my_content').toggle();
                $(".tv").addClass('toggled');
                $(".main").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });;
            }
            if ($(this).is('#3')) {

                $('#my_content').toggle();
                $(".door").addClass('toggled');
                $(".main").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });;
            }
        }
    }

});

JSFiddle Other Demo JSFiddle 其他演示

Worked it out, my if's and accepted was wrong with the proper IDs; 算出来,我的“如果”和“接受”的正确ID是错误的。 accept: '#1, #2, #3', --- if (ui.draggable.attr('id')=='1' 接受:'#1,#2,#3',---如果(ui.draggable.attr('id')=='1'

$('body').droppable({
    accept: '#1, #2, #3',
    drop: function (event, ui) {
        if ($('.board').data('outside') == 1) {
            if (ui.draggable.attr('id')=='1') {
                audioElement.play();
                $('#my_content').toggle();
                $(".lamp").removeClass('toggled');
                $(".board").addClass('toggled').delay(1000).queue(function () {
                    $(this).removeClass("toggled");
                    $(this).dequeue();
                });
            }

I am not pretty sure what you are trying to do exactly as the explanation and the fiddle is not clear, but from looking at your code, you can optimise it a lot better using data-attributes and the use of jQuery toggleClass 我不太确定您要按照解释的确切方式进行操作,并且弄不清楚,但是通过查看代码,可以使用data-attributes和jQuery toggleClass更好地对其进行优化。

Examine your code and you will that all of your if conditions are to add some sort of class specific to each ID. 检查您的代码, if条件是为每个ID添加某种特定的类,则将您所有的代码作为条件。 Then simply save that class in a data attribute and retrieve that using jQuery. 然后只需将该类保存在data属性中,然后使用jQuery检索该类。 Now, all the other functions like toggling content are repeated and then you add/remove classes which can be done using the toggleClass 现在,重复所有其他功能,例如切换内容,然后添加/删除类,可以使用toggleClass来完成

<div class="cholder">
    <div class="card" data-class="lamp" id="1">
        <div class="cardid">TURN ON LIGHTS</div>
    </div>
    <div class="card" data-class="tv" id="2">
        <div class="cardid">TURN ON TV</div>
    </div>
     .....
</div>

then all of your drop functions can be summarized in 那么您所有的放置功能都可以总结为

$('#my_content').toggle();
$('".'+ $(this).attr('data-class') +'"').toggleClass('toggled');
 // i dont know why you need this but still you can use toggleClass
$(".main").toggleClass('toggled');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM