为什么点击我的叠加层时不退出?

因此,我今天在网站上进行了一些更新,并删除了我对重叠式广告的标题。我觉得我不需要。我暂时将上述代码注释掉,以测试我是否喜欢它。我确实喜欢没有文字的图像叠加层,但是现在我无法通过再次单击它来退出叠加层。为什么是这样?这是我的repo代码源。下面也是我的js代码。

/*************************************************************
            Gallery Lightbox On Stories Page
**************************************************************/

//Problem: User when clicking on image goes to a dead end
//Solution: Create an overlay with the large image - Lightbox

var $overlay = $('<div id="overlay"></div>');
var $image = $("<img>");
// var $caption = $("<p></p>");

// An image to overlay
$overlay.append($image);

// A caption to overlay
// $overlay.append($caption);

// Add overlay
$("body").append($overlay);

// Capture the click event on a link to an image
$(".gallery a").click(function(event){
  event.preventDefault();
  var imageLocation = $(this).attr("href");
  // Update overlay with the image linked in the link
  $image.attr("src",imageLocation);

  // Show the overlay.
  $overlay.show();


  // Get child's alt attribute and set caption
//  var captionText = $(this).children("img").attr("alt");
//  $caption.text(captionText);
// }); 

// When overlay is clicked
$overlay.click(function(){
  // Hide the overlay
  $overlay.hide();
});

/*************************************************************
           Hide Email Using Javascript To Avoid Spam 
**************************************************************/
//var parts = ["lpstories","yahoo","com","&#46;","&#64;"];
//var email = parts[0] + parts[4] + parts[1] + parts[3] + parts[2];
//document.getElementById("email").innerHTML=email;
yl835389522laoye 回答:为什么点击我的叠加层时不退出?

  1. 您在 标签中将图像路径用作 href 。请改用数据属性。
  2. $ overlay.show() 之后
  3. 缺少})

使用此代码。

    /*************************************************************
            Gallery Lightbox On Stories Page
**************************************************************/

//Problem: User when clicking on image goes to a dead end
//Solution: Create an overlay with the large image - Lightbox

var $overlay = $('<div id="overlay"></div>');
var $image = $("<img>");
// var $caption = $("<p></p>");

// An image to overlay
$overlay.append($image);

// A caption to overlay
// $overlay.append($caption);

// Add overlay
$("body").append($overlay);

// Capture the click event on a link to an image
$(".gallery a").click(function(event){
event.preventDefault();
var imageLocation = $(this).attr("data-href");
// Update overlay with the image linked in the link
$image.attr("src",imageLocation);

// Show the overlay.
$overlay.css("display","flex");

});
// Get child's alt attribute and set caption
//  var captionText = $(this).children("img").attr("alt");
//  $caption.text(captionText);
// }); 

// When overlay is clicked
$overlay.click(function(){
// Hide the overlay
$overlay.hide();
});

/*************************************************************
        Hide Email Using Javascript To Avoid Spam 
**************************************************************/
//var parts = ["lpstories","yahoo","com","&#46;","&#64;"];
//var email = parts[0] + parts[4] + parts[1] + parts[3] + parts[2];
//document.getElementById("email").innerHTML=email;

它还在弹出窗口中居中显示图像。

这是相同的工作fiddle

如果您不想关闭图像点击时的弹出窗口,请添加此项。

$overlay.find("img").click(function(e){
 e.stopImmediatePropagation();
});
本文链接:https://www.f2er.com/3031839.html

大家都在问