HTML5 CSS3 modal window, close when click outside

好久不见. 提交于 2019-12-12 02:52:40

问题


I use the modal window as described by Keenan Payne, made with HTML5 & CSS3. I would like it to close when the user clicks on the darkened background, outside the modal window. Does anybody knows how to do this?

Modal window by Keenan Payne: http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/


回答1:


You have to change the target on something different than #openModal. You can import jQuery, put this in <head> section:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

and add this code right before </body> tag:

<script type="text/javascript">
    $("#openModal").click( function() {
        if (!$("#openModal>div").is(":hover")) document.location.href='#';
    });
</script>

So when user clicks on the overlay the target will change on '#' and the modal will be closed.



来源:https://stackoverflow.com/questions/19684599/html5-css3-modal-window-close-when-click-outside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!