[英]how to make text scroll ( like text to slide by clicking prev n next icon) through javascript and jQuery
[英]How to stop scroll top on clicking next or prev btn of Carousel?
以下是使用簡單的旋轉木馬,即使它看起來在這里很好的編碼,但是在瀏覽器每當試圖點擊next
或prev
按鈕,頁面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.