欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于JS實現(xiàn)回到頁面頂部的五種寫法(從實現(xiàn)到增強)

 更新時間:2016年09月03日 09:25:14   作者:小火柴的藍色理想  
這篇文章主要介紹了基于JS實現(xiàn)回到頁面頂部的五種寫法(從實現(xiàn)到增強)的相關(guān)資料,本文介紹的非常詳細,實用性也非常高,非常具有參考借鑒價值,需要的朋友可以參考下

寫法

【1】錨點

  使用錨點鏈接是一種簡單的返回頂部的功能實現(xiàn)。該實現(xiàn)主要在頁面頂部放置一個指定名稱的錨點鏈接,然后在頁面下方放置一個返回到該錨點的鏈接,用戶點擊該鏈接即可返回到該錨點所在的頂部位置

  [注意]關(guān)于錨點的詳細信息移步至此

<body style="height:2000px;">
<div id="topAnchor"></div>
<a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到頂部</a>
</body>

【2】scrollTop

  scrollTop屬性表示被隱藏在內(nèi)容區(qū)域上方的像素數(shù)。元素未滾動時,scrollTop的值為0,如果元素被垂直滾動了,scrollTop的值大于0,且表示元素上方不可見內(nèi)容的像素寬度

  由于scrollTop是可寫的,可以利用scrollTop來實現(xiàn)回到頂部的功能

  [注意]關(guān)于頁面的scrollTop的兼容問題詳細內(nèi)容移步至此

<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
<script>
test.onclick = function(){
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
</script>
</body>

【3】scrollTo()

  scrollTo(x,y)方法滾動當前window中顯示的文檔,讓文檔中由坐標x和y指定的點位于顯示區(qū)域的左上角

  設(shè)置scrollTo(0,0)可以實現(xiàn)回到頂部的效果

<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
<script>
test.onclick = function(){
scrollTo(0,0);
}
</script>
</body>

【4】scrollBy()

  scrollBy(x,y)方法滾動當前window中顯示的文檔,x和y指定滾動的相對量

  只要把當前頁面的滾動長度作為參數(shù),逆向滾動,則可以實現(xiàn)回到頂部的效果

<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
<script>
test.onclick = function(){
var top = document.body.scrollTop || document.documentElement.scrollTop
scrollBy(0,-top);
}
</script>
</body>

【5】scrollIntoView()

  Element.scrollIntoView方法滾動當前元素,進入瀏覽器的可見區(qū)域 

  該方法可以接受一個布爾值作為參數(shù)。如果為true,表示元素的頂部與當前區(qū)域的可見部分的頂部對齊(前提是當前區(qū)域可滾動);如果為false,表示元素的底部與當前區(qū)域的可見部分的尾部對齊(前提是當前區(qū)域可滾動)。如果沒有提供該參數(shù),默認為true

  使用該方法的原理與使用錨點的原理類似,在頁面最上方設(shè)置目標元素,當頁面滾動時,目標元素被滾動到頁面區(qū)域以外,點擊回到頂部按鈕,使目標元素重新回到原來位置,則達到預(yù)期效果

<body style="height:2000px;">
<div id="target"></div>
<button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
<script>
test.onclick = function(){
target.scrollIntoView();
}
</script>
</body>

增強

  下面對回到頂部的功能進行增強

【1】顯示增強

  使用CSS畫圖,將“回到頂部”變成可視化的圖形(如果兼容IE8-瀏覽器,則用圖片代替)

  使用CSS偽元素及偽類hover效果,當鼠標移動到該元素上時,顯示回到頂部的文字,移出時不顯示  

<style>
.box{
position:fixed;
right:10px;
bottom: 10px;
height:30px;
width: 50px; 
text-align:center;
padding-top:20px; 
background-color: lightblue;
border-radius: 20%;
overflow: hidden;
}
.box:hover:before{
top:50%
}
.box:hover .box-in{
visibility: hidden;
}
.box:before{
position: absolute;
top: -50%;
left: 50%;
transform: translate(-50%,-50%);
content:'回到頂部';
width: 40px;
color:peru;
font-weight:bold;
} 
.box-in{
visibility: visible;
display:inline-block;
height:20px;
width: 20px;
border: 3px solid black;
border-color: white transparent transparent white;
transform:rotate(45deg);
}
</style>
<body style="height:2000px;">
<div id="box" class="box">
<div class="box-in"></div>
</div> 
</body>

【2】動畫增強

  為回到頂部增加動畫效果,滾動條以一定的速度回滾到頂部

  動畫有兩種:一種是CSS動畫,需要有樣式變化配合transition;一種是javascript動畫,使用定時器來實現(xiàn)  

  在上面的5種實現(xiàn)中,scrollTop、scrollTo()和scrollBy()方法可以增加動畫,且由于無樣式變化,只能增加javascript動畫

  定時器又有setInterval、setTimeout和requestAnimationFrame這三種可以使用,下面使用性能最好的定時器requestAnimationFrame來實現(xiàn)

  [注意]IE9-瀏覽器不支持該方法,可以使用setTimeout來兼容

  1、增加scrollTop的動畫效果

  使用定時器,將scrollTop的值每次減少50,直到減少到0,則動畫完畢

<script>
var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
} 
});
}
</script>

  2、增加scrollTo()動畫效果

  將scrollTo(x,y)中的y參數(shù)通過scrollTop值獲取,每次減少50,直到減少到0,則動畫完畢

<script>
var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
scrollTo(0,oTop-50);
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
} 
});
}
</script>

  3、增加scrollBy()動畫效果

  將scrollBy(x,y)中的y參數(shù)設(shè)置為-50,直到scrollTop為0,則回滾停止

<script>
var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
scrollBy(0,-50);
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
} 
});
}
</script>

實現(xiàn)

  由于scrollTop、scrollBy()和scrollTo()方法,都以scrollTop值是否減少為0作為動畫停止的參照,且三個動畫的原理和實現(xiàn)都基本相似,性能也相似。最終,以最常用的scrollTop屬性實現(xiàn)動畫增強效果

  當然,如果覺得50的速度不合適,可以根據(jù)實際情況進行調(diào)整

<style>
.box{
position:fixed;
right:10px;
bottom: 10px;
height:30px;
width: 50px; 
text-align:center;
padding-top:20px; 
background-color: lightblue;
border-radius: 20%;
overflow: hidden;
}
.box:hover:before{
top:50%
}
.box:hover .box-in{
visibility: hidden;
}
.box:before{
position: absolute;
top: -50%;
left: 50%;
transform: translate(-50%,-50%);
content:'回到頂部';
width: 40px;
color:peru;
font-weight:bold;
} 
.box-in{
visibility: visible;
display:inline-block;
height:20px;
width: 20px;
border: 3px solid black;
border-color: white transparent transparent white;
transform:rotate(45deg);
}
</style>
<body style="height:2000px;">
<div id="box" class="box">
<div class="box-in"></div>
</div> 
</body>
<script>
var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn(){
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if(oTop > 0){
document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
timer = requestAnimationFrame(fn);
}else{
cancelAnimationFrame(timer);
} 
});
}
</script>

以上所述是小編給大家介紹的基于JS實現(xiàn)回到頁面頂部的五種寫法(從實現(xiàn)到增強)的全部敘述,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的,在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論