首页 > 解决方案 > Simplelightbox 不适用于 jQuery 3.4.1

问题描述

我在让 Simplelightbox 工作时遇到问题。我遵循了 Traversy 的教程,但似乎无法弄清楚我做错了什么。

在我的 index.html 里面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/simple-lightbox.min.css">
    <title>Simplelightbox Test</title>
</head>

<body>
    <div class="gallery">
        <a href="https://i7.pngguru.com/preview/523/198/482/google-logo-google-search-google-play-google.jpg"><img src="https://i7.pngguru.com/preview/523/198/482/google-logo-google-search-google-play-google.jpg" alt="photo"></a>
        <a href="https://chromeunboxed.com/wp-content/uploads/2016/10/GoogleAssistant-1200x550.png"><img src="https://chromeunboxed.com/wp-content/uploads/2016/10/GoogleAssistant-1200x550.png" alt="two"></a>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="simple-lightbox.min.js"></script>
    <script>
    $(function() {
        const $gallery = $('.gallery a').simpleLightbox();
      });
    </script>

</body>
</html>

截屏

标签: jquery

解决方案


在 HTML 文件中包含脚本

<script src="https://cdnjs.cloudflare.com/ajax/libs/simplelightbox/2.1.3/simple-lightbox.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/simplelightbox/2.1.3/simple-lightbox.jquery.min.js"></script>

<div class="gallery">
  <a href="https://images.pexels.com/photos/850602/pexels-photo-850602.jpeg?cs=srgb&dl=photography-of-three-dogs-looking-up-850602.jpg&fm=jpg">
    <img src="https://images.pexels.com/photos/850602/pexels-photo-850602.jpeg?cs=srgb&dl=photography-of-three-dogs-looking-up-850602.jpg&fm=jpg" width="250" />
  </a>
  <a href="https://images.pexels.com/photos/319975/pexels-photo-319975.jpeg?cs=srgb&dl=adorable-animal-blur-breed-319975.jpg&fm=jpg">
    <img src="https://images.pexels.com/photos/319975/pexels-photo-319975.jpeg?cs=srgb&dl=adorable-animal-blur-breed-319975.jpg&fm=jpg" width="250" />
  </a>
</div>

然后只需在您的 JS 文件中调用该函数

$('.gallery a').simpleLightbox();

推荐阅读