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

jQuery實(shí)現(xiàn)返回頂部功能適合不支持js的瀏覽器

 更新時(shí)間:2014年08月19日 08:41:15   投稿:whsnow  
a標(biāo)簽指向錨點(diǎn)top,可以在頂部防止一個(gè)a name=top的錨點(diǎn),這樣在瀏覽器不支持js時(shí)也可以實(shí)現(xiàn)返回頂部的效果了

很多網(wǎng)站上都有返回頂部的效果,本文闡述如何使用jquery實(shí)現(xiàn)返回頂部按鈕。

首先需要在頂部添加如下html元素:

<p id="back-to-top"><a href="#top" rel="external nofollow" ><span></span>返回頂部</a></p>

其中a標(biāo)簽指向錨點(diǎn)top,可以在頂部防止一個(gè)<a name="top"></a>的錨點(diǎn),這樣在瀏覽器不支持js時(shí)也可以實(shí)現(xiàn)返回頂部的效果了。

要想讓返回頂部的圖片顯示在右側(cè),還需要一些css樣式,如下:

/*returnTop*/ 
p#back-to-top{ 
position:fixed; 
display:none; 
bottom:100px; 
right:80px; 
} 
p#back-to-top a{ 
text-align:center; 
text-decoration:none; 
color:#d1d1d1; 
display:block; 
width:64px; 
/*使用CSS3中的transition屬性給跳轉(zhuǎn)鏈接中的文字添加漸變效果*/ 
-moz-transition:color 1s; 
-webkit-transition:color 1s; 
-o-transition:color 1s; 
} 
p#back-to-top a:hover{ 
color:#979797; 
} 
p#back-to-top a span{ 
background:transparent url(/static/imgs/sprite.png?1202) no-repeat -25px -290px; 
border-radius:6px; 
display:block; 
height:64px; 
width:56px; 
margin-bottom:5px; 
/*使用CSS3中的transition屬性給<span>標(biāo)簽背景顏色添加漸變效果*/ 
-moz-transition:background 1s; 
-webkit-transition:background 1s; 
-o-transition:background 1s; 
} 
#back-to-top a:hover span{ 
background:transparent url(/static/imgs/sprite.png?1202) no-repeat -25px -290px; 
}

上面樣式中的背景圖片是雪碧圖,下面提供兩個(gè)單獨(dú)的返回頂部圖片方便朋友們使用:

有了html和樣式,我們還需要用js控制返回頂部按鈕,在頁(yè)面滾動(dòng)時(shí)漸隱漸現(xiàn)返回頂部按鈕。

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js"></script> 
<script> 
$(function(){ 
//當(dāng)滾動(dòng)條的位置處于距頂部100像素以下時(shí),跳轉(zhuǎn)鏈接出現(xiàn),否則消失 
$(function () { 
$(window).scroll(function(){ 
if ($(window).scrollTop()>100){ 
$("#back-to-top").fadeIn(1500); 
} 
else 
{ 
$("#back-to-top").fadeOut(1500); 
} 
}); 

//當(dāng)點(diǎn)擊跳轉(zhuǎn)鏈接后,回到頁(yè)面頂部位置 

$("#back-to-top").click(function(){ 
$('body,html').animate({scrollTop:0},1000); 
return false; 
}); 
}); 
}); 
</script>

這樣就可以了。

注意在載入頁(yè)面后需要向下拖動(dòng)一點(diǎn)滾動(dòng)條才可以看到返回頂部的效果圖。

相關(guān)文章

最新評(píng)論