欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)垂直滾動(dòng)條效果

 更新時(shí)間:2017年01月18日 17:26:00   作者:秋天1014童話  
這篇文章為大家詳細(xì)主要介紹了JavaScript實(shí)現(xiàn)垂直滾動(dòng)條效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js垂直滾動(dòng)條的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

1、紅色盒子高度計(jì)算公式:

容器的高度 / 內(nèi)容的高度 * 容器的高度

2、紅色方塊移動(dòng)一像素 ,我們的內(nèi)容盒子移動(dòng)多少呢?

(內(nèi)容盒子高度 - 大盒子高度) / (大盒子高度 - 紅色盒子的高度) 計(jì)算倍數(shù)

(內(nèi)容盒子高度 -  大盒子高度)/  (大盒子高度 - 紅色盒子的高度)   * 紅色盒子移動(dòng)的數(shù)值

<html>
<head>
  <meta charset="UTF-8">
  <title>垂直滾動(dòng)條</title>
  <style>
  *{
    padding: 0;
    margin: 0;
  }
    .box{
      width: 300px;
      height: 500px;
      border: 1px solid red;
      padding-right: 20px;
      margin: 100px;
      position: relative;
    }
    .content{
       padding: 5px 18px 10px 5px;
      position: absolute;
      left: 0;
      top: -10px;
    }
    .scroll{
      position: absolute;
      top: 0;
      right: 0;
      background-color: #ccc;
      width: 20px;
      height: 100%;
    }
    .bar{
      width: 100%;
      height: 20px;
      background-color: red;
      border-radius: 10px;
      position: absolute;
      left: 0;
      top: 0;
      cursor: pointer;
    }
  </style>  
</head>
<body>
  <div class="box" id="box">
    <div class="content">
      三觀不同,一句話都嫌多。我想,人和人之間一定存在磁場這回事,沿著三觀向外輻射。
   …………
 </div>
    <div class="scroll">
      <div class="bar"></div>
    </div>
  </div>
  <script>  
    var box = document.getElementById('box');
    var content = box.children[0];
    var scroll = box.children[1];
    var bar = scroll.children[0];
    //計(jì)算滾動(dòng)條紅色bar的長度:容器長度/內(nèi)容長度 * 容器長度,,比例關(guān)系
    bar.style.height = box.offsetHeight / content.offsetHeight * box.offsetHeight +"px";
    bar.onmousedown = function(event){
      var event = event || window.event;
      var y = event.clientY - this.offsetTop;
      document.onmousemove = function(event){
        var event = event || window.event;

        var top = event.clientY - y;
        if(top < 0)
          top =0;
        else if(top > scroll.offsetHeight - bar.offsetHeight)
          top = scroll.offsetHeight - bar.offsetHeight;
        bar.style.top = top +"px";
        //(內(nèi)容盒子高度 - 大盒子高度) / (大盒子高度 - 紅色盒子的高度)  * 紅色盒子移動(dòng)的數(shù)值
        content.style.top = -(content.offsetHeight - box.offsetHeight)/(box.offsetHeight - bar.offsetHeight)*top+"px";

        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();  // 防止拖動(dòng)滑塊的時(shí)候, 選中文字
      }
    }
    document.onmouseup = function(){
      document.onmousemove = null;
    }
  </script>
</body>
</html> 

效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論