JQuery 1 单击触发 2 个条件(如果)

发布于 2024-10-05 06:09:16 字数 1486 浏览 5 评论 0原文

$('.anfahrt').click(function() {
    $button = $(this);
    if ( clickedc == 0){
        if( !$button.hasClass( 'disabled' ) ) {
            $button.addClass( 'disabled' );
            clickedc = 1
            $('.lupe').animate({opacity: '0'},750)
            $('.card > img').animate({height: 150, width: 193, opacity: '1', left: 0, top: 9},500)
            $('.contact-content2').animate({opacity:'1'},500).animate({opacity: '0'},500)
            $('.cardgreen > img').animate({height:150, width: 193, opacity: '0'},500).animate({opacity: '1', top: 9},500,
            function() { $button.removeClass('disabled') }
            );
        } 
    }
});

$(document).(click(function() {
    $button = $(this);
    if ( clickedc == 1){
        if( !$button.hasClass( 'disabled' ) ) {
            $button.addClass( 'disabled' );
            clickedc = 0
            $('.cardgreen > img').animate({opacity: '0'},300).animate({height:0,width:0});
            $('.contact-content2').show(0).animate({opacity: '1'},300)
            $('.clickding').animate({width: '0', height: '0'},0)
            $('.card > img').animate({opacity: '1'},300)
                .animate({opacity: '0', width: 0, height: 0, left:194, top:75},270,
            function() { $button.removeClass('disabled') }
            );
        } 
    }
}));

所以我点击 div.. 然后动画开始(淡入)。然后它应该停止...然后用户单击文档上的任何位置,第二个动画(淡出)应该开始。 - 但这不起作用..因为当我单击 Div 时,淡入动画开始,然后第二个动画立即开始。没有停止..请帮助我如何解决这个问题。

$('.anfahrt').click(function() {
    $button = $(this);
    if ( clickedc == 0){
        if( !$button.hasClass( 'disabled' ) ) {
            $button.addClass( 'disabled' );
            clickedc = 1
            $('.lupe').animate({opacity: '0'},750)
            $('.card > img').animate({height: 150, width: 193, opacity: '1', left: 0, top: 9},500)
            $('.contact-content2').animate({opacity:'1'},500).animate({opacity: '0'},500)
            $('.cardgreen > img').animate({height:150, width: 193, opacity: '0'},500).animate({opacity: '1', top: 9},500,
            function() { $button.removeClass('disabled') }
            );
        } 
    }
});

$(document).(click(function() {
    $button = $(this);
    if ( clickedc == 1){
        if( !$button.hasClass( 'disabled' ) ) {
            $button.addClass( 'disabled' );
            clickedc = 0
            $('.cardgreen > img').animate({opacity: '0'},300).animate({height:0,width:0});
            $('.contact-content2').show(0).animate({opacity: '1'},300)
            $('.clickding').animate({width: '0', height: '0'},0)
            $('.card > img').animate({opacity: '1'},300)
                .animate({opacity: '0', width: 0, height: 0, left:194, top:75},270,
            function() { $button.removeClass('disabled') }
            );
        } 
    }
}));

So I click on the div.. and the animation start (fadein). Then it should stop... Then user clicks everywhere on the document and the 2nd animation (fadeout) should start. - But that does not work .. cause when I click the Div the fadein animation starts after that the 2nd animation starts right away. Theres no stop.. pls help me how to fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅忆流年 2024-10-12 06:09:16

原因是当您单击 div 时,单击事件会冒泡到文档级别。

您要使用的是事件的 stopPropagation 方法:

$("#yourdiv").click(function(event){
   alert("Your div clicked"); 
   event.stopPropagation();
});

The reason for this is that the click event is bubbling up to the document level when you click the div.

What you want to use is the stopPropagation method on the event:

$("#yourdiv").click(function(event){
   alert("Your div clicked"); 
   event.stopPropagation();
});
離人涙 2024-10-12 06:09:16

