首先,每次滚动页面时都要检查其偏移量
$(document).scroll(function() { checkOffset();});
如果在页脚之前将其放下10像素以下,则将其位置设为绝对位置。
function checkOffset() { if($('#social-float').offset().top + $('#social-float').height()>= $('#footer').offset().top - 10) $('#social-float').css('position', 'absolute'); if($(document).scrollTop() + window.innerHeight < $('#footer').offset().top) $('#social-float').css('position', 'fixed'); // restore when you scroll up}
请注意,
#social-float的父母应该是页脚的同级
<div > <div id="social-float"> something... </div></div><div id="footer"></div>
祝好运 :)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)