瀏覽網(wǎng)頁時叙赚,當(dāng)滾動條滾動的時候老客,會出現(xiàn)回到頂部的按鈕纠俭,有的按鈕是通過圖片實現(xiàn)的沿量,但是其實也可以通過純css+javascript的方式來實現(xiàn)它冤荆。
制作回到頂部按鈕
<div id="article"></div>
<div id="gotop">
<div class="arrow"></div>
<div class="stick"></div>
利用了div的border的特性繪制了三角形箭頭
.arrow{
border: 9px solid transparent;
border-bottom-color: #3DA0DB;
width: 0px;
height: 0px;
top:0px
}
其他的就看看注釋吧朴则,很簡單
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>doc</title>
<style>
.arrow{
border: 9px solid transparent;
border-bottom-color: #3DA0DB;
width: 0px;
height: 0px;
top:0px
}
.stick{
width: 8px;
height: 14px;
border-radius: 1px;
background-color: #3DA0DB;
top:15px;
}
#gotop div{
position: absolute;
margin: auto;
right: 0px;
left: 0px;
}
#gotop{
background-color: #dddddd;
height: 38px;
width: 38px;
border-radius: 3px;
display: block;
cursor: pointer;
position: fixed;
right: 50px;
bottom: 100px;
display: none;
}
</style>
</head>
<body>
<div id="article"></div>
<div id="gotop">
<div class="arrow"></div>
<div class="stick"></div>
</div>
<script src="http://cdn.staticfile.org/jquery/1.11.1-rc2/jquery.min.js"></script>
<script>
$(function(){
for(var i =0 ;i <100;i++){
$("#article").append("<p>xxxxxxxxxx<br></p>")
}
})
</script>
<script>
$(function(){
$(window).scroll(function(){ //只要窗口滾動,就觸發(fā)下面代碼
var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //獲取滾動后的高度
if( scrollt >200 ){ //判斷滾動后高度超過200px,就顯示
$("#gotop").fadeIn(400); //淡入
}else{
$("#gotop").stop().fadeOut(400); //如果返回或者沒有超過,就淡出.必須加上stop()停止之前動畫,否則會出現(xiàn)閃動
}
});
$("#gotop").click(function(){ //當(dāng)點擊標(biāo)簽的時候,使用animate在200毫秒的時間內(nèi),滾到頂部
$("html,body").animate({scrollTop:"0px"},200);
});
});
</script>
</body>
</html>