欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javaScript实现滚动条事件详解

程序员文章站 2022-07-06 21:06:44
本文实例为大家分享了js实现滚动条事件的具体代码,供大家参考,具体内容如下 代码: <...

本文实例为大家分享了js实现滚动条事件的具体代码,供大家参考,具体内容如下

javaScript实现滚动条事件详解

代码:

<html>

 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <title></title>

  <style>
   body {
    margin: 0;
    padding: 0;
   }

   .cont {
    height: 7000px;
   }

   #top {
    position: fixed;
    width: 100%;
    height: 55px;
    top: 0px;
    left: 0px;
    background-color: rosybrown;
    display: none;
   }

   #left {
    position: fixed;
    width: 50px;
    height: 400px;
    top: 150px;
    left: 50px;
    background-color: cadetblue;
    display: none;
   }

   #left ul {
    list-style: none;
    padding-left: 0px;
   }

   #left ul li {
    border: 1px solid white;
    /*color: azure;*/
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin: 15px auto;
    /*display: none;*/
   }

   .a {
    background-color: burlywood;
    color: #ffffff;
   }
  </style>

  <script>
   //滚动事件
   function myscroll() {

    var i = document.body.scrolltop;
    var top = document.getelementbyid("top");
    var left = document.getelementbyid("left");
    var f1 = document.getelementbyid("f1");
    var f2 = document.getelementbyid("f2");
    var f3 = document.getelementbyid("f3");
    var f4 = document.getelementbyid("f4");
    var f5 = document.getelementbyid("f5");
    var f6 = document.getelementbyid("f6");

    //控制顶部
    if(i >= 1000) {
     top.style.display = "block";
     top.innerhtml = i;
    } else {
     top.style.display = "none";
    }

    //控制左侧
    if(i >= 2000) {
     left.style.display = "block";
    } else {
     left.style.display = "none";
    }

    //显示楼层

    if(i >= 2000 && i <= 2500) {
     f1.classname = "a";
     f2.classname = "";
     f3.classname = "";
     f4.classname = "";
     f5.classname = "";
     f6.classname = "";
    } else if(i > 2500 && i <= 3000) {
     f1.classname = "";
     f2.classname = "a";
     f3.classname = "";
     f4.classname = "";
     f5.classname = "";
     f6.classname = "";
    } else if(i > 3000 && i <= 3500) {
     f1.classname = "";
     f2.classname = "";
     f3.classname = "a";
     f4.classname = "";
     f5.classname = "";
     f6.classname = "";
    } else if(i > 3500 && i <= 4000) {
     f1.classname = "";
     f2.classname = "";
     f3.classname = "";
     f4.classname = "a";
     f5.classname = "";
     f6.classname = "";
    } else if(i > 4000 && i <= 4500) {
     f1.classname = "";
     f2.classname = "";
     f3.classname = "";
     f4.classname = "";
     f5.classname = "a";
     f6.classname = "";
    } else if(i > 4500 && i <= 5000) {
     f1.classname = "";
     f2.classname = "";
     f3.classname = "";
     f4.classname = "";
     f5.classname = "";
     f6.classname = "a";
    }

   }
  </script>

 </head>

 <body onscroll="myscroll()">
  <div id="left">
   <ul>
    <li id="f1">1f</li>
    <li id="f2">2f</li>
    <li id="f3">3f</li>
    <li id="f4">4f</li>
    <li id="f5">5f</li>
    <li id="f6">6f</li>

   </ul>

  </div>
  <div id="top"></div>
  <div class="cont"></div>

 </body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。