繁体   English   中英

如何在单击轮播的下一个或上一个 btn 时停止滚动顶部?

[英]How to stop scroll top on clicking next or prev btn of Carousel?

以下是使用简单的旋转木马,即使它看起来在这里很好的编码,但是在浏览器每当试图点击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 的新手一样。

提前致谢

干杯..

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

提示#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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM