如何使用 esc 键关闭灯箱页面叠加

2024-02-21

我有关于灯箱的问题请参阅我的jsFiddle http://jsfiddle.net/sambodhiprem/R9RGJ/4/。 单击其中一张图像将打开该画作的更大版本作为页面叠加层。

How to use the ESC key to close this page overlay?

以及如何使用方向键移动到下一张图像?

我需要什么样的 jQuery 插件/javascript 才能实现这一点?

<ul class="lb-album">

                    <li>
                        <a href="#Fly-My-Pretties-Walled-Garden">
                            <img src="http://sandipa.com.au/images/works-for-sale/thumbs/Fly-my-Pretties-Walled-Garden-sm.jpg" alt="Fly My Pretties: Walled Garden">
                            <span>Fly My Pretties</span>                        </a>
          <div class="lb-overlay" id="Fly-My-Pretties-Walled-Garden">
                            <a href="#page" class="lb-close">x Close</a>
                            <img src="http://sandipa.com.au/images/2010-2011/1000px-wide/Fly-my-Pretties-Walled-Garden.jpg" alt="Fly My Pretties: Walled Garden">
                            <div>
                                <h3>Fly My Pretties: Walled Garden<span>mixed media on canvas</span></h3>
                                <p>72 x 137 cm</p>
                                <a href="#Light-that-Shapes-the-Shadows" class="lb-prev">Prev</a>
                                <a href="#Central-Highlands-Circle-of-Gold" class="lb-next">Next</a>                            
                            </div>
                        </div>
                    </li>

<li>
                        <a href="#Central-Highlands-Circle-of-Gold">
                            <img src="http://sandipa.com.au/images/works-for-sale/thumbs/Central-Highlands-Circle-of-Gold-sm.jpg" alt="Central Highlands: Circle of Gold">
                            <span>Circle of Gold</span>                     </a>
          <div class="lb-overlay" id="Central-Highlands-Circle-of-Gold">
                            <a href="#page" class="lb-close">x Close</a>
                            <img src="http://sandipa.com.au/images/works-for-sale/Central-Highlands-Circle-of-Gold.jpg" alt="Central Highlands: Circle of Gold">
                            <div>
                                <h3>Central Highlands: Circle of Gold<span>mixed media on canvas</span></h3>
                                <p>51 x 108 cm</p>
                                <a href="#Fly-My-Pretties-Walled-Garden" class="lb-prev">Prev</a>
                                <a href="#Guardian-of-the-Night" class="lb-next">Next</a>                           
                            </div>
                        </div>
                    </li>

</ul>

纯 Javascript Lightbox 或 Image Popup Modal 的完整实现可以在我的答案中找到https://stackoverflow.com/a/67169851/8210884 https://stackoverflow.com/a/67169851/8210884.

上面链接中提到的这个答案允许处理隐藏灯箱的问题ESC键以及使用 Lightbox 中的图像进行导航Left and Right箭头键。

以下是该答案中的代码片段,它将帮助我们解决这两个问题。

隐藏灯箱ESC key :

  if(event.keyCode==27){ // If ESC key is pressed
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){ // LIGHTBOX ON
      document.getElementById("lightbox-container").classList.remove("showcontainer");
    }
  }

在 Lightbox 中浏览网页上的所有图像左、右箭头键 :

