JS采用絕對(duì)定位實(shí)現(xiàn)回到頂部效果完整實(shí)例
本文實(shí)例講述了JS采用絕對(duì)定位實(shí)現(xiàn)回到頂部效果。分享給大家供大家參考,具體如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>絕對(duì)定位回到頂部按鈕</title> <style type="text/css"> body{margin:0px;padding:0px;height:2500px;background:#6f0024;} #div1{width:120px;height:34px;right:4px;bottom:5px;cursor:pointer;background:url(images/ToTop.png) no-repeat;position:fixed;_position:absolute;display:none;} </style> <script type="text/javascript"> //絕對(duì)定位隱藏顯示 function getScroll(id){ var obj = document.getElementById(id); var timer = null; positionFixed(obj); if(obj){ obj.style.display = 'none'; window.onscroll=function(){ getScrollTop() > 0 ? obj.style.display = "block" : obj.style.display = "none"; } obj.onclick=function(){ var timer = setInterval(sMove,10); function sMove(){ setScrollTop(getScrollTop() / 1.5); if(getScrollTop() < 1)clearInterval(timer); } } } } //判斷IE6 function positionFixed(obj){ var iE6 = !-[1,] && !window.XMLHttpRequest; if(obj){ var top = obj.offsetTop; if(iE6){ document.documentElement.style.textOverflow = "ellipsis"; obj.style.position = "absolute"; obj.style.setExpression("top", "eval(documentElement.scrollTop + " + top + ') + "px"'); } } } //獲取滾動(dòng)條Top function getScrollTop(){ return document.documentElement.scrollTop || document.body.scrollTop; } //回到頂部 function setScrollTop(value){ document.documentElement.scrollTop = value; document.body.scrollTop = value; } window.onload = function(){ getScroll('div1'); }; </script> </head> <body> <div id="div1"></div> </body> </html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- 頁(yè)面回到頂部的三種實(shí)現(xiàn)(錨標(biāo)記,js)
- js滾動(dòng)條回到頂部的代碼
- js隱藏與顯示回到頂部按鈕及window.onscroll事件應(yīng)用
- 用原生js做個(gè)簡(jiǎn)單的滑動(dòng)效果的回到頂部
- 原生js實(shí)現(xiàn)淘寶首頁(yè)點(diǎn)擊按鈕緩慢回到頂部效果
- 原生JS實(shí)現(xiàn)平滑回到頂部組件
- js+css實(shí)現(xiàn)回到頂部按鈕(back to top)
- JavaScript簡(jiǎn)單實(shí)現(xiàn)網(wǎng)頁(yè)回到頂部功能
- 實(shí)現(xiàn)變速回到頂部的JavaScript代碼
- JS實(shí)現(xiàn)帶動(dòng)畫(huà)的回到頂部效果
相關(guān)文章
Bootstrap modal使用及點(diǎn)擊外部不消失的解決方法
這篇文章主要為大家詳細(xì)介紹了Bootstrap modal使用及點(diǎn)擊外部不消失的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Bootstrap實(shí)現(xiàn)帶暫停功能的輪播組件(推薦)
最近小編在項(xiàng)目中用到了一款帶輪播組件,功能非常齊全,可以設(shè)置各種標(biāo)題樣式、動(dòng)畫(huà)效果、輪播時(shí)間、開(kāi)始暫停等功能,下面小編通過(guò)本文給大家詳細(xì)介紹下實(shí)現(xiàn)過(guò)程,需要的朋友參考下吧2016-11-11js css+html實(shí)現(xiàn)簡(jiǎn)單的日歷
這篇文章主要為大家詳細(xì)介紹了由html、css、javascript結(jié)合實(shí)現(xiàn)的簡(jiǎn)單日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07基于Proxy的小程序狀態(tài)管理實(shí)現(xiàn)
這篇文章主要介紹了基于Proxy的小程序狀態(tài)管理實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06JavaScript實(shí)現(xiàn)封裝一個(gè)快速生成目錄樹(shù)的全局腳本
目錄樹(shù)可以很好的介紹項(xiàng)目中各文件目錄的用途,幫助讀者了解整個(gè)項(xiàng)目結(jié)構(gòu)。本文就來(lái)用JavaScript封裝一個(gè)快速生成目錄樹(shù)的全局腳本,希望對(duì)大家有所幫助2023-03-03