【前端系列教程之JavaScript】12_offset、client、scroll三大家

offset、client、scroll三大家

偏移量

  • offsetParent用于获取定位的父级元素

  • offsetParent和parentNode的区别

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }

    #box {
      /* position: relative; */
      width: 300px;
      height: 300px;
      background-color: red;
      /* 解决顶部外边距问题 */
      overflow: hidden;
      margin: 50px;
    }

    #child {
      width: 100px;
      height: 100px;
      background-color: blue;
      margin: 50px;
      border: 10px solid yellow;
      padding: 10px;
    }
  </style>
</head>

<body>
  <div id="box">
    <div id="child">

    </div>
  </div>

  <script>
    // 3组和大小 位置相关的属性
    // offset  client  scroll
    // 
    // offset  偏移量
    var box = document.getElementById('box');
    // 获取box的坐标
    console.log(box.offsetLeft);
    console.log(box.offsetTop);
    // 获取box的大小
    console.log(box.offsetWidth);
    console.log(box.offsetHeight);

    console.log("----------------")

    // offsetParent   获取距离当前元素最近的定位父元素,如果没有定位父元素此时是body

    // 获取子元素的位置和大小
    var child = document.getElementById('child');
    console.log(child.offsetParent);
    // 获取child的位置     offsetLeft 距离offsetParent的横向偏移
    console.log(child.offsetLeft);
    console.log(child.offsetTop);

    // 获取child的大小   包括边框和padding
    console.log(child.offsetWidth);
    console.log(child.offsetHeight);
  </script>
</body>

</html>

客户区大小

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }

    #box {
      width: 100px;
      height: 100px;
      margin: 50px;
      border: 30px solid red;
      padding: 10px;
      background-color: green;
    }
  </style>
</head>

<body>
  <div id="box">

  </div>
  <script>
    // client
    var box = document.getElementById('box');
    // clientLeft  是border-left 的宽度
    // clientTop    border-top 的宽度
    console.log(box.clientLeft);
    console.log(box.clientTop);


    // 获取大小   包括padding  但是不包括边框
    console.log(box.clientWidth);
    console.log(box.clientHeight);

    // offsetWidth   offsetHeight     包括padding和边框
  </script>
</body>
</html>

滚动偏移

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }

    #box {
      width: 100px;
      height: 100px;
      margin: 50px;
      border: 30px solid red;
      padding: 10px;
      background-color: green;
      overflow: auto;
    }
  </style>
</head>

<body>
  <div id="box">
    超文本是一种组织信息的方式,它通过超级链接方法将文本中的文字、图表与其他信息媒体相关联。这些相互关联的信息媒体可能在同一文本中,也可能是其他文件,或是地理位置相距遥远的某台计算机上的文件。
  </div>
  <script>
    // scroll
    var box = document.getElementById('box');
    // 当拖动box中的滚动条的时候触发
    box.onscroll = function () {
      console.log(box.scrollLeft);
      console.log(box.scrollTop);

    }


    // // box滚动出去的距离
    // console.log(box.scrollLeft);
    // console.log(box.scrollTop);

    // // 内容的大小,包括padding 和未显示的内容,不包括滚动条
    // console.log(box.scrollWidth);
    // console.log(box.scrollHeight);

    // // 元素的大小 + padding   不包括滚动条
    // console.log(box.clientWidth);
    // console.log(box.clientHeight);
  </script>
</body>

</html>

案例

div匀速移动

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }
    #box {
      position: relative;
      background-color: red;
      width: 100px;
      height: 100px;
    }
  </style>
</head>
<body>
  <input type="button" value="开始" id="btn">
  <div id="box"></div>
  <script>
    var btn = document.getElementById('btn');
    var box = document.getElementById('box');
    btn.onclick = function () {
      var step = 6; 
      var target = 500;
      var timerId = setInterval(function () {
        if (box.offsetLeft >= target) {  
          clearInterval(timerId);
          box.style.left = target + 'px';
          return;
        }
        box.style.left = box.offsetLeft + step + 'px';
      }, 30);
    }
  </script>
</body>
</html>

