我正在寻找一种方法,只有在向右滚动约100%后,才能将页面的#header元素定位为“已修复”。
如果留下< 100%那么
#header { display:none; }
如果离开> 100%那么
#header { position:fixed, top:0, left:0, width:50px, height:50px, color:red, z-index:9999; }
我试过了:
<script type='text/javascript'>
var header = $("#header");
$(document).scroll(function() {
if($(this).scrollLeft() > 1920) {
header.css({"position" : "fixed", "top" : "0", "left" : "0", "width" : "50px", "height" : "50px", "background" : "red", "z-index" : "9999"});
} else {
header.css({"display" : "none"});
}
});
</script>
但它不起作用。是否可以使用百分比而不是像素数来制作条件? (100%而不是1920)
请问你能帮帮我吗?
答案 0 :(得分:1)
尝试使用“window.innerWidth”替换1920。这是一个JS DOM变量,用于浏览器窗口的宽度。