Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将导航按钮(上一个和下一个)添加到模式_Javascript_Jquery_Html_Css_Twitter Bootstrap - Fatal编程技术网

Javascript 如何将导航按钮(上一个和下一个)添加到模式

Javascript 如何将导航按钮(上一个和下一个)添加到模式,javascript,jquery,html,css,twitter-bootstrap,Javascript,Jquery,Html,Css,Twitter Bootstrap,我已经创建了图像缩略图,并使用以下代码链接该缩略图 移民 x 《愤怒的葡萄》是约翰·斯坦贝克于1939年出版的一部美国文学名著。这本书获得了国家图书奖和普利策小说奖,1962年斯坦贝克获得诺贝尔奖时,这本书被引到了显著位置 接近 水 x 兰科植物是一个种类繁多、分布广泛的开花植物科,其花朵通常五彩缤纷、芳香四溢,俗称兰科。与菊科植物一样,它们也是开花植物中最大的两个科之一 植物。兰科植物约有27800种,分布于880属 接近 我不确定您对jQuery有多满意,但您可以通过一点Ja

我已经创建了图像缩略图,并使用以下代码链接该缩略图




移民 x 《愤怒的葡萄》是约翰·斯坦贝克于1939年出版的一部美国文学名著。这本书获得了国家图书奖和普利策小说奖,1962年斯坦贝克获得诺贝尔奖时,这本书被引到了显著位置

接近 水 x 兰科植物是一个种类繁多、分布广泛的开花植物科,其花朵通常五彩缤纷、芳香四溢,俗称兰科。与菊科植物一样,它们也是开花植物中最大的两个科之一 植物。兰科植物约有27800种,分布于880属

接近
我不确定您对jQuery有多满意,但您可以通过一点Javascript/jQuery来实现这一点。最好的方法是从某种类型的数据对象中的图像和文本开始。下面是一个Plunker链接,其中有一个基于原始代码的示例

在其中,我使用Javascript对象的基本数组创建了一个数据集

