阅读背景:

元素position设置为fixed而未指定top、bottom、left、right时的定位

来源:互联网 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>fixed定位</title>

<style type="text/css">
* {margin:0; padding:0; border:0;}
#wrapper {width:1000px; height:2000px; margin: 0 auto;}
#div1 {height:100px; background-color:red;}
#div21 {width:100px; height:200px; background-color:blue; padding:10px; border:5px solid black;}
#div22 {width:50px; height:100px; background-color:#0F0; position:static; float:right;}
</style>

<script>
window.onscroll = function () {
	var scrollTop = document.documentElement.scrollTop;
	if(scrollTop > 115){
		document.getElementById("div22").style.position = "fixed";
	}
	else {
		document.getElementById("div22").style.position = "static";
	}
}
</script>
</head>

<body>
<div id="wrapper">
  <div id="div1"></div>
  <div id="div21">
    <div id="div22"></div>
  </div>
</div>
</body>
</html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tr



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: