javascript - JQuery - 如果在页面上检测到 x 个类,则执行 y

标签 javascript jquery jquery-ui jquery-ui-draggable jquery-ui-droppable

我正在为客户制作一个拖放匹配游戏。

问题 1:当所有图像都与文字匹配后,如何才能显示一个弹出窗口,显示“游戏结束”或类似内容?

问题 2:图像与其文字匹配后,是否可以使图像和文字同时淡出 View (这样页面就不会被已匹配的图像弄乱)?

这是我的 JavaScript:

$("#bottle, #butterfly").draggable({ revert: "invalid", containment: "#wrapper"});
$("#bottleDrop").droppable({
    accept: "#bottle",
    drop: function(event, ui) {
        if(ui.draggable.is("#bottle")){
            $(this).addClass("correct").find("p").html("correct!");
        }
    }
});
$("#butterflyDrop").droppable({
    accept: "#butterfly",
    drop: function(event, ui) {
        if(ui.draggable.is("#butterfly")){
            $(this).addClass("correct").find("p").html("correct!");
        }
    }
});

JSFiddle

最佳答案

这是一种实现方法:

var done = true;    
    $("#bottle, #butterfly").draggable({ 
        revert: "invalid", containment: "#wrapper",
        start: function(event, ui){
        if(!done)
            return false;
        },
        stop: function(event, ui){
            if($(".correct").length == $(".drop").length){
                setTimeout(function(){
                    alert('All items have been matched!');
                },2000);
            }
        }
    });
    $("#bottleDrop").droppable({
        accept: "#bottle",
        drop: function(event, ui) {
            if(ui.draggable.is("#bottle")){
                $(this).addClass("correct").find("p").html("correct!");
                ui.draggable.fadeOut(2000);
                $(this).fadeOut(2000);
            }
        }
    });
    $("#butterflyDrop").droppable({
        accept: "#butterfly",
        drop: function(event, ui) {
            if(ui.draggable.is("#butterfly")){
                $(this).addClass("correct").find("p").html("correct!");
                done = false;
                ui.draggable.fadeOut(2000);
                $(this).fadeOut(2000,function(){
                    done = true;
                });
            }
        }
    });

示例:

http://jsfiddle.net/trevordowdle/Uf8Tq/2/

关于javascript - JQuery - 如果在页面上检测到 x 个类,则执行 y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539077/

相关文章:

jquery - slider 和溢出问题

jquery - 如何将自定义 html 关闭按钮添加到 jQuery 对话框主体?

没有 jQueryUI 的 jQuery 模态对话框

javascript - javascript new Date() 构造函数对相同不同格式字符串的不明确行为

javascript - 如何在 for 循环中创建 Promise 后使用 Promise.all()

javascript - 使用AJAX获取JSON数据,然后修改raphael.js脚本

javascript - jQuery : Append a non-breaking space to content of Textarea

javascript - AngularJS 和 jQuery-UI Sortable 不起作用

javascript - 通过循环动态显示html元素

javascript - 原型(prototype)链获取不到Object()?