else if(event.keyCode==37) { // Left arrow key
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){ // LIGHTBOX ON
      // first get the URL of image displayed in the LIGHT BOX
      var currimgsrc = document.getElementById("lightbox-cont-img").getAttribute("src");

      // now match the sequence number in the array 
      var serialofarray = 0;
      for(k=0;k<allimgurlarray.length;k++){
        if(currimgsrc == allimgurlarray[k][2]){
          serialofarray = allimgurlarray[k][0];
        }
      }

      // with LEFT arrow, we are supposed to reduce the sequence and then use its ATTR SRC to LIGHT BOX
      if(serialofarray<=0){
        serialofarray = allimgurlarray.length - 1;
      }
      else {
        serialofarray = serialofarray - 1;
      }
      console.log("Left Arrow : "+serialofarray);
      document.getElementById("lightbox-cont-img").setAttribute("src", allimgurlarray[serialofarray][2]);

    }
  }
  else if(event.keyCode==39) { // Right Arrow Key
    if(document.getElementById("lightbox-container").classList.contains("showcontainer")){
      // first get the URL of image displayed in the LIGHT BOX
      var currimgsrc = document.getElementById("lightbox-cont-img").getAttribute("src");

      // now match the sequence number in the array 
      var serialofarray = 0;
      for(l=0;l<allimgurlarray.length;l++){
        if(currimgsrc == allimgurlarray[l][2]){
          serialofarray = allimgurlarray[l][0];
        }
      }

      // with RIGHT arrow, we are supposed to increase the sequence and then use its ATTR SRC to LIGHT BOX
      if(serialofarray>=allimgurlarray.length-1){
        serialofarray = 0;
      }
      else {
        serialofarray = serialofarray + 1;
      }
      console.log("Right Arrow : "+serialofarray);
      document.getElementById("lightbox-cont-img").setAttribute("src", allimgurlarray[serialofarray][2]);
    }
  }

这些有条件的情况涉及按键事件被解决在document.onkeydown = function(event).


下面的这段代码对于禁用默认行为IMG 标签上的按键事件以及将网页上的所有图像堆叠起来在数组中,以允许使用向左和向右箭头键在 Lightbox 中导航。

// Select all A tags with IMG child nodes
var atagswithimgtag = document.querySelectorAll("a[href]");

