jquery結(jié)合css實現(xiàn)返回頂部功能
css操作
CSS
$("").css(name|pro|[,val|fn])
位置
$("").offset([coordinates])
$("").position()
$("").scrollTop([val])
$("").scrollLeft([val])
尺寸
$("").height([val|fn])
$("").width([val|fn])
$("").innerHeight()
$("").innerWidth()
$("").outerHeight([soptions])
$("").outerWidth([options])
實例返回頂部
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-2.2.3.js"></script>
<script>
window.onscroll=function(){
var current=$(window).scrollTop();
console.log(current)
if (current>100){
$(".returnTop").removeClass("hide")
}
else {
$(".returnTop").addClass("hide")
}
}
function returnTop(){
// $(".div1").scrollTop(0);
$(window).scrollTop(0)
}
</script>
<style>
body{
margin: 0px;
}
.returnTop{
height: 60px;
width: 100px;
background-color: darkgray;
position: fixed;
right: 0;
bottom: 0;
color: greenyellow;
line-height: 60px;
text-align: center;
}
.div1{
background-color: orchid;
font-size: 5px;
overflow: auto;
width: 500px;
}
.div2{
background-color: darkcyan;
}
.div3{
background-color: aqua;
}
.div{
height: 300px;
}
.hide{
display: none;
}
</style>
</head>
<body>
<div class="div1 div">
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
</div>
<div class="div2 div"></div>
<div class="div3 div"></div>
<div class="returnTop hide" onclick="returnTop();">返回頂部</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.div1{
height: 100px;
width: 100px;
background-color: red;
}
.div3{
height: 120px;
width: 120px;
background-color: seagreen;
}
.div2{
position: relative;
}
.div4{
background-color: aquamarine;
width: 150px;
height: 150px;
padding: 5px;
margin: 6px;
border: 4px solid green;
}
.div5{
width: 50%;
height: 200px;
overflow: auto;
}
.div6{
width: 100%;
height: 1000px;
}
.div5{
background-color: aquamarine;
}
.div6{
background-color: chocolate;
}
.div7{
width: 90px;
height: 60px;
position: fixed;
right: 20px;
bottom: 20px;
background-color: yellow;
text-align: center;
/*文本橫向居中*/
line-height: 60px;
/*文本行高*/
}
.hide{
display: none;
}
</style>
</head>
<body>
<!-- <div class="div1"></div>-->
<!-- <div class="div2">-->
<!-- <div class="div3"></div>-->
<!-- </div>-->
<!-- <div class="div4"></div>-->
<!-- <script src="jquery-3.3.1.js"></script>-->
<!-- <script>-->
<!-- // 計算離視口偏移量-->
<!-- console.log($('.div1').offset().left); // 0-->
<!-- console.log($('.div1').offset().top); // 0-->
<!-- console.log($('.div3').offset().left); // 0-->
<!-- console.log($('.div3').offset().top); // 100-->
<!-- // 計算離已定位的父標簽的偏移量(注意是已定位)-->
<!-- console.log($('.div3').position().left); // 0-->
<!-- console.log($('.div3').position().top); // 0-->
<!-- // 計算標簽尺寸-->
<!-- console.log($('.div4').height()); // 150(width: 150px)-->
<!-- // console.log($('.div4').height('200px')) // 高度變成200px-->
<!-- console.log($('.div4').innerHeight()); // 160(width: 150px+padding: 5px+padding: 5px)-->
<!-- console.log($('.div4').outerHeight()); // 168(width: 150px+padding: 5px+padding: 5px+border: 4px+border: 4px)-->
<!-- console.log($('.div4').outerHeight(true)); // 180(width: 150px+padding: 5px+padding: 5px+border: 4px+border: 4px+margin: 6px+margin: 6px)-->
<!-- </script>-->
<!--滾動條監(jiān)聽并返回頂部實例-->
<div class="div5">
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
</div>
<div class="div6">
<button onclick="returnTop1()">return</button>
</div>
<div class="div7 hide" onclick="returnTop()">返回頂部</div>
<script src="jquery-3.3.1.js"></script>
<script>
window.onscroll=function () {
// onscroll 事件在元素滾動條在滾動時觸發(fā)(window對象事件)
let num=$(window).scrollTop(); // 左右滾動條是scrollLeft
// scrollTop() 方法返回或設(shè)置匹配元素的滾動條的垂直位置(jquery)
console.log(num);
if (num>100) {
$('.div7').removeClass('hide');
}else{
$('.div7').addClass('hide');
};
};
function returnTop() {
$(window).scrollTop(0);
};
function returnTop1() {
$('.div5').scrollTop(0);
};
</script>
</body>
</html>
實例擴展:
CSS:
.backToTop {
display: none;
width: 18px;
line-height: 1.2;
padding: 5px 0;
background-color: #000;
color: #fff;
font-size: 12px;
text-align: center;
position: fixed;
_position: absolute;
right: 10px;
bottom: 100px;
_bottom: "auto";
cursor: pointer;
opacity: .6;
filter: Alpha(opacity=60);
}
jQuery代碼
(function() {
var $backToTopTxt = "返回頂部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
.text($backToTopTxt).attr("title", $backToTopTxt).click(function() {
$("html, body").animate({ scrollTop: 0 }, 120);
}), $backToTopFun = function() {
var st = $(document).scrollTop(), winh = $(window).height();
(st > 0)? $backToTopEle.show(): $backToTopEle.hide();
//IE6下的定位
if (!window.XMLHttpRequest) {
$backToTopEle.css("top", st + winh - 166);
}
};
$(window).bind("scroll", $backToTopFun);
$(function() { $backToTopFun(); }); })();:
到此這篇關(guān)于jquery結(jié)合css實現(xiàn)返回頂部功能的文章就介紹到這了,更多相關(guān)jquery的css操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
input:checkbox多選框?qū)崿F(xiàn)單選效果跟radio一樣
checkbox是多選,怎么才能讓他變成單選,效果跟radio一樣呢,本菜鳥就自己寫了個小程序,代碼很簡單2014-06-06
jQuery實現(xiàn)中獎播報功能(讓文本滾動起來) 簡單設(shè)置數(shù)值即可
這篇文章主要介紹了jQuery實現(xiàn)中獎播報功能(讓文本滾動起來) 簡單設(shè)置數(shù)值即可,文中通過實例代碼給大家詳細介紹,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
JQuery 引發(fā)兩次$(document.ready)事件
ASP.net MVC 做了個工程,不知道為什么Search按就總是執(zhí)行兩次。2010-01-01
jQuery簡單實現(xiàn)仿京東商城的左側(cè)菜單效果代碼
這篇文章主要介紹了jQuery簡單實現(xiàn)仿京東商城的左側(cè)菜單效果代碼,通過簡單的jQuery鼠標事件及元素動態(tài)變換實現(xiàn)樣式動態(tài)切換功能,非常簡單實用,需要的朋友可以參考下2015-09-09
jQuery EasyUI datagrid在翻頁以后仍能記錄被選中行的實現(xiàn)代碼
這篇文章主要介紹了jQuery EasyUI datagrid在翻頁以后仍能記錄被選中行的實現(xiàn)代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
jQuery實現(xiàn)導(dǎo)航滾動到指定內(nèi)容效果完整實例【附demo源碼下載】
這篇文章主要介紹了jQuery實現(xiàn)導(dǎo)航滾動到指定內(nèi)容效果,結(jié)合完整實例形式分析了頁面元素屬性動態(tài)變換操作相關(guān)技巧,涉及jQuery插件jquery.scrollto.js的使用,并附帶demo源碼下載供讀者下載參考,需要的朋友可以參考下2016-09-09
ajax頁面無刷新 IE下遭遇Ajax緩存導(dǎo)致數(shù)據(jù)不更新的問題
在做ajax頁面無刷新添加的時候,IE下遭遇Ajax緩存,因為剛開始并不知道IE有這個壞毛病,折騰好久,終于解決問題,曬出來和大家分享,希望可以幫助你們2012-12-12