For the click event on the div do: http://api.jquery.com/event.stopPropagation/. It will stop the event from bubbling up to the document level.

牵你的手,一向走下去 2024-10-12 06:09:16

我相信你的问题在于你正在约束你的事实
一次性点击事件的动画。因此,你点击div,它是
动画触发了,但同时你也触发了点击
$(document) 上的事件,这就是您注意到堆叠的原因
发生。

为了缓解这个问题,我将 .click() 事件的注册移至
$(document) 放入 div 的点击事件中,并删除了需要
一旦文档的点击事件被触发,就有 clickc 标志
它负责解除自身的绑定。

这种方法可能出现的一个问题是,如果用户
再次点击div,动画将再次发生。但我离开了
这对你来说是一个练习。

该解决方案未经测试。

function open_animation () {
    $('.lupe').animate({opacity: '0'},750)
    $('.card > img').animate({height: 150, width: 193, opacity: '1', left: 0, top: 9},500)
    $('.contact-content2').animate({opacity:'1'},500).animate({opacity: '0'},500)
    $('.cardgreen > img').animate({height:150, width: 193, opacity: '0'},500).animate({opacity: '1', top: 9},500, function() { $button.removeClass('disabled') });
}

function close_animation () {
    $('.cardgreen > img').animate({opacity: '0'},300).animate({height:0,width:0});
    $('.contact-content2').show(0).animate({opacity: '1'},300)
    $('.clickding').animate({width: '0', height: '0'},0)
    $('.card > img').animate({opacity: '1'},300).animate({opacity: '0', width: 0, height: 0, left:194, top:75},270, function() { $button.removeClass('disabled')});
}

$('.anfahrt').click(function() {
    $button = $(this);
    if( !$button.hasClass( 'disabled' ) ) {
        $button.addClass( 'disabled' );
        open_animation()
        $(document).click(function() {
            $button = $('.anfahrt');
            if( !$button.hasClass( 'disabled' ) ) {
                $button.addClass( 'disabled' );
                close_animation();
                $(document).unbind('click');
            }
        });
    }
});

I believe your problem lies in that fact that you're binding your
animations to click events all at once. Thus, you click the div, it's
animation triggers, but at the same time you also triggered the click
event on $(document) and so that's why you notice the stacking
occurring.

To alleviate this, I moved the registration of the .click() event on
$(document) into the click event of the div and removed the need to
have the clickc flag as once the document's click event is triggered
it takes care of unbinding itself.

One problem that might arise with this approach is that if the user
clicks again the div the animation will all happen again. But I leave
that to you as an exercise.

This solution is untested.

function open_animation () {
    $('.lupe').animate({opacity: '0'},750)
    $('.card > img').animate({height: 150, width: 193, opacity: '1', left: 0, top: 9},500)
    $('.contact-content2').animate({opacity:'1'},500).animate({opacity: '0'},500)
    $('.cardgreen > img').animate({height:150, width: 193, opacity: '0'},500).animate({opacity: '1', top: 9},500, function() { $button.removeClass('disabled') });
}

function close_animation () {
    $('.cardgreen > img').animate({opacity: '0'},300).animate({height:0,width:0});
    $('.contact-content2').show(0).animate({opacity: '1'},300)
    $('.clickding').animate({width: '0', height: '0'},0)
    $('.card > img').animate({opacity: '1'},300).animate({opacity: '0', width: 0, height: 0, left:194, top:75},270, function() { $button.removeClass('disabled')});
}

$('.anfahrt').click(function() {
    $button = $(this);
    if( !$button.hasClass( 'disabled' ) ) {
        $button.addClass( 'disabled' );
        open_animation()
        $(document).click(function() {
            $button = $('.anfahrt');
            if( !$button.hasClass( 'disabled' ) ) {
                $button.addClass( 'disabled' );
                close_animation();
                $(document).unbind('click');
            }
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文