ITPub博客

首页 > IT职业 > IT图书 > jQuery 实现广告图fixed与scroll关联

jQuery 实现广告图fixed与scroll关联

IT图书 作者:founderwzh 时间:2021-09-09 10:03:20 0 删除 编辑

html代码部分:

<div class="arc_box">    <div class="arc_left">左侧</div>    <div class="arc_right">    右侧    <a class="teachers_right_ad mt20">广告块</a>    </div></div>

jQuery代码部分

$(function(){	var ad = $(".teachers_right_ad"); //声明对象	var ad_f = $(".arc_right");			//对象父元素	var ad_gf = $(".arc_box");			//对象父元素的父元素	var offset = ad.offset();	var box_h = ad.outerHeight(true);	var h=ad_gf.offset().top+ad_gf.outerHeight()-box_h; //计算触发ad停止fixed的临界点	var hf=ad_gf.offset().top+ad_gf.outerHeight();			//计算触发ad停止absolute的临界点	var hh=ad_gf.height()-ad.outerHeight(true);				//计算adabsolute的top值	ad_f.css({"position":"relative"});									//给定ad的父元素position relative	$(window).scroll(function(){	if($(window).scrollTop()>=offset.top&&$(window).scrollTop()<h){		ad.css({"position":"fixed","top":"0px"});	}	else if($(window).scrollTop()>=h &&$(window).scrollTop()<hf){		ad.css({"position":"absolute","top":hh+"px"});	}	 else {		ad.css({"position":"static"});	}	});})


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4605/viewspace-2815671/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 6月30号的第三天
请登录后发表评论 登录
全部评论

注册时间:2019-07-10

  • 博文量
    159
  • 访问量
    738