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

    js簡單的點(diǎn)擊返回頂部效果實(shí)現(xiàn)方法
    來源:易賢網(wǎng) 閱讀:663 次 日期:2015-04-13 14:02:30
    溫馨提示:易賢網(wǎng)小編為您整理了“js簡單的點(diǎn)擊返回頂部效果實(shí)現(xiàn)方法”,方便廣大網(wǎng)友查閱!

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

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

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

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

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

    要點(diǎn)四:document.documentElement.scrollTop = document.body.scrollTop =0; 當(dāng)點(diǎn)擊元素時(shí),讓頁面的滾動距離為0.寫兩個(gè)是為了兼容。

    上代碼:

    <!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>

    更多信息請查看IT技術(shù)專欄

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

    2025國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(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)警報(bào)警專用圖標(biāo)