获取鼠标在div内的坐标

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }

    #box {
      width: 300px;
      height: 300px;
      border: 1px solid red;
      margin: 100px 10px 10px 100px;
    }
  </style>
</head>
<body>
  <div id="box">
    
  </div>
  <script src="common.js"></script>
  <script>
    var box = document.getElementById('box');
    box.onclick = function (e) {
      // 获取盒子在页面上的位置
      // console.log(this.offsetLeft);
      // console.log(this.offsetTop);

      e = e || window.event;
      // 获取鼠标在盒子中的位置 = 鼠标的坐标 - 盒子的坐标
      // var x = e.pageX
      var x = getPage(e).pageX - this.offsetLeft;
      var y = getPage(e).pageY - this.offsetTop;
      console.log(x);
      console.log(y);

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

拖拽案例

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .nav {
      height: 30px;
      background: #87cefa;
      line-height: 30px;
      padding-left: 30px;
    }

    .nav a {
      color: #fff;
      text-align: center;
      font-size: 14px;
      text-decoration: none;

    }

    .d-box {
      width: 400px;
      height: 300px;
      border: 5px solid #eee;
      box-shadow: 2px 2px 2px 2px #666;
      position: absolute;
      top: 50%;
      left: 50%;
      background-color: white;
      /* 不让文字被选中 */
      user-select: none;
      transform: translate(-50%, -50%);
    }

    .hd {
      width: 100%;
      height: 35px;
      background-color: #87cefa;
      border-bottom: 1px solid #369;
      line-height: 35px;
      color: white;
      cursor: move;
    }

    #box_close {
      float: right;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="nav">
    <a href="javascript:;" id="register">注册信息</a>
  </div>
  <div class="d-box" id="d_box">
    <div class="hd" id="drop">注册信息 (可以拖拽)
      <span id="box_close">【关闭】</span>
    </div>
    <div class="bd"></div>
  </div>

  <script>
    // 获取盒子
    var box = document.getElementById('d_box');
    // 获取盒子标题区块
    var drop = document.getElementById('drop');

    drop.onmousedown = function (e) {
      // 兼容性处理
      var e = e || window.event;

      // 当鼠标在盒子标题区域按下的时候,获取鼠标在盒子里面的下标
      // 鼠标在盒子的下标 = 鼠标在页面的的下标 - 盒子的偏移量;
      var x = e.pageX - box.offsetLeft;
      var y = e.pageY - box.offsetTop;

      console.log(x + " " + y);

      document.onmousemove = function (e) {
        // 兼容性处理
        var e = e || window.event;
        // 当鼠标在页面上移动的时候。求盒子的坐标
        // 盒子的坐标 = 鼠标当前在页面中的位置 - 鼠标在盒子中的位置
        var boxX = e.pageX - x;
        var boxY = e.pageY - y;
        box.style.left = boxX + "px";
        box.style.top = boxY + "px";

      }
    }

    // 当鼠标在页面文档中弹起,移除移动跟随
    document.onmouseup = function () {
      document.onmousemove = null;
    }

    // 点击关闭按钮,隐藏盒子
    var box_close = document.getElementById('box_close');
    box_close.onclick = function () {
      box.style.display = 'none';
    }
  </script>
</body>

</html>

弹出登录窗口

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    .login-header {
      width: 100%;
      text-align: center;
      height: 30px;
      font-size: 24px;
      line-height: 30px;
    }

    ul,li,ol,dl,dt,dd,div,p,span,h1,h2,h3,h4,h5,h6,a {
      padding: 0px;
      margin: 0px;
    }

    .login {
      width: 512px;
      height: 280px;
      position: absolute;
      border: #ebebeb solid 1px;
      left: 50%;
      top: 50%;
      background: #ffffff;
      box-shadow: 0px 0px 20px #ddd;
      z-index: 9999;
      display: none;
      transform: translate(-50%, -50%);
    }

    .login-title {
      width: 100%;
      margin: 10px 0px 0px 0px;
      text-align: center;
      line-height: 40px;
      height: 40px;
      font-size: 18px;
      position: relative;
      cursor: move;
      -moz-user-select: none;
      /*火狐*/
      -webkit-user-select: none;
      /*webkit浏览器*/
      -ms-user-select: none;
      /*IE10*/
      -khtml-user-select: none;
      /*早期浏览器*/
      user-select: none;
    }

    .login-input-content {
      margin-top: 20px;
    }

    .login-button {
      width: 50%;
      margin: 30px auto 0px auto;
      line-height: 40px;
      font-size: 14px;
      border: #ebebeb 1px solid;
      text-align: center;
    }

    .login-bg {
      width: 100%;
      height: 100%;
      position: fixed;
      top: 0px;
      left: 0px;
      background: #000000;
      filter: alpha(opacity=30);
      -moz-opacity: 0.3;
      -khtml-opacity: 0.3;
      opacity: 0.3;
      display: none;
    }

    a {
      text-decoration: none;
      color: #000000;
    }

    .login-button a {
      display: block;
    }

    .login-input input.list-input {
      float: left;
      line-height: 35px;
      height: 35px;
      width: 350px;
      border: #ebebeb 1px solid;
      text-indent: 5px;
    }

    .login-input {
      overflow: hidden;
      margin: 0px 0px 20px 0px;
    }

    .login-input label {
      float: left;
      width: 90px;
      padding-right: 10px;
      text-align: right;
      line-height: 35px;
      height: 35px;
      font-size: 14px;
    }

    .login-title span {
      position: absolute;
      font-size: 12px;
      right: -20px;
      top: -30px;
      background: #ffffff;
      border: #ebebeb solid 1px;
      width: 40px;
      height: 40px;
      border-radius: 20px;
    }
  </style>
</head>

<body>
  <div class="login-header"><a id="link" href="javascript:void(0);">点击,弹出登录框</a></div>
  <div id="login" class="login">
    <div id="title" class="login-title">登录会员
      <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
    </div>
    <div class="login-input-content">
      <div class="login-input">
        <label>用户名:</label>
        <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
      </div>
      <div class="login-input">
        <label>登录密码:</label>
        <input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
      </div>
    </div>
    <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
  </div>
  <!-- 遮盖层 -->
  <div id="bg" class="login-bg"></div>
  <script>
    // 显示登录框和遮盖层
    var login = document.getElementById('login');
    var bg = document.getElementById('bg');
    //1 点击按钮,弹出登录框和遮盖层
    var link = document.getElementById('link');
    link.onclick = function () {
      login.style.display = 'block';
      bg.style.display = 'block';
      return false;
    }

    // 2 点击关闭按钮,隐藏 登录框和遮盖层
    var closeBtn = document.getElementById('closeBtn');
    closeBtn.onclick = function () {
      // 隐藏 登录框和遮盖层
      login.style.display = 'none';
      bg.style.display = 'none';
    }

    // 3 拖拽
    var title = document.getElementById('title');
    title.onmousedown = function (e) {
      // 鼠标按下,求鼠标在盒子中的位置
      var x = e.pageX - login.offsetLeft;
      var y = e.pageY - login.offsetTop;

      document.onmousemove = function (e) {
        // 鼠标移动的时候, 盒子的坐标
        var loginX = e.pageX - x;
        var loginY = e.pageY - y;

        login.style.left = loginX + 'px';
        login.style.top = loginY + 'px';
      }
    }

    document.onmouseup = function () {
      // 移除鼠标移动的事件
      document.onmousemove = null;
    }
  </script>
</body>

</html>

放大镜案例

        1、鼠标经过box的时候 显示 mask和bigBox , 当鼠标离开box的时候隐藏mask和bigBox

        2、当鼠标在盒子中移动的时候,让mask和鼠标一起移动

        3、当mask移动的时候,让大图片移动

mask移动的距离 / mask最大能够移动的距离 = 大图片移动的距离 / 大图片最大能够移动的距离

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      width: 350px;
      height: 350px;
      margin: 100px;
      border: 1px solid #ccc;
      position: relative;
    }

    .big {
      width: 400px;
      height: 400px;
      position: absolute;
      top: 0;
      left: 360px;
      border: 1px solid #ccc;
      overflow: hidden;
      display: none;
    }

    .big img {
      position: absolute;
    }

    .mask {
      width: 175px;
      height: 175px;
      background: rgba(255, 255, 0, 0.4);
      position: absolute;
      top: 0px;
      left: 0px;
      cursor: move;
      display: none;
    }

    .small {
      position: relative;
    }
  </style>
