jQuery實現(xiàn)簡單的回到頂部totop功能示例
更新時間:2017年10月16日 08:57:19 作者:向君
這篇文章主要介紹了jQuery實現(xiàn)簡單的回到頂部totop功能,涉及jQuery事件響應及頁面元素屬性動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了jQuery實現(xiàn)簡單的回到頂部totop功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>回到頂部</title>
<style type="text/css">
body{
width: 100%;
height: 10000px;
}
#totop{
width: 50px;
height: 50px;
line-height: 50px;
background: magenta;
font-size: 20px;
position: fixed;
right: 50px;
bottom: 50px;
}
</style>
</head>
<body>
</body>
<script src="js/jquery-1.8.3.min.js" ></script>
<script>
//寫在common.js文件中用來調用即可
//1獲取滾動條當前的位置
function getScrollTop() {
var scrollTop = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if (document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
};
//2獲取文檔完整的高度
function getScrollHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
};
//3回到頂部
function toTop(n) {
$(window).on('scroll', function() {
//console.log(getScrollTop()+"!"+getScrollHeight());
if ($("#totop").size() > 0) {
if (getScrollTop() < $(window).height() * n) {
$("#totop").remove();
}
} else {
if (getScrollTop() >= $(window).height() * n) {
$("body").after("<div id='totop'>totop</div>");
//插入了新標簽 ,記得添加樣式!
$("#totop").on('click', function() {
scroll(0,200);
});
}
}
});
};
//xxx.js文件來執(zhí)行
$(function(){
toTop(2);
})
</script>
</html>
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery窗口操作技巧總結》、《jQuery常用插件及用法總結》、《jquery中Ajax用法總結》、《jQuery表格(table)操作技巧匯總》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
相關文章
基于jquery實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
這篇文章主要為大家詳細介紹了基于jquery實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12
jquery-mobile表單的創(chuàng)建方法詳解
這篇文章主要介紹了jquery-mobile表單的創(chuàng)建方法,結合實例形式分析了jquery-mobile插件創(chuàng)建表單的具體操作步驟與各種常見表單元素的創(chuàng)建技巧,需要的朋友可以參考下2016-11-11
基于jQuery實現(xiàn)動態(tài)數(shù)字展示效果
Jq數(shù)據(jù)列表動態(tài)效果,動態(tài)更新,支持Ajax動態(tài)刷新。下面小編給大家介紹下基于jQuery實現(xiàn)動態(tài)數(shù)字展示效果,需要的朋友可以參考下2015-08-08
jQuery計算textarea中文字數(shù)(剩余個數(shù))的小程序
這篇文章主要介紹了jQuery計算textarea中文字數(shù)(剩余個數(shù))的示例程序,大家參考使用吧2013-11-11

