简体   繁体   English

单击后打开和关闭jQuery功能

[英]jquery on and off function after click

I'm using 2 jquery codes on my website. 我在我的网站上使用2个jquery代码。

first code is : 第一个代码是:

    function  bottom_open() {


        $('.cartouche_bottom').css('position','absolute').animate({
        top :$(".top_table").height() - 1,
        });

        $('.bottom_close').show();
        //$('.layout_bottom').stop().animate({height :0});

    }

and the second one is : 第二个是:

    function  bottom_close() {



        $('.cartouche_bottom').css('position','fixed').animate({
        top : cartouche_bottom_position
    });
        $('.bottom_close').hide();
}

} }

what I'm trying to do is when first function is executed (by a click function), unbind the function, and when the second function is then executed, rebind the 1st function. 我想做的是在执行第一个功能时(通过单击功能),取消绑定该功能,然后在执行第二个功能时,重新绑定第一个功能。 I can't find a way of doin it. 我找不到解决办法。

here is what I've tried : 这是我尝试过的:

function bottom_cartouche(){

    var cartouche_bottom_position = $(window).height() - $('.top_table').height() - $('.layout_bottom').height();

/ FIRST ACTION, when ".cartouche_bottom_inside" is clicked, execute function bottom_open() and then unbind bottom_open(). / 第一步,单击“ .cartouche_bottom_inside”时,执行函数bottom_open(),然后解除绑定bottom_open()。 So if we click again on ".cartouche_bottom_inside" bottom_open() will not be executed. 因此,如果再次单击“ .cartouche_bottom_inside”,将不会执行bottom_open()。 / /

    $('.cartouche_bottom_inside').click(function(){
    bottom_open();
    $(this).off('click');
    });

/ then SECOND ACTION, when ".bottom_close" is clicked exectude the second function bottom_close() and rebind first function bottom_open(). / 然后进行第二操作,当单击“ .bottom_close”时,将显示第二个函数bottom_close()并重新绑定第一个函数bottom_open()。 So if we click again on ".cartouche_bottom_inside", bottom_open() will be executed / 因此,如果再次单击“ .cartouche_bottom_inside”,将执行bottom_open() /

    $('.bottom_close').click(function(){
    bottom_close();
    $('cartouche_bottom_inside').on('click');

    });

        function  bottom_open() {


            $('.cartouche_bottom').css('position','absolute').animate({
            top :$(".top_table").height() - 1,
            });

            $('.bottom_close').show();
            //$('.layout_bottom').stop().animate({height :0});

        }

        function  bottom_close() {



            $('.cartouche_bottom').css('position','fixed').animate({
            top : cartouche_bottom_position
        });
            $('.bottom_close').hide();
    }
}

Here is an explaination : 这是一个解释:

FIRST ACTION, when ".cartouche_bottom_inside" is clicked, execute function bottom_open() and then unbind bottom_open(). 首先,当单击“ .cartouche_bottom_inside”时,执行函数bottom_open(),然后解除绑定bottom_open()。 So if we click again on ".cartouche_bottom_inside" bottom_open() will not be executed. 因此,如果再次单击“ .cartouche_bottom_inside”,将不会执行bottom_open()。

then SECOND ACTION, when ".bottom_close" is clicked exectude the second function bottom_close() and rebind first function bottom_open() So if we click again on ".cartouche_bottom_inside", bottom_open() will be executed... and so on... 然后第二个动作,当单击“ .bottom_close”时,将执行第二个函数bottom_close()并重新绑定第一个函数bottom_open(),因此,如果再次单击“ .cartouche_bottom_inside”,将执行bottom_open()等等。 。

I can't find out what I'm doing wrong. 我找不到我在做什么错。

Can anybody help me with this ? 有人可以帮我吗?

thanks a lot for your time and for your help, 非常感谢您的时间和帮助,

You could probably bind the functions to certain classes, and then add the classes and remove them as needed to your elements in the appropriate places. 您可以将函数绑定到某些类,然后添加这些类并根据需要将它们删除到适当位置的元素中。

EDIT: Also, it looks like you have a typo here: 编辑:另外,看起来您在这里有错字:

$('.bottom_close').click(function(){
    bottom_close();
    $('cartouche_bottom_inside').on('click');
});

should be (notice the missing '.' on your selector): 应该是(请注意选择器上缺少的“。”):

$('.bottom_close').click(function(){
    bottom_close();
    $('.cartouche_bottom_inside').on('click');
});

I would just add and remove a class with something like: 我只想添加和删除类似的类:

    $('#buttonhere').click(function (e) {

    if ($(this).hasClass('firstPart')) {
        // Do one part.
        bottom_open();
        $(this).toggleClass('firstPart');
    }
    else {
        bottom_close();
        $(this).toggleClass('firstPart');
    }

})

try this 尝试这个

$('.bottom_close').click(function(){
  bottom_close();
  $('.cartouche_bottom_inside').on('click', function() {bottom_open() });
});

?

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

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