让某div模块随屏幕滚动固定位置代码

www.tzvx.com 2021-01-11建站代码人已围观

简介css代码: .is-fixed { position: fixed; top: 10px; z-index: 99; } div class=topnav /div js代码1: jQuery(document).ready(function($){ var obj=null; var As=document.getElementById(starlist).getElementsByTagName(a); obj = As[0]; for(i=1;...

css代码:

.is-fixed {
    position: fixed;
    top: 10px;
    z-index: 99;
}

<div class="topnav"> </div>



js代码1:

jQuery(document).ready(function($){
  
			
    var obj=null;
    var As=document.getElementById('starlist').getElementsByTagName('a');
    obj = As[0];
    for(i=1;i<As.length;i++){if(window.location.href.indexOf(As[i].href)>=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;
    }
};
 

Tags:

热点文章