如何在单击文档主体和指定元素时淡入和淡出任何元素?

发布于 2024-11-01 13:42:23 字数 1207 浏览 1 评论 0原文

大家好!

我一直在尝试创建淡入和淡出菜单,但它似乎根本不起作用!看到问题是当我单击属于 example 的触发元素 (div) 时 它应该在类 element_obj 的元素 (div) 中淡入淡出,但是如果用户在 element_obj (div)淡入。但是,当我单击实例的触发元素时,我的 element_obj (div) 淡入,并在另一时刻淡出!我知道这是问题所在,因为我也将我的身体元素作为淡出触发器!但如果我不这样做,我的整个效果就会死亡!这是我创建的 jQuery 代码~~~~~~~~~

$(document).ready(function () {
  $('div#example').click(function () {

      if($('div#example_obj').is(':visible')){
            $('div#example_obj').fadeOut();
      }else{
            $('div#example_obj').fadeIn();
      }

  });
    $(document).click(function () {

      if($('div#example_obj').is(':visible')){
            $('div#example_obj').fadeOut();
      }

    });
});

谁能帮我解决这个问题。

问题演示

提前致谢!


正如您在这个演示中看到的,我的上述问题得到了解决,但又出现了一个新问题!现在我希望点击下拉菜单时出现。我的意思是,如果用户单击下拉菜单,它不会像现在一样淡出。请看下面的演示来看看这个新问题~~~~~~~~~~~~

问题 2 DEMO

希望你们也能帮我解决这个问题。

提前致谢!


Hello Guys!

I have been trying to create a fadeIn and fadeOut menu, but it does not seem to be working at all! See the problem is when I click on the my trigger element (div) which classed example
it should be fading in the element (div) classed element_obj but if the user's click on the body element when the element_obj (div) is faded in. But when I click on my trigger element for an instance my element_obj (div) fades in and in another moment it is fade out! I know that this is the problem because I have put my body element also as a fading out trigger! But if I don't do that my whole effect is dead! Here is the jQuery code that I created ~~~~~~~~~

$(document).ready(function () {
  $('div#example').click(function () {

      if($('div#example_obj').is(':visible')){
            $('div#example_obj').fadeOut();
      }else{
            $('div#example_obj').fadeIn();
      }

  });
    $(document).click(function () {

      if($('div#example_obj').is(':visible')){
            $('div#example_obj').fadeOut();
      }

    });
});

Can anyone help me out with this.

PROBLEM DEMO

Thanks in Advance!


As you can see in this demo my above problem is solved but a new problem have arise! Now I want the drop-down menu to be appear on click on it. I mean if the user clicks on the drop-down menu it does not fades out as it is now. Please see the below demo to see this new problem~~~~~~~~~~~

PROBLEM 2 DEMO

Hope you guys can help me out with this one too.

Thanks in Advance!


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

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

发布评论

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

评论(3

反差帅 2024-11-08 13:42:23

这是因为 div#example 上的点击事件向上冒泡到 body 元素并且
因此目标元素通过 div#example 淡入,随后主体点击处理程序也被执行。
您需要停止 div#example 的事件传播以保留 fadeIn

将 div#example 单击处理程序更改为如下:

$('div#example').click(function (e) {        
    if($('div#example_obj').is(':visible')){             
        $('div#example_obj').fadeOut();       
    }else{             
        $('div#example_obj').fadeIn();       
    }    
  e.stopPropagation();
}); 

It is because the click event on div#example is bubbled up to the body element and
hence the target element fades in by the div#example and subsequently the body click handler is also executed.
You need to stop the event propagation of the div#example to retain the fadeIn

Change the div#example click handler to as below:

$('div#example').click(function (e) {        
    if($('div#example_obj').is(':visible')){             
        $('div#example_obj').fadeOut();       
    }else{             
        $('div#example_obj').fadeIn();       
    }    
  e.stopPropagation();
}); 
拥醉 2024-11-08 13:42:23

这可能有效。真丑啊...

$(document).ready(function () {
    $(document).click(function(e) {
      if (e.targetElement.id == 'example')
      {
        if($('div#example_obj').is(':visible')){
            $('div#example_obj').stop().fadeOut();
        }else{
            $('div#example_obj').stop().fadeIn();
        }
      } else {
        if($('div#example_obj').is(':visible')){
            $('div#example_obj').stop().fadeOut();
        }
      }
    });
});

This might work. It's ugly...

$(document).ready(function () {
    $(document).click(function(e) {
      if (e.targetElement.id == 'example')
      {
        if($('div#example_obj').is(':visible')){
            $('div#example_obj').stop().fadeOut();
        }else{
            $('div#example_obj').stop().fadeIn();
        }
      } else {
        if($('div#example_obj').is(':visible')){
            $('div#example_obj').stop().fadeOut();
        }
      }
    });
});
薯片软お妹 2024-11-08 13:42:23

您的事件正在通过两个事件侦听器冒泡。当您点击 $('div#example') 时,您同时也在点击 document,因此两者都会触发。您需要防止事件传播。将第一个事件处理程序更改为:

$('div#example').click(function (e) {
  e.stopPropagation();

  if($('div#example_obj').is(':visible')){             
    $('div#example_obj').fadeOut();       
  }else{             
    $('div#example_obj').fadeIn();       
  }    

});

Your event is bubbling up through both event listeners. When you click on $('div#example'), you are also clicking on the document, so both are firing. You need to prevent the event propagation. Change the first event handler to:

$('div#example').click(function (e) {
  e.stopPropagation();

  if($('div#example_obj').is(':visible')){             
    $('div#example_obj').fadeOut();       
  }else{             
    $('div#example_obj').fadeIn();       
  }    

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文