【问题标题】:Change 2 image each other when click on them单击它们时相互更改2个图像
【发布时间】:2014-03-27 16:19:48
【问题描述】:

我有一个内部有多个图像的 div,我需要单击随机图像,然后再次单击随机图片,当我单击第二张图像以相互更改图像时。所有图像都是可互换的。这是我到目前为止所做的:

编辑 FIDDLE: http://jsfiddle.net/w53Ls/5/

$("#image1").click(function(){
    imagePath = $("#image2").attr("src");
    if(imagePath == "http://s15.postimg.org/oznwrj0az/image.jpg"){
        $("#image3").attr("src", "http://s21.postimg.org/ojn1m2eev/image.jpg");
    }else{
        $("#image4").attr("src", "http://s23.postimg.org/epckxn8uz/image.jpg");        
    }
});

编辑: 我为检查功能尝试过的代码在 EDIT FIDDLE 中,并带有警报,我检查图片的 src。现在我只需要创建一个条件在我按顺序更改所有部分并找到整个图片后提醒一些东西。有什么提示吗?

【问题讨论】:

    标签: javascript jquery arrays onclick swap


    【解决方案1】:

    DEMO

    var clickCount = 0;
    var imgSrc;
    var lastImgId;
    $("img.element").click(function(){
        if (clickCount == 0)
        {
            imgSrc = $(this).attr("src");
            lastImgId = $(this).attr("id");
            clickCount++;
        }
        else {
            $("#"+lastImgId).attr("src",$(this).attr("src"));
            $(this).attr("src",imgSrc)
            clickCount = 0;
        }
    });
    

    更新

    当你完成拼图时,这会让你知道

    DEMO

    var clickCount = 0;
    var imgSrc;
    var lastImgId;
    
    // Function for Comparing Arrays
    // source: http://stackoverflow.com/questions/7837456/
    Array.prototype.compare = function (array) {
        if (!array) return false;
    
        if (this.length != array.length) return false;
    
        for (var i = 0, l = this.length; i < l; i++) {
            if (this[i] instanceof Array && array[i] instanceof Array) {
                if (!this[i].compare(array[i])) return false;
            } else if (this[i] != array[i]) {
                return false;
            }
        }
        return true;
    }
    
    $(document).ready(function () {
    
        // Store the correct order first in an array.
        var correctOrder = $("#puzzle > img").map(function () {
            return $(this).attr("src");
        }).get();
    
        // Randomize your images
        var a = $("#puzzle > img").remove().toArray();
        for (var i = a.length - 1; i >= 1; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var bi = a[i];
            var bj = a[j];
            a[i] = bj;
            a[j] = bi;
        }
        $("#puzzle").append(a);
    
        $("img.element").click(function () {
            if (clickCount == 0) {
                imgSrc = $(this).attr("src");
                lastImgId = $(this).attr("id");
                clickCount++;
            } else {
                $("#" + lastImgId).attr("src", $(this).attr("src"));
                $(this).attr("src", imgSrc);
                clickCount = 0;
    
                // Get the current order of the images
                var currentOrder = $("#puzzle > img").map(function () {
                    return $(this).attr("src");
                }).get();
    
                // Compare the current order with the correct order
                if (currentOrder.compare(correctOrder)) alert("Puzzle completed");
            }
        });
    });
    

    【讨论】:

    • 这个太完美了!谢谢
    • 如果我有 16 个方块和 doc 上的随机播放功能,然后用户安排所有部分以获得反馈并停用交换图像的能力,此代码是否有效?
    • 它应该只要图像上有ids 并且属于element
    • 我要编辑小提琴并将其发布以查看我的整个代码。
    • 我现在看到了一个问题...当我更改图片时,ID 没有改变:-o
    【解决方案2】:

    http://jsfiddle.net/w53Ls/2/

    var counter = 0;
    

    代码是即兴的,但可以工作 XD

    你尝试改进它

    【讨论】:

    • 没关系,我无法使用它,因为有人先回答,我检查了他的回答并接受了它。现在我只需要在所有排列好的碎片和用户发现整个图像时给出反馈(我必须说我有 16 个方块)
    【解决方案3】:

    这是一个new version of your jsfiddle,我认为它可以满足您的需求。

    它将相同的单击处理程序应用于每个具有类swapable 的对象。每次单击可交换元素时,处理程序都会检查之前是否先单击了另一个元素。如果是这样,它会交换它们。如果不是,它只记得这个元素是第一个。

    var firstId = ''; // Initially, no element has been clicked first
    var firstSrc = '';
    // Apply the following function to every element with 'class="swapable"
    $('.swapable').click(function(){
      if (firstId !== '') { // There is already a first element clicked
        // Remember the information of the currently clicked element
        var secondId = $(this).attr('id');
        var secondSrc = $(this).attr('src');
        // Replace the currently clicked element with the first one
        $('#' + secondId).attr('src', firstSrc);
        // Replace the first one with the current one
        $('#' + firstId).attr('src', secondSrc);
        // Forget the first one, so that the next click will produce a new first
        firstId = '';
        firstSrc = '';
      }
      else // This is the first element clicked (this sequence)
      {
        // Remember this information for when a second is clicked
        firstId = $(this).attr('id');
        firstSrc = $(this).attr('src');
      }
    });
    

    【讨论】:

    • 它太棒了。糟糕的是我不能接受两个答案:(但是当我得到 15 个代表点时我会投票 +1。谢谢你的时间
    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多