</head>

<body>
  <div class="box" id="box">
    <div class="small">
      <img src="images/small.jpg" width="350" alt="" />
      <div class="mask"></div>
    </div>
    <div class="big">
      <img src="images/big.jpg" width="800" alt="" />
    </div>
  </div>
  <script>
    // 获取box、small盒子、big盒子
    var box = document.getElementById("box");
    var smallBox = box.children[0];
    var bigBox = box.children[1];

    // 获取小图片
    var smallImg = smallBox.children[0];
    // 获取遮罩
    var mask = smallBox.children[1];
    // 获取大图片
    var bigImg = bigBox.children[0];

    // 1、 鼠标经过box的时候 显示 mask和bigBox, 当鼠标离开box的时候隐藏mask和bigBox
    box.onmouseover = function () {
      mask.style.display = "block";
      bigBox.style.display = "block";
    }

    box.onmouseout = function () {
      mask.style.display = "none";
      bigBox.style.display = "none";
    }

    // 2、 当鼠标在盒子中移动的时候, 让mask和鼠标一起移动
    box.onmousemove = function (e) {
      // 获取鼠标在盒子里面的坐标,也就是mask的坐标
      var maskX = e.pageX - box.offsetLeft;
      var maskY = e.pageY - box.offsetTop;

      // 让鼠标位于盒子中间
      maskX = maskX - mask.offsetWidth / 2;
      maskY = maskY - mask.offsetHeight / 2;

      // 限制mask的活动范围
      maskX = maskX < 0 ? 0 : maskX;
      maskY = maskY < 0 ? 0 : maskY;

      maskX = maskX > box.offsetWidth - mask.offsetWidth ? box.offsetWidth - mask.offsetWidth : maskX;
      maskY = maskY > box.offsetHeight - mask.offsetHeight ? box.offsetHeight - mask.offsetHeight : maskY;

      // 把坐标设置给mask
      mask.style.left = maskX + "px";
      mask.style.top = maskY + "px";

      // 3、 当mask移动的时候, 让大图片移动
      //  mask移动的距离 / mask最大能够移动的距离 = 大图片移动的距离 / 大图片最大能够移动的距离
      // mask最大能够移动距离
      var maskMax = box.offsetWidth - mask.offsetWidth;
      var bingImgMax = bigImg.offsetWidth - bigBox.offsetWidth;

      // 计算得出大图片移动距离
      var bigImgX = maskX * bingImgMax / maskMax;
      var bigImgY = maskY * bingImgMax / maskMax;

      bigImg.style.left = -bigImgX + "px";
      bigImg.style.top = -bigImgY + "px";

    }
  </script>
</body>

</html>

回到顶部

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
        }

        header {
            width: 1024px;
            height: 150px;
            background: red;
            margin: auto;
        }

        section {
            width: 1024px;
            height: 550px;
            background: yellow;
            margin: 15px auto;

        }

        footer {
            width: 1024px;
            height: 150px;
            background: red;
            margin: auto;
        }

        aside {
            position: fixed;
            right: 0;
            bottom: 100px;
        }
    </style>
</head>

<body>
    <header></header>
    <section></section>
    <section></section>
    <section></section>
    <footer></footer>
    <aside>
        <a href="javascript:void(0)" id="goTop">回到顶部</a>
    </aside>

    <script src="comm.js"></script>
    <script>
        // 无论多远,300ms都要回到顶部
        var goTop = document.getElementById("goTop");

        goTop.onclick = function () {
            // 获取页面滚动出去的距离
            var y = getScroll().scrollTop;

            // 分30次滚动完毕,每次滚动多少距离
            var step = y / 10;

            var interId = setInterval(function () {
                if (getScroll().scrollTop <= 0) {
                    clearInterval(interId);
                    return;
                }
                // x 和 y 每次滚动的距离
                scrollBy(0, -step);
            }, 10)
        }
    </script>
</body>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是波哩个波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值