简体   繁体   English

Jquery - 如何为多个元素制作绑定点击事件?

[英]Jquery - How to make bind click event for multiple elements?

Iam using jquery to animate an image to make a mixed up image that scrolls different parts when clicked.我正在使用 jquery 对图像进行动画处理,以制作一个混合图像,单击时会滚动不同的部分。

I thought of doing like this我想这样做

$("#head").click(function () {
        if (headclix < 9) {
            $("#head").animate({
                left: "-=367px"
            }, 500);
            headclix++;
        } else {
            $("#head").animate({
                left: "0px"
            }, 500);
            headclix = 0;
        }
    });

    $("#eyes").click(function () {
        if (eyeclix < 9) {
            $("#eyes").animate({
                left: "-=367px"
            }, 500);
            eyeclix++;
        } else {
            $("#eyes").animate({
                left: "0px"
            }, 500);
            eyeclix = 0;
        }
    });

    $("#nose").click(function () {
        if (noseclix < 9) {
            $("#nose").animate({
                left: "-=367px"
            }, 500);
            noseclix++;
        } else {
            $("#nose").animate({
                left: "0px"
            }, 500);
            noseclix = 0;
        }
    });

    $("#mouth").click(function () {
        if (mouthclix < 9) {
            $("#mouth").animate({
                left: "-=367px"
            }, 500);
            mouthclix++;
        } else {
            $("#mouth").animate({
                left: "0px"
            }, 500);
            mouthclix = 0;
        }
    });

I hope there is better way of doing it我希望有更好的方法

I'm thinking I can do something with the class and each but not sure how to quite make it work.我想我可以对班级和每个班级做一些事情,但不确定如何让它发挥作用。 need to make it a click event and keep track of each image part需要使它成为一个点击事件并跟踪每个图像部分

$(".face").each(function (i) {
        if (i < 9) {
            $(".face").parent().animate({
                left: "-=367px"
            }, 500);
            i++;
        } else {
            $(".face").parent().animate({
                left: "0px"
            }, 500);
            i = 0;
        }
    });

HTML: HTML:

<div id="pic_box">
                <div id="head" class="face"><img src="images/headsstrip.jpg"></div>
                <div id="eyes" class="face"><img src="images/eyesstrip.jpg"></div>
                <div id="nose" class="face"><img src="images/nosesstrip.jpg"></div>
                <div id="mouth" class="face"><img src="images/mouthsstrip.jpg"></div>
  </div>

image in this link will give you an idea of the functionality此链接中的图像将使您了解功能

Thank you.谢谢你。

You can create a face object that holds the click counts of each face part and also a function to handle the click event (named clickHandler below).您可以创建一个人脸对象来保存每个人脸部分的点击次数,以及一个处理点击事件的函数(下面命名为 clickHandler)。 The clickHandler takes in an id and calls the appropriate animate function on the element that has that id . clickHandler 接受一个id并在具有该id的元素上调用适当的 animate 函数。

Check below:检查以下:

 var face = { "headClicks" : 0, "eyesClicks" : 0, "noseClicks" : 0, "mouthClicks" : 0, "clickHandler" : function(id) { if(this[id+"Clicks"] < 9) { animateLeft367(id); this[id+"Clicks"]++; } else { animateLeft0(id); this[id+"Clicks"] = 0; } } } function animateLeft367(id) { $("#" + id).animate({left: "-=367px"}, 500); console.log('animated ' + id + ' 367'); } function animateLeft0(id) { $("#" + id).animate({left: "0px"}, 500); console.log('animated ' + id + ' 0'); } $(".face").click(function() { face.clickHandler(this.id); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="pic_box"> <div id="head" class="face"><img src="images/headsstrip.jpg"></div> <div id="eyes" class="face"><img src="images/eyesstrip.jpg"></div> <div id="nose" class="face"><img src="images/nosesstrip.jpg"></div> <div id="mouth" class="face"><img src="images/mouthsstrip.jpg"></div> </div>

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

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