javascript回到頂部特效
更新時間:2016年07月30日 10:06:01 作者:行者杰客
這篇文章主要為大家詳細介紹了javascript回到頂部特效,具有一定的參考價值,感興趣的朋友可以參考一下
本文實例為大家分享了javascript回到頂部效果,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<title>回到頂部效果(原生js)</title>
<style type="text/css">
body{
margin: 0;
}
.bg{
width: 1022px;
margin: 0 auto;
}
.btn{
display: none;
width: 75px;
height: 75px;
background:url(../images/網(wǎng)頁top小圖標(返回頂部)/返回頂部-085.png) no-repeat left top;
position: fixed;
left: 50%;
margin-left: 530px;
bottom: 10px;
text-indent: -9999px;
outline: none;
}
.btn:hover{
background-position: 0 -75px;
}
</style>
<script>
var timer=null;
var isScroll=true;
//1.2構(gòu)造oScroll函數(shù)
function oScroll(){
var osTop=document.documentElement.scrollTop||document.body.scrollTop;//1.1滾動高度,兼容
var oSpeed=Math.ceil(osTop/4);//滾動速度
document.documentElement.scrollTop=document.body.scrollTop=osTop-oSpeed;
if(osTop==0){//判斷到達頂部,清理定時器
clearInterval(timer);
}
isScroll=true;
}
window.onload=function() {
var obtn=document.getElementById('btn');//獲取按鈕元素
var clientHeight=document.documentElement.clientHeight||document.body.clientHeight;
// 1.點擊返回按鈕事件
btn.onclick=function(){//為按鈕綁定點擊事件
timer=setInterval(oScroll,50);
}
window.onscroll=function() {
var osTop=document.documentElement.scrollTop||document.body.scrollTop;//1.1滾動高度,兼容
if (osTop>clientHeight) {
btn.style.display="block";
}else{
btn.style.display="none";
}
if(!isScroll){
clearInterval(timer);
}
isScroll=false;
}
}
</script>
</head>
<body>
<div class="bg">
<img src="../images/jz.jpg">
</div>
<a href="javascript:void(0);" title="返回頂部" id="btn" class="btn">返回頂部</a>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家學習JavaScript程序設(shè)計有所幫助。
相關(guān)文章
JS一維數(shù)組轉(zhuǎn)化為三維數(shù)組的實現(xiàn)示例
很多時候,跟后端接口開發(fā)對接時,因為需求的關(guān)系,需要前端地區(qū)數(shù)據(jù)跟后端的數(shù)據(jù)要一一對應,有時候需要的是多維數(shù)據(jù),這篇文章主要介紹了JS一維數(shù)組轉(zhuǎn)化為三維數(shù)組的實現(xiàn)示例,感興趣的可以了解一下2021-12-12
js在數(shù)組中刪除重復的元素自保留一個(兩種實現(xiàn)思路)
遍歷要刪除的數(shù)組arr, 把元素分別放入另一個數(shù)組tmp中,在判斷該元素在arr中不存在才允許放入tmp中,具體實現(xiàn)如下,需要的朋友可以看看
2014-08-08
基于insertBefore制作簡單的循環(huán)插空效果
這是一個基于insertBefore制作簡單的循環(huán)插空效果,實現(xiàn)的數(shù)字下面循環(huán)插空效果,給需要的朋友分享。
2015-09-09
JS中setTimeout的巧妙用法前端函數(shù)節(jié)流
這篇文章主要介紹了JS中setTimeout的巧妙用法前端函數(shù)節(jié)流 的相關(guān)資料,需要的朋友可以參考下
2016-03-03 
