js滚动条固定

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
*{margin:0px auto; padding:0px;}
#shang{width:100%; height:200px; background-color:#CF6;}
#caidan{width:100%; height:100px; background-color:#9C0;}
.xuanxiang{width:200px; height:100px; background-color:#606; margin-top:20px;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<div id="shang"></div>
<div id="caidan"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
<div class="xuanxiang"></div>
</body>
<script type="text/javascript">
window.onscroll = function(){
            var m = document.getElementById("caidan");
    if(window.scrollY>=200){
        m.style.position="fixed";
        m.style.shang="0px";
        }else{
            m.style.position="";
            }
    }
</script>

</html>

原文地址:https://www.cnblogs.com/palpitate/p/8891246.html