JavaScript中容易弄混淆的位置屬性 offset client scroll page

鼠標:

1.e.clientX和e.clientY:獲取的鼠標在瀏覽器可視區域相對左上角的距離;
2.e.pageX和e.pageY:獲取的是鼠標在當前頁面的距離(相對於document)javascript

兼容性處理方式:getPage(e).pageX和getPage(e).pageY

3.scrollLeft和scrollTop:獲取頁面的滾動距離html

偏移量

offsetParent用於獲取定位的父級元素

幾個高度的區別:java

  1. offsetHeight、offsetWidth:元素的大小+padding+border
    offsetTop和offsetLeft獲取的盒子的座標
  2. clientHeight、clientWidth:元素的大小+padding
    clientLeft和clientTop:獲取的是border的寬高,不是座標
  3. scrollHeight、scrollWidth:內容的大小(包括隱藏掉的部分)+padding

圖示:
未標題-1.jpg
注意:
scrollWidth不包括滾動條的寬度
scrollTop和scrollLeft是滾動出去的距離web

Tip:把滾動條顯示出來的方法:在盒子CSS樣式添加:overflow:auto;(系統自帶的,不過建議本身寫滾動條:代碼在下面)

案例

製做模擬滾動條

效果圖:
image.png瀏覽器

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {margin: 0;padding: 0;}
        .box {
            width: 300px;
            height: 500px;
            border: 1px solid red;
            margin: 100px;
            position: relative;

            overflow: hidden;

            /* 不讓文字被選中 */
            -webkit-user-select:none;
            -moz-user-select:none;
            -ms-user-select:none;
            user-select:none;
        }
        .content {
            padding: 5px 18px 5px 5px;
            position: absolute;
            top: 0;
            left: 0;

        }
        .scroll {
            width: 18px;
            height: 100%;
            position: absolute;
            top: 0;
            right: 0;
            background-color: #eee;
        }
        .bar {
            height: 100px;
            width: 100%;
            position: absolute;
            top: 0;
            left: 0;
            background-color: red;
            border-radius:  10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <div class="content" id="content">
       我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
       我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,
        我是文字內容,我是文字內容,我是文字內容,

    </div>
    <div class="scroll" id="scroll">
        <div class="bar" id="bar"></div>
    </div>
</div>
<script src="common.js"></script>
<script>
  var box = my$('box');
  var content = my$('content');
  var scroll = my$('scroll');
  var bar = my$('bar');
  //1 根據內容的大小,計算滾動條的高度
  //  滾動條的高度 / scroll的高度 = box的高度 / 內容的高度
  // offsetHeight     元素的大小 + padding + border
  // clientHeight     元素的大小 + padding
  // scrollHeight     內容的大小 + padding
  
  // 當內容的高度大於box的高度,再計算 滾動條的高度,不然的話滾動條的高度爲0
  var barHeight = 0;
  if (content.scrollHeight > box.clientHeight) {
    barHeight = box.clientHeight / content.scrollHeight * scroll.clientHeight;
  }
  bar.style.height = barHeight + 'px';
  
  //2 讓滾動條可以拖拽
  // 2.1 當鼠標按下的時候,求鼠標在滾動條中的位置
  bar.onmousedown = function (e) {
    e = e || window.event;

    // 鼠標在滾動條中的位置
    var y = getPage(e).pageY - bar.offsetTop - box.offsetTop;
    // 2.2 當鼠標在頁面上移動的時候,求滾動條的位置
    document.onmousemove = function (e) {
        //求滾動條的位置
        var barY = getPage(e).pageY - y - box.offsetTop;

        // 控制bar不能移除scroll
        barY = barY < 0 ? 0 : barY;
        barY = barY > scroll.clientHeight - bar.clientHeight ? scroll.clientHeight - bar.clientHeight : barY;

        bar.style.top = barY + 'px';

        //3 當拖拽滾動條的時候,改變內容的位置
        
        // 內容滾動的距離 / 內容最大可以滾動的距離 = 滾動條滾動的距離 / 滾動條最大可以滾動的距離
        
        // 內容最大可以滾動的距離
        var contentMax = content.scrollHeight - box.clientHeight;
        // 滾動條最大可以滾動的距離
        var barMax = scroll.clientHeight - bar.clientHeight;

        var contentY = barY / barMax * contentMax;
        content.style.top = -contentY + 'px';
    }
  }

  document.onmouseup = function () {
    // 移除鼠標移動的事件
    document.onmousemove = null;
  }
  

  
  
</script>
</body>
</html>

common.js代碼:spa

function my$(id) {
  return document.getElementById(id);
}

// 獲取頁面滾動距離的瀏覽器兼容性問題
// 獲取頁面滾動出去的距離
function getScroll() {
  var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
  var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  return {
    scrollLeft: scrollLeft,
    scrollTop: scrollTop
  }
}

// 獲取鼠標在頁面的位置,處理瀏覽器兼容性
function getPage(e) {
  var pageX = e.pageX || e.clientX + getScroll().scrollLeft;
  var pageY = e.pageY || e.clientY + getScroll().scrollTop;
  return {
    pageX: pageX,
    pageY: pageY
  }
}
相關文章
相關標籤/搜索