javascript - 添加关闭按钮灯箱

标签 javascript jquery css

一段时间以来,我一直在尝试向该灯箱添加关闭按钮(右上角),但没有成功。任何人都可以发送添加了关闭功能的工作 JSFiddle 或 Codepen 吗?

谢谢

http://codepen.io/stoypenny/pen/pJkcK

这是JS

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

$overlay.append($image);
$("#body").append($overlay);

$("#imageGallery  a").click(function(event){
  event.preventDefault();

  //Get image link
  var imageLocation = $(this).children().attr("src");
  //Add image source to the image
  $image.attr("src", imageLocation);
  //Set styles so that the image doesn't display at resolutions beyond the screen size
  $image.css({maxWidth: "70%", maxHeight: "70%", marginTop: "10%" });
  //Display the overlay
  $overlay.show();
});

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

最佳答案

这是您问题的快速解决方案,将其添加到您的 css 文件中:

#overlay:after {
  width: 40px;
  height: 40px;
  content: 'X';
  padding: 0;
  margin: 0;
  background: red;
  border-radius: 50%;
  color: #FFF;
  line-height: 40px;
  text-align: center;
  position: absolute;
  top: 0;
  right: 0;
  cursor: pointer;
}

我确信有更好的选择,但这至少应该让你现在继续前进,直到你找到更好的解决方案。

关于javascript - 添加关闭按钮灯箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44122313/

相关文章:

html - 位于表格外部的 CSS 单元格以及如何处理它们的宽度和边框

javascript - 如何允许德国用户在 React JavaScript 中输入德语格式的数字

javascript - 按第二个值对二维数组进行排序

javascript - 动态图层名称 - Leaflet 图层控制

jquery - 将 css 添加到动态添加的 Tablerow

javascript - 从使用 jQuery 的 php 网站提取数据。无相关来源信息

css - 将CSS文件放在Grails应用程序中的哪里?

javascript - 如何添加类到使用document.createElement创建的元素?

jquery - 我需要固定宽度容器中的图形和图像来跨越浏览器窗口的整个宽度

php - 将 css 与 php 链接起来