javascript中返回頂部按鈕的實(shí)現(xiàn)
炫酷的返回頂部功能
js核心代碼
window.onscroll = function(){ var t = document.documentElement.scrollTop || document.body.scrollTop; var top_div = document.getElementById( "go" ); if( t >= 300 ) { top_div.style.display = "block"; } else { top_div.style.display = "none"; } } var ptt=20; function pageScroll() { document.getElementById('flypig').style.display='block'; window.scrollBy(0,-10); //每次上升10px if(document.body.scrollTop==0) { document.getElementById('flypig').style.marginBottom=ptt+'px'; ptt=ptt+10; } scrolldelay = setTimeout('pageScroll()',2); //2毫秒重復(fù)執(zhí)行一次 if(ptt>=1000){ ptt=0; document.getElementById('flypig').style.marginBottom=20+'px'; document.getElementById('flypig').style.display='none'; clearTimeout(scrolldelay); } //pageScroll ends }
html
<!DOCTYPE html> <html> <head> <title>返回頂部按鈕的實(shí)現(xiàn)</title> <meta charset="utf-8" /> <style> body{ margin:0px; } #flypig{ display: none; float: left; position: fixed; bottom: 20px; margin-left: 75%; cursor: pointer; margin-bottom: 20px; } #go{ display: block; float: left; position: fixed; bottom: 10px; margin-left: 75%; cursor: pointer; } </style> <script> window.onscroll = function(){ var t = document.documentElement.scrollTop || document.body.scrollTop; var top_div = document.getElementById( "go" ); if( t >= 300 ) { top_div.style.display = "block"; } else { top_div.style.display = "none"; } } var ptt=20; function pageScroll() { document.getElementById('flypig').style.display='block'; window.scrollBy(0,-10); //每次上升10px if(document.body.scrollTop==0) { document.getElementById('flypig').style.marginBottom=ptt+'px'; ptt=ptt+10; } scrolldelay = setTimeout('pageScroll()',2); //2毫秒重復(fù)執(zhí)行一次 if(ptt>=1000){ ptt=0; document.getElementById('flypig').style.marginBottom=20+'px'; document.getElementById('flypig').style.display='none'; clearTimeout(scrolldelay); } //pageScroll ends } </script> </head> <body> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <h1 style="text-align:center;">請(qǐng)往下滑</h1> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /><!--讓你的頁(yè)面足夠的長(zhǎng)--> <div style="display: none; float: left; position: fixed; margin-left: 50%; cursor: pointer; margin-bottom: 20px;" id="flypig"> <img src="/wp-content/themes/Jakesoft/images/flypig.gif"> </div> <div style="display: none; float: left; position: fixed; bottom: 10px; margin-left: 50%; cursor: pointer;" id="go" onclick="pageScroll()">返回頂部</div></html>
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- JavaScript實(shí)現(xiàn)返回頂部按鈕案例
- JavaScript 監(jiān)控微信瀏覽器且自帶返回按鈕時(shí)間
- 基于Javascript實(shí)現(xiàn)返回頂部按鈕
- javascript返回頂部的按鈕實(shí)現(xiàn)方法
- JavaScript阻止瀏覽器返回按鈕的方法
- javascript實(shí)現(xiàn)博客園頁(yè)面右下角返回頂部按鈕
- JS返回上一頁(yè)實(shí)例代碼通過圖片和按鈕分別實(shí)現(xiàn)
- js 點(diǎn)擊按鈕彈出另一頁(yè),選擇值后,返回到當(dāng)前頁(yè)
- JavaScript 彈出窗體點(diǎn)擊按鈕返回選擇數(shù)據(jù)的實(shí)現(xiàn)
- JavaScript實(shí)現(xiàn)返回頂部按鈕
相關(guān)文章
Jquery Autocomplete 結(jié)合asp.net使用要點(diǎn)
Jquery的Autocomplete是一個(gè)很好的智能提示插件,但是在實(shí)際使用過程中還是會(huì)遇到一些小問題.2010-10-10js窗口關(guān)閉提示信息(兼容IE和firefox)
這篇文章主要介紹了當(dāng)關(guān)閉窗口的時(shí)候,顯示窗口提示信息,友好的進(jìn)行提示,防止丟失重要信息,需要的朋友可以參考一下2015-10-10js文件中調(diào)用js的實(shí)現(xiàn)方法小結(jié)
JavaScript文件引用JavaScript文件的方法,需要的朋友可以參考下。2009-10-10JS雙向鏈表實(shí)現(xiàn)與使用方法示例(增加一個(gè)previous屬性實(shí)現(xiàn))
這篇文章主要介紹了JS雙向鏈表實(shí)現(xiàn)與使用方法,在之前鏈表的基礎(chǔ)上增加一個(gè)previous屬性實(shí)現(xiàn)的雙向鏈表功能,需要的朋友可以參考下2019-01-01Javscript調(diào)用iframe框架頁(yè)面中函數(shù)的方法
這篇文章主要介紹了Javscript調(diào)用iframe框架頁(yè)面中函數(shù)的方法,可實(shí)現(xiàn)iframe之間傳值或修改值,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11JavaScript中函數(shù)聲明與函數(shù)表達(dá)式的區(qū)別詳解
可能很多朋友只知道兩種聲明方式一個(gè)是函數(shù)聲明一個(gè)是函數(shù)表達(dá)式,具體有什么不同沒能說(shuō)得很好。事實(shí)上,JavaScript的解析器對(duì)函數(shù)聲明與函數(shù)表達(dá)式并不是一視同仁地對(duì)待的。下面看看這兩者到底有什么不同。2016-08-08JavaScript實(shí)現(xiàn)兼容IE6的收起折疊與展開效果實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)兼容IE6的收起折疊與展開效果,結(jié)合具體實(shí)例形式分析了javascript事件響應(yīng)及針對(duì)頁(yè)面元素屬性的動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09