简体   繁体   English

隐藏模态窗口而不使用关闭按钮

[英]Hiding a modal window without using close button

I have an image gallery. 我有一个图库。 clicking on any image opens a modal window with the clicked in image. 单击任何图像将打开一个带有单击图像的模态窗口。 I want to hide the modal window. 我想隐藏模态窗口。 i know how to do it by putting a close button. 我知道如何通过关闭按钮来做到这一点。 But i don't want to add any close button. 但我不想添加任何关闭按钮。 what i am trying to do is whenever a user click anywhere other then following div gallery-image gallery-control-previous gallery-control-next the modal window should get hidden. 我想要做的是每当用户点击其他任何地方然后跟随div gallery-image gallery-control-previous gallery-control-next ,模态窗口应该被隐藏。

can anyone suggest me how to achieve this. 任何人都可以建议我如何实现这一目标。

Here is my jsFiddle 这是我的jsFiddle

Demo 演示

Hide on clicking other than navigation keys navigation keys以外的其他位置隐藏

$('.gallery-overlay').click(function(event){
    if($(event.target).hasClass('gallery-control') ==false){
        $(this).hide();
    }
});

Hide on clicking other than image and navigation keys imagenavigation keys以外的其他位置隐藏

$('.gallery-overlay').click(function(event){
    if($(event.target).hasClass('gallery-control') ==false && $(event.target).hasClass('gallery-image') == false){
        $(this).hide();
    }
});

you can use fadeout('slow') instead of hide for giving good effects 你可以使用fadeout('slow')代替hide来获得好的效果

Just add the following to your code: 只需在代码中添加以下内容:

$('.gallery-overlay').click(function() {
    $(this).fadeOut('slow');
});

$('.gallery-image').click(function() {
    return false;
});

You will also need to add return false at the end of your click handlers for .gallery-control-previous , .gallery-control-next and .gallery-image so that the event doesn't propagate to .gallery-overlay . 您还需要在click处理程序的末尾为.gallery-control-previous.gallery-control-next.gallery-image添加return false ,以便事件不会传播到.gallery-overlay

Updated fiddle . 更新了小提琴

Here is the 这里是

jsFiddle .. jsFiddle ..

add this to end of your jquery code. 将其添加到jquery代码的末尾。

$(".gallery-overlay").click(function(e) {
    if($(e.target).hasClass("gallery-image") || $(e.target).hasClass("gallery-control-next") || $(e.target) .hasClass("gallery-control-previous")){/**/}
    else {$(".gallery-overlay").fadeOut("2000");
    }
});

e.target gives the actual clicked area, within .gallery-overlay . e.target在.gallery-overlay给出实际点击的区域。 $(this) doesn't work. $(this)不起作用。 You can slow down the rate of fading of modal window by increasing time. 您可以通过增加时间来减慢模态窗口的衰落速度。 Here it is 2000, ie2 seconds. 这是2000,即2秒。

Simply add a click event to fadeOut() the .gallery-overlay onclick. 只需将点击事件添加到fadeOut() . .gallery-overlay onclick即可。

View the JSFiddle for an example 查看JSFiddle以获取示例

Then for your .gallery-captionbox and .gallery-image , add a click event and return false on them. 然后,对于.gallery-captionbox.gallery-image ,添加一个click事件并在其上return false

// Hide the gallery on click
$('.gallery-overlay').click(function(){
    $(this).fadeOut();
});

// Return false on click of the caption box or the image itself
$('.gallery-captionbox, .gallery-image').click(function(){
    return false;
});

// All your other code below

View the JSFiddle 查看JSFiddle

Try, 尝试,

$(document).click(function(e) {

       if( e.target.id != 'yourDivId') {
           $("#yourDivId").hide();
       }
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM