JavaScript学习笔记16

  • 目录

     拖动模态框案例

    触发事件是鼠标按下mousedown,鼠标移动:mousemove,鼠标松开:mouseup

    京东放大镜效果显示隐藏遮挡层和大盒子案例

    鼠标经过小图片盒子,黄色的遮挡层和大图片盒子显示,离开隐藏2个盒子功能

    当页面加载完毕再执行里面的JS代码:window.addEventListener('load' ,function(){})

    引入外部JS文件:

    鼠标经过:mouseover;鼠标离开:mouseout

    黄色的遮挡层跟随鼠标功能

    移动黄色遮挡层,大图片跟随移动功能

    大图片移动距离=遮挡层移动距离*大图片最大移动距离/遮挡层的最大移动距离

    遮挡层最大移动距离:var maskMax = preview-img.offsetWidth - mask.offsetWidth

    大图片的移动距离 :var bigImg = document.querySelect('.bigImg');  var bigImg = bigImg.offsetWidth - big.offsetWidth;

    client获取元素可视区相关信息

    淘宝flexible分析JS

    立即执行函数

    (function( ){ })( );

    (function( ){ }( ));

    devicePixelRatio物理像素比

    setBodyFontSize设置body字体大小

    DOMContentLoaded页面主要的DOM元素加载完毕再去设置字体大小   

    元素scroll系列属性

    element.scrollTop返回被卷上去的上侧

    element.scrollWidth包含padding但是不包含border

    仿淘宝侧边栏案例


  •  拖动模态框案例

    • 点击弹出层模态框和遮挡层就会显现出来display:block;

    • 点击关闭按钮,模态框和遮盖层就会隐藏起来display:none;

    • 在页面中拖拽的原理:鼠标按下并且移动,之后松开鼠标

    • 触发事件是鼠标按下mousedown,鼠标移动:mousemove,鼠标松开:mouseup

    • 拖拽过程:鼠标移动过程中,获得最新的赋值模态框的left和top值,这样模态框就可以跟着鼠标走了

    • 鼠标按下触发的事件源是最上面一行,就是id为title

    • 鼠标的坐标减去鼠标在盒子内的坐标,才是模态框的真正位置

    • 鼠标按下,我们要得到鼠标在盒子的坐标

    • 鼠标移动,就让模态框的坐标设置为:鼠标坐标减去盒子坐标即可,注意移动事件写到按下事件里面。

    • 鼠标松开,就停止拖拽,就是可以让鼠标移动事件解除

    • 案例

      <!DOCTYPE html>
      <html lang="en">
      
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Document</title>
          <style>
              * {
                  padding: 0;
                  margin: 0;
              }
      
              .btn {
                  display: block;
                  font-size: 20px;
                  margin: 20px auto;
                  padding: 10px 20px;
                  border: 0;
              }
      
              .login {
                  position: fixed;
                  top: 50%;
                  left: 50%;
                  display: none;
                  width: 500px;
                  text-align: center;
                  border: 1px solid skyblue;
                  background-color: #fff;
                  transform: translate(-50%, -50%);
              }
      
              h3 {
                  height: 50px;
                  line-height: 50px;
                  background-color: skyblue;
              }
      
              table {
                  width: 100%;
                  height: 50px;
                  line-height: 50px;
                  border-collapse: collapse;
                  border-spacing: 0;
                  text-align: left;
                  margin-top: 30px;
              }
      
              table tr td:nth-child(1) {
                  width: 100px;
                  text-align: right;
              }
      
              tr {
                  height: 70px;
              }
      
              input {
                  margin-left: 10px;
                  outline: none;
                  width: 300px;
                  height: 30px;
              }
      
              .button {
                  width: 200px;
                  height: 50px;
                  line-height: 50px;
                  background-color: skyblue;
                  margin: 30px auto;
                  border: 0;
                  font-size: 20px;
              }
      
              span {
                  position: absolute;
                  right: 0;
                  top: 0;
                  display: inline-block;
                  width: 30px;
                  height: 30px;
                  font-size: 25px;
                  cursor: pointer;
              }
          </style>
      </head>
      
      <body>
          <button class="btn">点击弹出登录框</button>
          <div class="login">
              <h3 class="title">登录界面</h3>
              <table>
                  <tr>
                      <td>用户名</td>
                      <td><input type="text"></td>
                  </tr>
                  <tr>
                      <td>密码</td>
                      <td><input type="password"></td>
                  </tr>
              </table>
              <button class="button">登录</button>
              <span>×</span>
          </div>
          <script>
              //  点击 btn 按钮,显示登录界面,同时body改变背景颜色
              // 鼠标拖动 登录框 h3(标题区) 登录框随鼠标移动
              // 1.获取元素
              var btn = document.querySelector('.btn');
              var login = document.querySelector('.login');
              var title = document.querySelector('.title');
              var span = document.querySelector('span');
              // 2.绑定事件
              // 点击 btn 显示登录框,改变body背景颜色
              btn.addEventListener('click', function (e) {
                  login.style.display = 'block';
                  document.body.style.backgroundColor = '#ccc';
              });
              // 点击 span(关闭) 隐藏登录框 恢复body背景颜色
              span.addEventListener('click', function (e) {
                  login.style.display = 'none';
                  document.body.style.backgroundColor = '#fff';
              });
      
              // 鼠标点击拖动 h3(标题区) ..登录框随着鼠标走
              title.addEventListener('mousedown', function (e) {
                  // e.pageX e.pageY   鼠标所在页面的位置
                  // login.offsetLeft login.offsetTop 登录框离页面的左距离/上距离
                  var loginLeft = e.pageX - login.offsetLeft; // 鼠标离登录框左距离
                  var loginTop = e.pageY - login.offsetTop; // 鼠标离登录框左距离
                  document.addEventListener('mousemove', move);
      
                  function move(e) {
                      login.style.left = e.pageX - loginLeft + 'px';
                      login.style.top = e.pageY - loginTop + 'px';
                  }
                  document.addEventListener('mouseup', function (e) {
                      document.removeEventListener('mousemove', move);
                  });
              });
          </script>
      </body>
      
      </html>

    • 结果

  • 京东放大镜效果显示隐藏遮挡层和大盒子案例

    • 整个案例可以分为三个功能板块

    • 鼠标经过小图片盒子,黄色的遮挡层和大图片盒子显示,离开隐藏2个盒子功能

      • 当页面加载完毕再执行里面的JS代码:window.addEventListener('load' ,function(){})

      • 引入外部JS文件:<script src="js/detail.js"></script>

      • 鼠标经过:mouseover;鼠标离开:mouseout

    • 黄色的遮挡层跟随鼠标功能

      • 黄色遮挡层跟随鼠标功能

      • 把鼠标在盒子内的坐标给黄色遮挡层

      • 用到鼠标移动事件,将获得鼠标在盒子的坐标,之后吧数值给遮挡层作为left和top值

    • 移动黄色遮挡层,大图片跟随移动功能

      • 大图片移动距离=遮挡层移动距离*大图片最大移动距离/遮挡层的最大移动距离

      • 遮挡层最大移动距离:var maskMax = preview-img.offsetWidth - mask.offsetWidth

      • 大图片的移动距离 :var bigImg = document.querySelect('.bigImg');  var bigImg = bigImg.offsetWidth - big.offsetWidth;

      • 案例

        <!DOCTYPE html>
        <html lang="en">
        
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <title>Document</title>
            <style>
                * {
                    margin: 0;
                    padding: 0;
                }
        
                .box {
                    position: relative;
                    top: 50px;
                    left: 50px;
                    width: 300px;
                    height: 300px;
                    border: 1px solid #ccc;
                }
        
                .small {
                    width: 300px;
                }
        
                .small_box {
                    display: none;
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 200px;
                    height: 200px;
                    background-color: yellow;
                    opacity: .3;
                }
        
                .big_box {
                    display: none;
                    position: absolute;
                    top: 0;
                    left: 330px;
                    width: 500px;
                    height: 500px;
                    border: 1px solid #ccc;
                    overflow: hidden;
                }
        
                .bigImg {
                    position: absolute;
                    top: 0;
                    left: 0;
                }
            </style>
        </head>
        
        <body>
            <div class="box">
                <img src="images/maomi.jpg" alt="" class="small">
                <div class="small_box"></div>
                <div class="big_box">
                    <img src="images/maomi.jpg" alt="" class="bigImg">
                </div>
            </div>
            <script>
                // 鼠标移动到 box 盒子时..显示 small_box盒子 和 big_box 盒子,鼠标移出则隐藏
                // small_box盒子 跟随鼠标移动而移动,同时 big_box 里面的大图片,显示对应图片区域
                // 1.获取元素
                var box = document.querySelector('.box');
                var small_box = document.querySelector('.small_box');
                var big_box = document.querySelector('.big_box');
                var bigImg = document.querySelector('.bigImg');
                // 2.绑定事件
                // 3.鼠标经过 box盒子..显示 small_box盒子 和 big_box盒子
                box.addEventListener('mousemove', function (e) {
                    small_box.style.display = 'block';
                    big_box.style.display = 'block';
                });
                // 4.鼠标离开 box盒子..隐藏 small_box盒子 和 big_box盒子
                box.addEventListener('mouseout', function (e) {
                    small_box.style.display = 'none';
                    big_box.style.display = 'none';
                });
                // 5.让 small_box盒子 跟随鼠标移动
                box.addEventListener('mousemove', function (e) {
                    // e.pageX  e.pageY  鼠标在页面的位置
                    var x = e.pageX - box.offsetLeft; // 鼠标离 box盒子 左边的距离
                    var y = e.pageY - box.offsetTop; // 鼠标离 box盒子 顶部的距离
                    // 鼠标在 small_box盒子 内居中
                    var small_box_x = (x - small_box.offsetWidth / 2);
                    var small_box_y = (y - small_box.offsetHeight / 2);
                    // small_box盒子 最大移动距离
                    var small_box_max = box.offsetWidth - small_box.offsetWidth;//小盒子最大不能超过=盒子宽-小盒子宽
                    // 限制 small_box盒子 的移动范围 限制小盒子在盒子内部不超出盒子
                    if (small_box_x <= 0) {//当小盒子坐标小于0,就赋值为0
                        small_box_x = 0;
                    } else if (small_box_x >= small_box_max) {//当小盒子坐标大于最大移动距离,就赋值最大值
                        small_box_x = small_box_max;
                    }
                    if (small_box_y <= 0) {
                        small_box_y = 0;
                    } else if (small_box_y >= small_box_max) {
                        small_box_y = small_box_max;
                    }
                    small_box.style.left = small_box_x + 'px';
                    small_box.style.top = small_box_y + 'px';
        
                    // 6.大盒子图片跟随小黄盒子所在图片位置的移动而移动,按照比例移动
                    // 大图片的移动距离 = 遮挡层移动距离*大图片最大移动距离 / 遮挡层的最大移动距离
                    // 大图片最大移动距离
                    var bigImgMax = bigImg.offsetWidth - big_box.offsetWidth;
                    // 大图片移动距离
                    var bigImgX = small_box_x * bigImgMax / small_box_max;
                    var bigImgY = small_box_y * bigImgMax / small_box_max;
                    bigImg.style.left = -bigImgX + 'px';
                    bigImg.style.top = -bigImgY + 'px';
                });
            </script>
        </body>
        
        </html>

      • 结果

         

  • client获取元素可视区相关信息

    • 不包含边框但是包含padding

  • 淘宝flexible分析JS

    • 立即执行函数

      • 独立创建了一个作用域,里面所有的变量都是局部变量,不会有命名冲突的情况

      • 不需要调用,立马可以执行的函数

      • 常见写法:

        • (function( ){ })( );

        • (function( ){ }( ));

    • devicePixelRatio物理像素比

    • setBodyFontSize设置body字体大小

      • DOMContentLoaded页面主要的DOM元素加载完毕再去设置字体大小   

    • 元素scroll系列属性

      • element.scrollTop返回被卷上去的上侧

      • element.scrollWidth包含padding但是不包含border

    • 仿淘宝侧边栏案例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值