JS實現跳轉到頁面任何地方

來源:互聯網
上載者:User

標籤:

要實現兩個內容:

1、從A頁面跳轉到B頁面任何地方

方法:用id對要跳轉的地方進行標記。

首先,在A頁面可以設一個連結

1 <a href = "b.html#pos" target = "_blank">點擊跳轉</a>

然後在B頁面要跳轉的地方

<div id = "pos"></div>

這樣就成功實現跳轉。

2、從B頁面某處跳轉到該頁面任何地方

方法:與上述思路一樣,但如果是按鈕的話需要添加onclick事件。

<a href = "#pos">點擊跳轉</a>......<div id = "pos">跳轉到這裡</div>

如果是點擊按鈕跳轉的話。。。

<script type = "text/javascript">    function on_scroll() {       document.getElementById("anchor_scroll").click();    }</script><button onclick = on_scroll();>點擊跳轉</button><a href = "#pos" id = "anchor_scroll" style = "display:none">點擊跳轉</a>......<div id = "pos">跳轉到這裡</div>

 

還有個方法是用jQuery的animate動畫方法,這個使用的還不是很熟練。

 <script type="text/javascript"> function click_scroll() {  var scroll_offset = $("#pos").offset();  //得到pos這個div層的offset,包含兩個值,top和left  $("body,html").animate({   scrollTop:scroll_offset.top  //讓body的scrollTop等於pos的top,就實現了滾動   },0); } </script> <button onclick="click_scroll();" >點擊button跳轉</button>...這裡是很多文字,把頁面撐開,撐出捲軸...<div id="pos">跳轉到這裡</div>

這樣有個好處就是可以控制跳轉的時間,其中的0代表跳轉時間,調成1000則跳轉時間會變長。。。

總之,通過網上的學習,我深刻地瞭解到,id是個好東西。。。

JS實現跳轉到頁面任何地方

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.