午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    js簡單的點擊返回頂部效果實現(xiàn)方法
    來源:易賢網(wǎng) 閱讀:944 次 日期:2016-06-30 09:25:28
    溫馨提示:易賢網(wǎng)小編為您整理了“js簡單的點擊返回頂部效果實現(xiàn)方法”,方便廣大網(wǎng)友查閱!

    當(dāng)頁面特別長的時候,用戶想回到頁面頂部,必須得滾動好幾次滾動鍵才能回到頂部,如果在頁面右下角有個“返回頂部”的按鈕,用戶點擊一下,就可以回到頂部,對于用戶來說,是一個比較好的體驗。

    實現(xiàn)原理:當(dāng)頁面加載的時候,把元素定位到頁面的右下角,當(dāng)頁面滾動時,元素一直位于右下角,當(dāng)用戶點擊的時候,頁面回到頂部。

    要點一:document.documentelement.clientwidth || document.body.clientwidth; 獲得可視區(qū)的寬度。后面是兼容chrome,前面是兼容其它瀏覽器。

    要點二:otop.style.left = screenw - otop.offsetwidth +px; 當(dāng)頁面加載時,讓元素的位置位于頁面最右邊,用可視區(qū)的寬度減去元素本身的寬度。

    要點三:otop.style.top = screenh - otop.offsetheight + scrolltop +px; 當(dāng)頁面滾動時,元素的y坐標(biāo)位置等于可視區(qū)的高度減去元素本身的高度,加上滾動距離。

    要點四:document.documentelement.scrolltop = document.body.scrolltop =0; 當(dāng)點擊元素時,讓頁面的滾動距離為0.寫兩個是為了兼容。

    上代碼:

    <!doctype html>

    <html>

    <head>

    <meta charset=utf-8 />

    <title>無標(biāo)題文檔</title>

    <style>

    body{margin:0; padding:0}

    #to_top{width:30px; height:40px; padding:20px; font:14px/20px arial; text-align:center; background:#06c; position:absolute; cursor:pointer; color:#fff}

    </style>

    <script>

    window.onload = function(){

    var otop = document.getelementbyid(to_top);

    var screenw = document.documentelement.clientwidth || document.body.clientwidth;

    var screenh = document.documentelement.clientheight || document.body.clientheight;

    otop.style.left = screenw - otop.offsetwidth +px;

    otop.style.top = screenh - otop.offsetheight + px;

    window.onscroll = function(){

    var scrolltop = document.documentelement.scrolltop || document.body.scrolltop;

    otop.style.top = screenh - otop.offsetheight + scrolltop +px;

    }

    otop.onclick = function(){

    document.documentelement.scrolltop = document.body.scrolltop =0;

    }

    }

    </script>

    </head>

    <body style=height:1000px;>

    <h1>返回頂部</h1>

    <div id=to_top>返回頂部</div>

    </body>

    </html>

    更多信息請查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:js簡單的點擊返回頂部效果實現(xiàn)方法
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)