<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

<style type="text/css">

  *{margin: 0;padding: 0;}

  html,body{height: 3000px}

  #box{

    width: 100%;

    height: 100px;

    background: red;

    position: fixed;

    top: 300px;

    text-align: center;

    line-height: 100px;

  }

</style>

</head>

<body>

  <div id="box">横条</div>

</body>

</html>

<script type="text/javascript">

    var b=document.getElementById("box");

    window.onscroll=function(){

      var sTop=document.documentElement.scrollTop||document.body.scrollTop;

      if(sTop>300){

        box.style.top=0;

      }else{

        box.style.top=parseInt(300-sTop)+"px";

      }

    }

</script>

posted on 2018-08-16 21:12  薛小白  阅读(143)  评论(0编辑  收藏  举报