1 回答

TA貢獻1828條經驗 獲得超3個贊
你的問題就在這里
$('a[href*=#]').on('click', function(e) { ...
要定位具有以您將使用的字符(而不是)href開頭的屬性的錨元素,這意味著以 開頭,并且您還可以在搜索字符周圍添加一些括號,如下所示#^*
$('a[href^="#"]').on('click', function(e) { ...
檢查如下:
注意:我特意給了 body 1000px 的高度,這樣我們就可以在這里測試和運行。
$(function() {
$('a[href*="#"]').on('click', function(e) {
e.preventDefault();
$('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top}, 500, 'linear');
});
});
body {
height: 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="{% static 'js/home.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/home.css' %}">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<section id="section01" class="demo">
<h1> Welcome to PHARSYS </h1>
<h2>Scroll for Login or Signup</h2>
<a href="#section02"><span></span></a>
</section>
<section id="section02" class="demo">
<h1>Login</h1><br>
<h2>Press the button to log in to the system.
<br><br>
</h2>
<a href="#section03"><span></span>Scroll</a>
</section>
<section id="section03" class="demo">
<h1>Signup</h1>
<h2>Press the button to register to the system</h2>
</section>
您還可以使用*
代替 作為通配符^
,然后它將匹配#
在其href
屬性中的任何位置具有字符的錨元素,而不僅僅是以 開頭#
。
- 1 回答
- 0 關注
- 171 瀏覽
添加回答
舉報