单击div,淡入div,单击div或div外部淡出div(Click div, fade in div, click div OR outside div to fade out div)

编程入门 行业动态 更新时间:2024-10-06 04:10:54
单击div,淡入div,单击div或div外部淡出div(Click div, fade in div, click div OR outside div to fade out div)

所以我有一些代码,我知道有效,这使得当你按下div时它会打开第二个div(设置为display:none in css)。 然后单击您单击的相同div打开第二个div以关闭它(淡出)。

但是,我希望能够这样做,这样你就可以点击OUTSIDE然后它会淡出。 我已经尝试了很多代码,但我无法让它工作。 请帮忙!

$(document).ready(function() { $("#dashbox").click(function() { $("#dashboardbox").fadeToggle(); }); });

So I have a little code, that I know works, that makes it so when you press on a div it opens a second div (which is set to display: none in css). You then click on the same div that you click to open the second div to close it (fade out).

However, I want to be able to make it so you can click OUTSIDE and it would fade out. I've tried numerous of codes, yet I cannot get it working. Please help!

$(document).ready(function() { $("#dashbox").click(function() { $("#dashboardbox").fadeToggle(); }); });

最满意答案

向文档添加一个单击事件,如果该框不可见,则通过单击#dashboardbox或#dashbox(将触发切换两次)或其任何子项来检查它是否未触发。 如果一切顺利,它将淡出盒子。

$(document).ready(function() { $(document).click(function(e) { // Skip if already hidden if(!$('#dashboardbox').is(":visible")) return; // Skip if clicked dashbox or its children if ($(event.target).closest('#dashbox').length) return; // Skip if clicked dashboardbox or its children if ($(event.target).closest('#dashboardbox').length) return; // Fade out $("#dashboardbox").fadeOut(); }); $("#dashbox").click(function(e) { $("#dashboardbox").fadeToggle(); }); });

在这里拨弄 。

Add a click event to the document that, if the box is not already invisible, checks that it did not get triggered by clicking #dashboardbox or #dashbox (would trigger toggle twice) or any of their children. If all is well it will then fade out the box.

$(document).ready(function() { $(document).click(function(e) { // Skip if already hidden if(!$('#dashboardbox').is(":visible")) return; // Skip if clicked dashbox or its children if ($(event.target).closest('#dashbox').length) return; // Skip if clicked dashboardbox or its children if ($(event.target).closest('#dashboardbox').length) return; // Fade out $("#dashboardbox").fadeOut(); }); $("#dashbox").click(function(e) { $("#dashboardbox").fadeToggle(); }); });

Fiddle here.

更多推荐

本文发布于:2023-08-07 11:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   div   Click   fade   click

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!