JavaScript 库通过点击导航菜单触发

标签 javascript photoswipe

我在同一网页上将FixedNav(单页网页的固定导航菜单)与 PhotoSwipe(JavaScript 库)组合在一起。

完成此操作后,当单击导航菜单中的链接时,它会向下滚动到页面的相关部分。它确实会按应有的方式滚动,但由于某种原因它也会打开 PhotoSwipe 图库。

单击菜单链接应该只是滚动到相关区域,而不会触发 PhotoSwipe 图库。

我使用了 FixNav 和 PhotoSwipe 的所有默认设置,并将它们放在一个页面上来说明问题:

https://jsfiddle.net/5pw1rem9/

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Adtile Fixed Nav</title>
    <meta name="author" content="Adtile">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="css/styles.css">

    <title>CodePen - PhotoSwipe Demo</title>
    <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.0/photoswipe.css'>
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.0/default-skin/default-skin.css'>
    <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Bitter:400,700,400italic'>
    <link rel="stylesheet" href="./style.css">


    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
      <link rel="stylesheet" href="css/ie.css">
    <![endif]-->
    <script src="js/responsive-nav.js"></script>
  </head>
  <body>

    <header>
      <a href="#home" class="logo" data-scroll>Fixed Nav</a>
      <nav class="nav-collapse">
        <ul>
          <li class="menu-item active"><a href="#home" data-scroll>Home</a></li>
          <li class="menu-item"><a href="#about" data-scroll>About</a></li>
          <li class="menu-item"><a href="#projects" data-scroll>Projects</a></li>
          <li class="menu-item"><a href="#blog" data-scroll>Blog</a></li>
          <li class="menu-item"><a href="http://www.google.com" target="_blank">Google</a></li>
        </ul>
      </nav>
    </header>

    <section id="home">
      <h1>Fixed Nav</h1>

<!-- partial:index.partial.html -->
<div class="container">
  <div class="row">
    <div class="col-sm-8 col-sm-offset-2">

      <h1 class="text-center">
          <small><em>..and a PhotoSwipe Demo</em></small>
        </h1>

      <!-- Galley wrapper that contains all items -->
      <div id="gallery" class="gallery" itemscope itemtype="http://schema.org/ImageGallery">

        <!-- Use figure for a more semantic html -->
        <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
          <!-- Link to the big image, not mandatory, but usefull when there is no JS -->
          <a href="https://unsplash.it/1200/900/?image=702" data-caption="Sea side, south shore<br><em class='text-muted'>© Dominik Schröder</em>" data-width="1200" data-height="900" itemprop="contentUrl">
            <!-- Thumbnail -->
            <img src="https://unsplash.it/400/300/?image=702" itemprop="thumbnail" alt="Image description">
          </a>
        </figure>

最佳答案

在您的函数中,您有一个“a”的点击事件,其中 a 不仅是图库链接;单击功能也会影响您的菜单链接。这就是导致您出现问题的原因。

要解决此问题,您需要更具体地定义您希望哪些链接受到单击事件的影响。就您而言,您所要做的就是在 a 之前插入 div id“#gallery”,因为图库 div 中的所有链接都是图像,如下所示:

$('#gallery a').click(function(event) {  
   //etc..

参见(略有更新)fiddle

但是,如果图库 div 中还有其他链接(除了图像之外),则这不是理想的解决方案,因此您还应该考虑使用类来标识图库图像组,并改用该类。

希望这有帮助

关于JavaScript 库通过点击导航菜单触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59793970/

相关文章:

javascript - AngularJS 变量依赖于作用域中的另一个变量

javascript - 浏览器宽度负 25%

javascript - 移动设备上具有大型控件的响应式 PhotoSwipe

javascript - 是否可以在 RactiveJS 中使用计算函数?

javascript - 单击链接后将全屏 api 保持在全屏状态

javascript - 更改复选框选中和取消选中时的复选框背景颜色

当到达画廊结束/开始时,JavaScript 画廊关闭(而不是循环)

javascript - photoswipe 不会返回到原始页面

javascript - 如何允许用户在显示照片滑动库时单击链接?

javascript - 如何使图像拉伸(stretch)以填充 PhotoSwipe 中的可用区域