ASP.NET jQuery 實(shí)例9 通過控件hyperlink實(shí)現(xiàn)返回頂部效果
窗體滾動(dòng)事件:$(window).scroll(function(){...});
獲取窗體滾動(dòng)距離:$(window).scrollTop();
獲取窗體高度:$(window).height();
了解以上內(nèi)容就可以實(shí)現(xiàn)通過hyperlink控件實(shí)現(xiàn)返回頂部的效果了。
1.準(zhǔn)備界面結(jié)構(gòu)代碼:
<form id="form1" runat="server">
<div>
<asp:HyperLink ID="Top" runat="server"></asp:HyperLink>
<h1 style="text-align: center">
利用jQuery實(shí)現(xiàn)返回頂部效果</h1>
<div style="width: 800px; border: 1px; text-align: left; margin-left: 230px;">
。。。。。。(很多內(nèi)容,可以滾動(dòng))
</div>
<asp:HyperLink ID="backToTopLink" runat="server" CssClass="backToTop">回頂部</asp:HyperLink>
</div>
</form>
2.給回頂部控件添加樣式:
<style type="text/css">
.backToTop
{
background-color: Yellow;
width: 30px;
border-style: outset;
border-width: 1px;
text-align: center;
font-weight: bold;
font-family: Arial;
font-size: x-large;
cursor: pointer;
position:absolute; // 注意要設(shè)置為絕對(duì)位置
right: 100px;
}
</style>
3.添加實(shí)現(xiàn)置頂效果腳本代碼:
<script type="text/javascript">
$(document).ready(function () {
$("#backToTopLink").attr("title", "回頂部");
$("#backToTopLink").attr("href", "#Top"); // 通過鏈接實(shí)現(xiàn)置頂
$(window).scroll(function () {
if ($(window).scrollTop() <= 100) {
$("#backToTopLink").fadeOut(200);
}
else {
$("#backToTopLink").fadeIn(400);
}
var v_Top = $(window).height() - $(".backToTop").height() - 10 + $(window).scrollTop(); // 動(dòng)態(tài)計(jì)算滾動(dòng)后置頂按鈕top位置
$(".backToTop").css("top", v_Top + "px");
});
});
</script>
注意,本代碼只是為了演示hyperlink控件來實(shí)現(xiàn)返回頂部的效果。還可以通過jQuery的動(dòng)畫效果,實(shí)現(xiàn)平滑置頂。
平滑過渡返回頂部代碼如下:
$('#backToTopLink').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); // 替換$("#backToTopLink").attr("href", "#Top");即可
相關(guān)文章
jquery插件jquery.dragscale.js實(shí)現(xiàn)拖拽改變元素大小的方法(附demo源碼下載)
這篇文章主要介紹了jquery插件jquery.dragscale.js實(shí)現(xiàn)拖拽改變元素大小的方法,涉及jquery針對(duì)鼠標(biāo)事件的響應(yīng)及頁面元素動(dòng)態(tài)操作的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02jQuery中attr()和prop()在修改checked屬性時(shí)的區(qū)別
使用語句$.attr('checked',true),將復(fù)選框的屬性改為被選中,在chrome瀏覽器中第一次點(diǎn)擊有效后面就不行了,IE8倒是沒有問題2014-07-07jQuery實(shí)現(xiàn)簡單漂亮的Nav導(dǎo)航菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡單漂亮的Nav導(dǎo)航菜單效果,涉及jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)遍歷與操作頁面元素屬性的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03jQuery實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)簡單飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07jQuery實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03jQuery對(duì)JSON數(shù)據(jù)進(jìn)行排序輸出的方法
這篇文章主要介紹了jQuery對(duì)JSON數(shù)據(jù)進(jìn)行排序輸出的方法,涉及jQuery中g(shù)etJSON與sort等方法的使用技巧,需要的朋友可以參考下2015-06-06webuploader模態(tài)框ueditor顯示問題解決方法
這篇文章主要為大家詳細(xì)介紹了webuploader模態(tài)框ueditor顯示問題的解決,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12jQuery刪除節(jié)點(diǎn)的三個(gè)方法即remove()detach()和empty()
jQuery提供了三種刪除節(jié)點(diǎn)的方法,即remove(),detach()和empty(),下面為大家詳細(xì)介紹下jQuery刪除節(jié)點(diǎn)三個(gè)方法的具體使用2013-12-12