// then prevent the default behaviour of A tags by preventing of opening new page by HREF
// as well as collect all the HREF of A tags with images to enable RIGHT and LEFT arrow key
var allimgurlarray = [];
for(i=0;i<atagswithimgtag.length;i++){
  var childAIMGtag = atagswithimgtag[i].childNodes;
  if (childAIMGtag[0].nodeType != Node.TEXT_NODE) // or if (el[i].nodeType != 3)
  {
    // this seems too be a A tag with IMG tag as Childnode

    // first we need to prevent the default behaviour of opening the IMG in New Tab
    atagswithimgtag[i].addEventListener("click", function(event){
      event.preventDefault();
    });

    // second is when we need to fill image URL aray with A HREF
    var listofnodes = atagswithimgtag[i];
    allimgurlarray[i] = [];
    allimgurlarray[i][0] = i;
    allimgurlarray[i][1] = " Image URL is ";//listofnodes.getAttributeNode("title").value;
    allimgurlarray[i][2] = listofnodes.getAttributeNode("href").value;
  }
  console.log(childAIMGtag[0].innerHTML);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 esc 键关闭灯箱页面叠加 的相关文章

  • 键盘向上和向下箭头

    我有一个自动完成搜索 通过输入几个字符 它将显示与输入的字符相匹配的所有名称 我使用 DIV 标记在 jsp 中填充这些数据 通过使用鼠标 我可以选择名称 但我想使用键盘上下箭头选择要选择的 DIV 标签中的名称 任何人都可以帮我解决这个问
  • lightbox 2:如何通过 javascript 添加动态图像

    我在尝试时遇到困难动态添加图像 since 灯箱2 由 lokesh dakar 在文档加载后由现有的 html 编码图像进行初始化 并且在第一次之后不再解析文档 如果我尝试使用 javascript 添加新图像 例如将它们附加到正文中 它
  • 使用标签或 的灯箱

    是否有任何灯箱实现允许使用 a href a fancybox net 只需很少的工作即可实现这一目标 a href data image each function this fancybox content img attr src t
  • PyQt 无法识别箭头键

    我正在尝试编写一个 目前非常 简单的 PyQt 应用程序 并希望允许用户使用箭头键进行导航 而不是单击按钮 我已经实现了基础知识 并且在我的主要内容中QWidget 我覆盖keyPressEvent 现在 我所要求的就是它发出警报 QMes
  • 如何加快左右方向键编辑文本的速度? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 是只有我一个人觉得这样 还是其他人发现 MacOS 上的左右箭头键编辑文本的速度异常缓慢 与我的 Windows 机器相比 在 Mac 上使用左 右箭
  • 如何使用箭头键禁用 FF 中的页面滚动

    我正在构建一个包含主题和项目的菜单 每个主题都可以通过单击展开和折叠 我的任务是使用向上和向下箭头键在菜单主题和项目之间移动成为可能 我已经这样做了 但问题是 当页面大于窗口时 按箭头键时页面会滚动 我尝试过使用 document body
  • Java - 具有显示箭头的按钮

    我想要一个 Java 中的按钮 它显示箭头 就像键盘上一样 到目前为止我有这个 JButton arrowUp new JButton JButton arrowDown new JButton v JButton arrowLeft ne
  • Angular lightbox:无法绑定到“data-lightbox”,因为它不是“a”的已知属性

    我读过 角 它说如果你想绑定一个属性 它应该用 包裹 所以我做了什么 但我收到了错误 知道我做错了什么吗 我的要求是 data lightbox 的值是我从另一个组件 父组件 设置 发送的值 图像显示 component ts Compon
  • 如何使 jquery lightbox 从一个链接打开多个图像?

    使用像这样的灯箱ColorBox http colorpowered com colorbox or jQuery 灯箱插件 http leandrovieira com projects jquery lightbox 我怎样才能创建一个
  • PrettyPhoto Jquery Lightbox 链接问题

    我正在使用非常棒的 PrettyPhoto Lightbox 它是一个 Jquery 灯箱克隆 要使用 iframe 启动灯箱 这是代码 a href http www google com iframe true width 100 he
  • 灯箱图片下载

    我在多个图片库中使用 Lightbox 2 我想为我的用户提供一种在灯箱中查看图像时下载图库中的图像的方法 如果有一个 下载 按钮就完美了 我把它想象在标题附近 问题是我对Javascript很烂 这个项目是一个不经常使用的内部工具 我自愿
  • Scriptaculous / Prototype 模态窗口

    我想使用原型和 scriptaculous 制作一个模式窗口 我想知道是否有任何网站有教程 或者是否有人可以给我一些想法从哪里开始 我不需要预建的 除了 Control Window 之外 我还发现了其他一些 http okonet ru
  • 如何在Python中使用paramiko库发送箭头键?

    我正在使用 python 2 7 和代码 ssh 客户端paramiko图书馆 我用myhost channel send chr keycode 将每个键码发送到服务器 但它仅适用于 1 字节键码 我想发送其他多字节键码 例如箭头键 我怎
  • VIM 自定义箭头键映射不适用于窗口切换?

    我一直在尝试创建一个在 vim 中打开的窗口拆分之间切换的快捷方式 而不是必须使用 ctrl w arrowkey 我更愿意只能够使用 ctrl arrow key 这是我当前的 vimrc 中的内容 map
  • jQuery 下一个有特殊类型子元素的兄弟

    我的 HTML 有一个包含许多同级元素的容器元素div元素 每个元素包含一个contenteditable p 这些兄弟姐妹div然而 被其他人 打断 div其中不包含可编辑元素 目前对我来说挑战的是如何 跳过 这些干扰div使用左右箭头键
  • 是否可以在 OCaml 解释器中使用箭头键?

    每次我在解释器中使用这些键时 我都会不断出现如下符号 D C 我在 ZSH 中使用 Linux Mint 12 但是在 Ubuntu 中使用 bash 得到了相同的结果 另外 ssh 中也是同样的情况 库存 OCaml 顶层没有内置行编辑功
  • lightbox-0.5 jquery 兼容性问题

    我正在尝试从这里集成 jquery lightbox 插件 http leandrovieira com projects jquery lightbox http leandrovieira com projects jquery lig
  • 如何在 Javascript 函数中处理箭头键和 <(大于)?哪个事件和哪个代码(charCode 与 keyCode)?

    我该如何处理箭头键和
  • 具有适当后退按钮支持的 jQuery Lightbox

    在进行了一些可用性测试后 我发现参与者打开了jQuery 灯箱 http leandrovieira com projects jquery lightbox 查看更大的图像 然后 他们只需点击浏览器后退按钮 而不是单击 关闭 按钮 这会将
  • 箭头键变成 Telnet 中的控制字符

    我在 Ubuntu 终端中 并通过 telnet 连接到服务器 现在每当我输入 up 时 它就会变成 A 其他箭头键也变成控制字符 有没有办法可以运行 telnet 以便它理解我的箭头键 这将是一个巨大的帮助 因为我想在我的命令历史记录中向

随机推荐