如何在单击文档主体和指定元素时淡入和淡出任何元素?
大家好!
我一直在尝试创建淡入和淡出菜单,但它似乎根本不起作用!看到问题是当我单击属于 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();
}
});
});
谁能帮我解决这个问题。
提前致谢!
正如您在这个演示中看到的,我的上述问题得到了解决,但又出现了一个新问题!现在我希望点击下拉菜单时出现。我的意思是,如果用户单击下拉菜单,它不会像现在一样淡出。请看下面的演示来看看这个新问题~~~~~~~~~~~~
希望你们也能帮我解决这个问题。
提前致谢!
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.
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~~~~~~~~~~~
Hope you guys can help me out with this one too.
Thanks in Advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 div#example 上的点击事件向上冒泡到 body 元素并且
因此目标元素通过 div#example 淡入,随后主体点击处理程序也被执行。
您需要停止 div#example 的事件传播以保留 fadeIn
将 div#example 单击处理程序更改为如下:
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:
这可能有效。真丑啊...
This might work. It's ugly...
您的事件正在通过两个事件侦听器冒泡。当您点击
$('div#example')
时,您同时也在点击document
,因此两者都会触发。您需要防止事件传播。将第一个事件处理程序更改为:Your event is bubbling up through both event listeners. When you click on
$('div#example')
, you are also clicking on thedocument
, so both are firing. You need to prevent the event propagation. Change the first event handler to: