1、錨點鏈接
<a href=‘#mao1’>mao1</a>
<a id='mao1'>我是mao1</a>
js:
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;//網頁被卷去的高
$('a[href^="#"]').click(function() {//其href屬性值以 "#"開頭的元素
var the_id = $(this).attr("href");
$('html, body').animate({
scrollTop: $(the_id).offset().top +"px"
}, 'slow');
return false;
});;
2、去到底部
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;//網頁被卷去的高度
var aHeight = document.documentElement.scrollHeight || document.body.scrollHeight;//網頁正文高
$("#register").on("click",function(){
$("body,html").animate({scrollTop:aHeight },1000);
});
3、回到頂部
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if(scrollTop >= 500) {
$("#backTop").css("display", "block")
} else {
$("#backTop").css("display", "none")
}
$("#backTop").on("click", function() {
$("body,html").animate({ scrollTop: 0 }, 1000);
})
4、判斷瀏覽器是否滾動到底部
1、判斷滾動條到底部,需要用到DOM的三個屬性值,即scrollTop、clientHeight、scrollHeight。
(1)scrollTop為滾動條在Y軸上的滾動距離。
(2)clientHeight為內容可視區(qū)域的高度。
(3)scrollHeight為內容可視區(qū)域的高度加上溢出(滾動)的距離。
從這個三個屬性的介紹就可以看出來,滾動條到底部的條件即為scrollTop + clientHeight == scrollHeight。
2、代碼:
$(window).scroll(function() {
var scrollTop = $(this).scrollTop();
var windowHeight = $(this).height();
var scrollHeight = $(document).height();
console.log(scrollTop+" "+windowHeight+" "+scrollHeight)
if(scrollTop + windowHeight == scrollHeight) {
alert("你已經到底部啦!");
}
});
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者