简体   繁体   English

第一次我的函数运行正常,第二次它的错误jQuery

[英]first time my function runs fine, second time its wrong jquery

I'm trying to make a lightbox. 我正在尝试制作一个灯箱。 But when i open the lightbox for the second time. 但是当我第二次打开灯箱时。 It goes trough my code twice. 它通过我的代码两次。 When i open my lightbox the third time, it goes trough my code three times. 当我第三次打开灯箱时,它遍历了我的代码三遍。 Don't get it at all. 一点都不明白。

$(document).ready(function(){
$('.bg-overlay, .overlay-content, .overlay-content img').hide();

$('.thump-bnr > li').click(function(){

    // show the overlay and bg-overlay
    $('.bg-overlay, .overlay-content').fadeIn(500);

    // gets the index of the thunp thats been clicked in the banner
    var x = $(this).index();
    // console.log(x);

    $('.overlay-content > img').eq(x).fadeIn(500);

    // thumpPop checks if there aren't to mutch list items
    var thumpPop = $('.overlay-content .thump-pop li').length;
    // console.log(thumpPop);

    // appends all li for the thump navigation in the overlay
    if (thumpPop < 1) {
        $('.overlay-content').append('<ul class="thump-pop"></ul>');
        for (var i = 0; i < 4; i++) {
            $('.thump-pop').append('<li></li>');
        }
    }

    // sets all thump li to the border white
    $('.thump-pop > li').css("border-color", "#fff");

    // sets the active thump li to a dark border
    $('.thump-pop > li').eq(x).css("border-color", "#e2e2e2");

    // console.log(x);


    // calls the thumpNav function for the thump navigation     
    thumpNav();

    // calles the arrowNav function for the arrow navigation beside the big images
    arrowNav();

});

In this function i have managed to execute the function only once by using an if statement. 在此函数中,我仅使用if语句设法仅执行一次函数。

// this is the function for the thump navigation
function thumpNav() {

$('.thump-pop > li').click(function(){

    // get the index number of the thump li 
    var y = $(this).index();
    // console.log(y);

    // checks if the big image thats equal to the clicked thump is hidden
    if($('.overlay-content > img').eq(y).is(':hidden')) {

        // fadeIn and fadeOut the big images
        $('.overlay-content img').fadeOut();
        $('.overlay-content > img').eq(y).fadeIn();

        // this wil change the border color of the active li
        $('.thump-pop > li').css("border-color", "#fff");
        $(this).css("border-color", "#e2e2e2");
    }           
});
}

I think i have made a mistake in the function arrowNav(), because he executes this twice when i open my lightbox for the second time. 我认为我在arrowNav()函数中犯了一个错误,因为当我第二次打开灯箱时,他执行了两次。

function arrowNav() {
$('.arrow-nav-left').click(function(){

    // this wil get the index number of the visible image in the overlay. This number can be used to display the number -1 our +1
    var x = $('.overlay-content').find('img:visible').index();
    // console.log(x); 

    var x = x - 2;
    console.log(x);

        $('.overlay-content > img').hide();
        $('.overlay-content > img').eq(x).show();

});
}



// hides the pop-up
$('.bg-overlay').click(function(){
    $('.bg-overlay, .overlay-content, .overlay-content img').fadeOut(500);
}); 
});

Please help me, and some feedback on the code is alway helpfull. 请帮助我,对代码的一些反馈总是有帮助的。 Thanks 谢谢

The problem is here: 问题在这里:

function thumpNav() {

    $('.thump-pop > li').click(function(){

You're attaching a new click handler everytime you call thumpNav , and they will all execute and do the same thing everytime you click. 每次调用thumpNav ,您都将附加一个新的单击处理程序,并且每次单击时它们都将执行并执行相同的操作。

Replace with: 用。。。来代替:

function thumpNav() {
    $('.thump-pop > li').unbind("click").click(function(){

Just like you did with arrowNav() . 就像您使用arrowNav()

Note that your code is very unefficient and not structured quite right. 请注意,您的代码效率很低,并且结构不正确。 Even if this works it's not good when you're juggling click handlers like this. 即使这种方法行得通,当您像这样处理单击处理程序时也不是很好。 At least define the callback as a seperate function and pass that as an argument to click() . 至少将回调定义为单独的函数,并将其作为参数传递给click()

If you want to get help with improving your code, you can always post it on Codereview . 如果您想获得改进代码的帮助,可以随时将其发布在Codereview上

Every time you're calling: 每次致电时:

thumpNav();

you're attaching a new click handler. 您要附加新的点击处理程序。

same with arrowNav() arrowNav()相同

but atleast here you unbind first. 但至少在这里您先解除绑定。

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

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