亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 JavaScript 上創建滾動效果

無法在 JavaScript 上創建滾動效果

慕婉清6462132 2024-01-22 16:59:19
我想創建一個外觀,允許當用戶按下“滾動”按鈕時,頁面將以滑動效果滾動。但它在我的頁面上不起作用。我嘗試將標記添加到我的 html 文件中,但它也不起作用。我找到了一個腳本,但它不起作用。我的錯誤在哪里?首頁.html    <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>home.js$(function() {  $('a[href*=#]').on('click', function(e) {    e.preventDefault();    $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top}, 500, 'linear');  });});
查看完整描述

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屬性中的任何位置具有字符的錨元素,而不僅僅是以 開頭#。



查看完整回答
反對 回復 2024-01-22
  • 1 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號