上滑绝对定位

.fixedBar {
    z-index: 1010;
    position: fixed;
    top: 40px;
    left: 0;
    width: 100%;
    height: 50px;
}
var titleFix = {
    init:function(){
		this.titles = $(".yk-box .yk-head .yk-TitleBar");
		this.len = this.titles.length;
		this.now = -1;
		if(this.len <= 0) return false;
		this.bind();
    },
	bind:function (){
		var _this = this;
	    $(window).on("touchmove scroll resize orientationchange",function(){
			var nowTop = $(window).scrollTop()+40;// 40:height of the header
			var now=-1;
			for(var i=0;i<_this.len;i++){
				var otop = _this.titles[i].parentNode.offsetTop;				
				if(nowTop >= otop){
					now = i;					
				}
			}
			if(_this.now == now) return;			
			if(now != -1){
				_this.titles.eq(now).addClass("fixedBar");
			}
			_this.titles.eq(_this.now).removeClass('fixedBar');
			_this.now = now;		
		});
	}
};


titleFix.init();
posted @ 2017-11-03 12:03  孙凯亮  阅读(190)  评论(0编辑  收藏  举报