javascript - 包含 lightbox.js 时灯箱无法工作

标签 javascript jquery html lightbox lightbox2

我正在尝试使用 Lightbox 实现一个图片库。使用该示例一切正常。由于我正在使用 Bootstrap 并且已经链接到 JQuery,因此我想使用 lightbox.js 而不是 lightroom-plus-jquery.js。然而,这会破坏功能,并且所有图像都会像常规链接一样全屏打开。

在官方网站上我找不到任何信息,也没有提供其他示例代码。所以我希望这里有人能帮我解决这个问题。

这是基于示例的最小 html:

<html>
<head>
<meta charset="utf-8">
<title>Lightbox Example</title>
<link rel="stylesheet" href="css/lightbox.css">
</head>
<body>
<section>
    <div>
    <a class="example-image-link" href="http://lokeshdhakar.com/projects/lightbox2/images/image-3.jpg" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="http://lokeshdhakar.com/projects/lightbox2/images/thumb-3.jpg" alt=""/></a>
    <a class="example-image-link" href="http://lokeshdhakar.com/projects/lightbox2/images/image-4.jpg" data-lightbox="example-set" data-title="Or press the right arrow on your keyboard."><img class="example-image" src="http://lokeshdhakar.com/projects/lightbox2/images/thumb-4.jpg" alt="" /></a>
    <a class="example-image-link" href="http://lokeshdhakar.com/projects/lightbox2/images/image-5.jpg" data-lightbox="example-set" data-title="The next image in the set is preloaded as you're viewing."><img class="example-image" src="http://lokeshdhakar.com/projects/lightbox2/images/thumb-5.jpg" alt="" /></a>
    <a class="example-image-link" href="http://lokeshdhakar.com/projects/lightbox2/images/image-6.jpg" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close."><img class="example-image" src="http://lokeshdhakar.com/projects/lightbox2/images/thumb-6.jpg" alt="" /></a>
    </div>
</section>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script src="js/lightbox.js"></script>
</body>
</html>

最佳答案

jquery-3.3.1.slim.min.js有问题,请使用jquery.min.js

示例在这里:https://codepen.io/mrdvpatel/pen/BgVpZQ

@import url('https://fonts.googleapis.com/css?family=Montserrat&display=swap');
*{
  font-family: 'Montserrat', sans-serif;
}
.example-image-link img {
    width: 24%;
    height: 200px;
    object-fit: cover;
}
<html>
<head>
<meta charset="utf-8">
<title>Lightbox Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.0/css/lightbox.css">
</head>
<body>
<section class="images">
	<a class="example-image-link" href="https://dummyimage.com/600x400/000/fff" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="https://dummyimage.com/600x400/000/fff" alt=""/></a>
	<a class="example-image-link" href="https://dummyimage.com/600x400/ff0000/fff" data-lightbox="example-set" data-title="Or press the right arrow on your keyboard."><img class="example-image" src="https://dummyimage.com/600x400/ff0000/fff" alt="" /></a>
	<a class="example-image-link" href="https://dummyimage.com/400x600/000/fff" data-lightbox="example-set" data-title="The next image in the set is preloaded as you're viewing."><img class="example-image" src="https://dummyimage.com/400x600/000/fff" alt="" /></a>
	<a class="example-image-link" href="https://dummyimage.com/400x600/ff0000/fff" data-lightbox="example-set" data-title="Click anywhere outside the image or the X to the right to close."><img class="example-image" src="https://dummyimage.com/400x600/ff0000/fff" alt="" /></a>
</section>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.0/js/lightbox.js"></script>
</body>
</html>

关于javascript - 包含 lightbox.js 时灯箱无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56870601/

相关文章:

javascript - .click()/.trigger ('click' ) 不适用于 Spotify 中的 Tampermonkey

javascript - 单击单元格获取列标题和行标题

javascript - LimeJS 自定义 Sprite 吞咽事件

javascript - jQuery Mobile 的后退按钮似乎在页面之间传递缓存数据

javascript - 使用 jQuery UI 拖放 : changing the dragged element on drop

javascript - jquery 与同一个类一一切换 div

javascript - 加载动态日历以在该月的正确日期开始

javascript - 如何在 Canvas 上绘制多个图像并将 Canvas 保存为png图像

javascript - 编写额外的 </div> 标记,Blogger 不会自动更正它

javascript - 绝对div不能在 Canvas 元素之上