HTML學習筆記之二(回到頂部 與 回到底部),學習筆記回到頂部

來源:互聯網
上載者:User

HTML學習筆記之二(回到頂部 與 回到底部),學習筆記回到頂部

回到頂部 回到底部

回到頂部的倆種方式

 一、使用js

      

 $('html, body').animate({ scrollTop: 0 }, 'fast');//帶動畫        $('html,body').scrollTop(0); //不帶動畫

     
  $(window).scroll(function () {            //You've scrolled this much:               $('p').text("You've scrolled " + $(window).scrollTop() + " pixels");        });



二、使用 a 標籤的name屬性

          

 <a name="top">top</a>            <a href="#top">Click here go back to the top.</a>



三、擷取高度


 1. 整個文檔高度

      

 var body = document.body,            html = document.documentElement;        var height = Math.max( body.scrollHeight, body.offsetHeight,                       html.clientHeight, html.scrollHeight, html.offsetHeight );       // 或者        var height = $(document).height();



 2. 當前螢幕高度

       

var wheight = $(window).height();


HTML代碼

<!-- 側邊欄 按鈕-->        <div id="back-top">  <button class="styled-button">TOP</button></div><div id="back-end">  <button class="styled-button">TOP</button></div><!--底部 內容-->        <div id="footer"></div>


js代碼

jQuery(document).ready(function($){    /**     * 回到頂部     */    $('#back-top').click(function(){        $('html,body').stop();        $('html,body').animate({            scrollTop:'0px'        },1000);    });        /**     * 回到底部     */    $('#back-end').click(function(){        $('html,body').stop();        $('html,body').animate({            scrollTop:$('#footer').offset().top        },1000);    });});

//回到頂部的 顯示 隱藏代碼   $(document).ready(function(){  // hide #back-top first  $("#back-top").hide();  // fade in #back-top  $(function () {    $(window).scroll(function () {      if ($(this).scrollTop() > 100) {        $('#back-top').fadeIn();      } else {        $('#back-top').fadeOut();      }    });    // scroll body to 0px on click    $('#back-top').click(function () {      $('body,html').animate({        scrollTop: 0      }, 'fast');      return false;    });  });});



css代碼

#back-top{position: fixed; bottom:20px; right: 2%; z-index: 100; }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.