【问题标题】:How to stop scroll top on clicking next or prev btn of Carousel?如何在单击 Carousel 的下一个或上一个 btn 时停止滚动顶部?
【发布时间】:2016-10-25 22:44:48
【问题描述】:

以下是使用中的简单轮播,即使在编码中看起来也不错,但是在浏览器上,每当尝试单击nextprev 按钮时,页面是scrolled 到HTML 的顶部。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .carousel-inner > .item > img,
  .carousel-inner > .item > a > img {
      width: 70%;
      margin: auto;
  }
  </style>
</head>
<body>
<h1>SAMPLE CONTENT HERE</h1>
    <h1>SAMPLE CONTENT HERE</h1>
    <h1>SAMPLE CONTENT HERE</h1>
    <h1>SAMPLE CONTENT HERE</h1>
<div class="container">
  <br>
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>
    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="img_chania.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_chania2.jpg" alt="Chania" width="460" height="345">
      </div>
    
      <div class="item">
        <img src="img_flower.jpg" alt="Flower" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_flower2.jpg" alt="Flower" width="460" height="345">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

data-target某处找到了原因,但是无法弄清楚,以及如何停止滚动顶部...

寻找最简单的解决方案来理解,作为 HTML 的新手..

提前致谢

干杯..

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap carousel


    【解决方案1】:

    只需将 href="#myCarousel" 更改为 data-target="#myCarousel" 即可。

    【讨论】:

      【解决方案2】:

      提示 #1

      你可以添加 event.preventDefault() 来防止 hashbang 的默认行为:

      $('a[class^=carousel-control-]').click(function (event) {
          event.preventDefault();
      });
      

      提示 #2

      如果你有 SmoothScroll 之类的插件,可以通过以下方式解绑:

      $('a[class^=carousel-control-]').unbind('click.SmoothScroll');
      

      要查找绑定到元素的所有点击事件,请使用:

      jQuery._data($('a[class^=carousel-control-]')[0], "events")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-01
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        相关资源
        最近更新 更多