让某div模块随屏幕滚动固定位置代码
时间:2021-01-11 07:36:19
阅读:288人已围观
css代码:
.is-fixed {
position: fixed;
top: 10px;
z-index: 99;
}
js代码1:
.is-fixed {
position: fixed;
top: 10px;
z-index: 99;
}
js代码1:
jQuery(document).ready(function($){ var obj=null; var As=document.getElementById('starlist').getElementsByTagName('a'); obj = As[0]; for(i=1;i=0) obj=As[i];} obj.id='selected'; $(window).scroll(function() { var h = $("body").height() - window.getHeight(); //console.log(h); if ($(window).scrollTop() > 28 && h > 120) { $(".topnav").addClass("is-fixed").find("").fadeOut(400); } else if ($(window).scrollTop() < 28) { $(".topnav").removeClass("is-fixed").find("").fadeIn(400); } }); }); window.getHeight = function() { if (window.innerHeight != undefined) { return window.innerHeight; } else { var B = document.body , D = document.documentElement; return Math.min(D.clientHeight, B.clientHeight); } }
js代码2:
var scroll=0;
var navST=60; //该元素距离网页顶端60px
window.onscroll = function(){
if(document.documentElement.scrollTop || document.body.scrollTop>navST && scroll==0){
document.getElementsByTagName("topnav")[0].classList.add("is-fixed");
scroll=1;
}
else if(document.documentElement.scrollTop || document.body.scrollTop<=navST && scroll==1){
document.getElementsByTagName("topnav")[0].classList.remove("is-fixed");
scroll=0;
}
};