javascript - HTML 中的灯箱效果
问题描述
我正在尝试编辑我的 javascript 代码以在单击图像时启用灯箱。目前,它在另一个选项卡中打开图像。我在网上搜索过,但似乎找不到答案..
<script language="JavaScript">
$(function() {
/* Lets the user click on the images to view them in full resolution. */
$("img").wrap(function() {
var link = $('<a/>');
link.attr('href', $(this).attr('src'));
link.attr('title', $(this).attr('alt'));
link.attr('target', '_blank');
return link;
});
});
</script>
<img src="img/4_email_a.png" style="border: #A9A9A9 1px solid; width:70%">
有人能指出我正确的方向吗?谢谢!
解决方案
您必须使用像 fancybox ( https://fancyapps.com/fancybox/3/ ) 这样的灯箱脚本,您只需要data-fancybox
为您的链接添加属性,例如:
<a data-fancybox="gallery" href="big_1.jpg"><img src="small_1.jpg"></a>
也可以在没有链接的情况下使其工作 - https://codepen.io/anon/pen/rRVeJW
推荐阅读
- java - forEach 方法接受一个返回值的 lambda 表达式。为什么我没有收到以下代码的编译问题?
- javascript - 每次保存后如何自动运行 Webpack-dev-server?
- php - 用php替换前4个字符
- node.js - 如何使用 req.params.ids(多个 id)删除多个数据
- c++ - type_info 引用已删除的函数
- windows - 7-zip 安装和卸载后的 zip 错误
- python-3.x - 定义多个 Python 脚本中使用的常量
- python - Pandas/matplotlib 线图不显示 x 轴文本标签
- ruby-on-rails - CSS类选择器不适用于form.collection_select Rails 5
- java - 如何根据语言选择Url?