var data = [
    { src: "https://placehold.it/150x150?text=Image1", title: "Image 1", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a est mauris. Sed non sollicitudin lacus. Sed maximus facilisis purus, et blandit lectus vehicula in." },
    { src: "https://placehold.it/150x150?text=Image2", title: "Image 2", description: "Aenean accumsan metus ipsum, id vehicula felis semper sed. Sed hendrerit pulvinar porttitor. Etiam id tortor leo. Integer ex dui, vulputate vel iaculis sit amet, laoreet eu sem." },
    { src: "https://placehold.it/150x150?text=Image3", title: "Image 3", description: "Vivamus luctus est at sapien sollicitudin, nec mattis arcu condimentum. Vivamus sed varius diam. Nulla varius, tortor vel tempus feugiat, libero felis pellentesque mi, sit amet sagittis lacus massa et erat. " },
    { src: "https://placehold.it/150x150?text=Image4", title: "Image 4", description: "Vestibulum eu ex ac nunc pretium hendrerit vel in quam. Morbi imperdiet imperdiet pharetra." }
  ];
然后,我创建了几个简单的函数来管理模式导航,包括检查导航链接是否应该被禁用。我还使用javascript触发器打开模式,这样您就可以在单击时加载相应的项

var currentItem = 0;

function prevImg() {
  if (currentItem > 0) {
    currentItem--;
  }
  loadData();
}

function nextImg() {
  if (currentItem < data.length - 1) {
    currentItem++;
  }
  loadData();
}

function loadData() {
  $("#modalTitle").html(data[currentItem].title);
  $("#modalImg").attr("src", data[currentItem].src).attr("alt", data[currentItem].title);
  $("#modalText").html(data[currentItem].description);

  // enable/disable nav buttons  
  $("#navPrev").removeAttr("disabled");
  $("#navNext").removeAttr("disabled");

  if (currentItem == 0) {
    $("#navPrev").attr("disabled", "disabled");
  }
  else if (currentItem == data.length - 1) {
    $("#navNext").attr("disabled", "disabled");
  }
}

function openModal(idx) {
  currentItem = idx;
  loadData();
  $("#modal").modal();
}

希望有帮助

您可以使用javascript模式API隐藏当前模式并显示所需的内容

例如,我将一个函数关联到modals的按钮以执行此操作:

<body>

  <!--thumbnail section-->
  <section class="container">
    <div class="row add-bottom text-center">
      <a href="#migrant" class="thumbnail" data-toggle="modal">
        <img src=".." alt="Project Image" class="img-responsive center-block">
      </a>
      <br />
      <br />
      <a href="#water" class="thumbnail" data-toggle="modal">
        <img src=".." alt="Project Image1" class="img-responsive center-block">
      </a>
    </div>
  </section>

  <!--Modal Content-->
  <div class="modal fade" id="migrant" role="dialog">
    <div class="modal-dialog">

      <div class="modal-content">
        <div class="modal-header">

          <h3>Migrants</h3>
          <button type="button" class="close" data-dismiss="modal">x</button>
        </div>
        <div class="modal-body">
          <div class="modal-footer txt_center">
            <p>
              <img src=".." alt="migrant01" class="pull-right">The Grapes of Wrath is an Amer list novel written by John Steinbeck and published in 1939. The book won the National Book Award and Pulitzer Prize for fiction, and it was cited prominently
              when Steinbeck was awarded the Nobel Prize in 1962
            </p>
          </div>
        </div>
        <div class="modal-footer">
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="showModal('water')">Next</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="water" role="dialog">
    <div class="modal-dialog">

      <div class="modal-content">
        <div class="modal-header">
          <h3>Water</h3>
          <button type="button" class="close" data-dismiss="modal">x</button>
        </div>
        <div class="modal-body">
          <div class="modal-footer txt_center">
            <p>
              <img src=".." alt="water01" class="pull-right">The Orchidaceae are a diverse and widespread family of flowering plants, with blooms that are often colourful and often fragrant, commonly known as the orchid family. Along with the Asteraceae,
              they are one of the two largest families of flowering plants. The Orchidaceae have about 27,800 currently accepted species, distributed in about 880 genera
            </p>
          </div>
        </div>
        <div class="modal-footer">
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="showModal('migrant')">Previous</button>

          </div>
        </div>
      </div>
    </div>
  </div>


  <script>
    function showModal(id) {
      $(".modal").modal('hide');
      $("#" + id).modal();
    }

  </script>
</body>



移民 x 《愤怒的葡萄》是约翰·斯坦贝克于1939年出版的一部美国文学名著。这本书获得了国家图书奖和普利策小说奖,并且被引到了显著的位置 1962年斯坦贝克获得诺贝尔奖时

下一个 水 x 兰科植物是一个种类繁多、分布广泛的开花植物科,其花朵通常五彩缤纷、芳香四溢,俗称兰科。与菊科植物一起, 它们是开花植物中最大的两个科之一。兰科植物约有27800种,分布于880属

以前的 功能显示模式(id){ $(“.modal”).modal('hide'); $(“#”+id).modal(); }

javascript API:

我尝试了上述代码,但得到了一些不理想的结果(一次显示多个缩略图和模态),这与在Plunker中显示的结果不同。此外,在Plunker中,我还看到指定缩略图对应的模态没有显示,相反,它默认显示第一个模态。我还想在到达第一个/最后一个模式时关闭“上一个/下一个”按钮。如何使用模态正文中的导航箭头,从而重定向到上一个/下一个模态?我更新了答案,添加了其他功能,并解决了单击时总是显示第一个项目的问题。如果您按原样使用代码,它将工作。我绝对建议您在网上寻找一些课程,帮助您更好地理解jQuery和Bootstrap。如何为多个缩略图实现相同的功能?使用示例函数“showModal”作为按钮上的
onclick
事件中的回调,并传递要显示的缩略图的id。这样,代码将非常长,对于多个缩略图,很难实现。所以,我想把缩略图放在一个数组中。我该如何实现这个呢?
<body>
  <!--thumbnail section-->
  <section class="container">
    <div class="row add-bottom text-center thumbnails">
    </div>
  </section>
  <!--Modal Content-->
  <div id="modal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 id="modalTitle" class="modal-title"></h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-xs-1">
              <a id="navPrev" href="#" onclick="prevImg()"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span></a>
            </div>
            <div class="col-xs-10">
              <img id="modalImg" src="" alt="" class="pull-right" />
              <span id="modalText"></span>
            </div>
            <div class="col-xs-1">
              <a id="navNext" href="#" onclick="nextImg()"><span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></a>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
</body>
#navPrev, #navNext {
  color: #333;
  font-size: 2em;
}

#navPrev:hover, #navNext:hover {
  color: red;
}

#navPrev[disabled], #navNext[disabled] {
  color: #cdcdcd;
  cursor: default;
}
<body>

  <!--thumbnail section-->
  <section class="container">
    <div class="row add-bottom text-center">
      <a href="#migrant" class="thumbnail" data-toggle="modal">
        <img src=".." alt="Project Image" class="img-responsive center-block">
      </a>
      <br />
      <br />
      <a href="#water" class="thumbnail" data-toggle="modal">
        <img src=".." alt="Project Image1" class="img-responsive center-block">
      </a>
    </div>
  </section>

  <!--Modal Content-->
  <div class="modal fade" id="migrant" role="dialog">
    <div class="modal-dialog">

      <div class="modal-content">
        <div class="modal-header">

          <h3>Migrants</h3>
          <button type="button" class="close" data-dismiss="modal">x</button>
        </div>
        <div class="modal-body">
          <div class="modal-footer txt_center">
            <p>
              <img src=".." alt="migrant01" class="pull-right">The Grapes of Wrath is an Amer list novel written by John Steinbeck and published in 1939. The book won the National Book Award and Pulitzer Prize for fiction, and it was cited prominently
              when Steinbeck was awarded the Nobel Prize in 1962
            </p>
          </div>
        </div>
        <div class="modal-footer">
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="showModal('water')">Next</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="water" role="dialog">
    <div class="modal-dialog">

      <div class="modal-content">
        <div class="modal-header">
          <h3>Water</h3>
          <button type="button" class="close" data-dismiss="modal">x</button>
        </div>
        <div class="modal-body">
          <div class="modal-footer txt_center">
            <p>
              <img src=".." alt="water01" class="pull-right">The Orchidaceae are a diverse and widespread family of flowering plants, with blooms that are often colourful and often fragrant, commonly known as the orchid family. Along with the Asteraceae,
              they are one of the two largest families of flowering plants. The Orchidaceae have about 27,800 currently accepted species, distributed in about 880 genera
            </p>
          </div>
        </div>
        <div class="modal-footer">
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="showModal('migrant')">Previous</button>

          </div>
        </div>
      </div>
    </div>
  </div>


  <script>
    function showModal(id) {
      $(".modal").modal('hide');
      $("#" + id).modal();
    }

  </script>
</body>