美文网首页
模拟淘宝放大镜简易效果

模拟淘宝放大镜简易效果

作者: _孙小胖 | 来源:发表于2018-11-02 15:39 被阅读0次
效果预览图 image.png
 <h1>放大镜简易效果</h1>
  <div id="s_box">
    <div class="mark_box"></div>
    <img
      src="https://img.haomeiwen.com/i12912382/514010962a5e5656.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"
      alt />
    <div class="position_box"></div>
  </div>
  <div id="b_box">
    <div id="b_box_all">
      <img
        src="https://img.haomeiwen.com/i12912382/514010962a5e5656.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"
        alt />
    </div>
  </div>

js计算比例

window.onload = function () {
    var oS_box = document.getElementById('s_box');
    var oS_position = oS_box.children[2];
    var oS_mark = oS_box.children[0];
    var oB_box = document.getElementById('b_box');
    var oB_box_all = document.getElementById('b_box_all')
    oS_mark.onmouseover = function () {
      oS_position.style.display = 'block';
      oB_box.style.display = 'block';
    }
    oS_mark.onmouseout = function () {
      oS_position.style.display = 'none';
      oB_box.style.display = 'none';
    }
    oS_mark.onmousemove = function (event) {
      var evt = event || window.event;
      var left = evt.offsetX - oS_position.offsetWidth / 2;
      //console.log(left)
      if (left < 0) {
        left = 0;
      } else if (left > oS_box.offsetWidth - oS_position.offsetWidth) {
        left = oS_box.offsetWidth - oS_position.offsetWidth
      }
      //console.log(left)
      oS_position.style.left = left + 'px';
      var top = evt.offsetY - oS_position.offsetHeight / 2;
      if (top < 0) {
        top = 0;
      } else if (top > oS_box.offsetHeight - oS_position.offsetHeight) {
        top = oS_box.offsetHeight - oS_position.offsetHeight
      }
      //console.log(top)
      oS_position.style.top = top + 'px';
      //移动的比例  把X值和Y值换算成比例;
      var proportionX = left / (oS_box.offsetWidth - oS_position.offsetWidth);
      var proportionY = top / (oS_box.offsetHeight - oS_position.offsetHeight);
      console.log(proportionX + ':' + proportionY)
      //利用比例去算出大小不同的元素的偏移距离;
      oB_box_all.style.left = -proportionX * (oB_box_all.offsetWidth - oB_box.offsetWidth) + 'px';

      oB_box_all.style.top = -proportionY * (oB_box_all.offsetHeight - oB_box.offsetHeight) + 'px';
    }
  }

基础样式设置

  * {
    margin: 0;
    padding: 0;
  }

  img {
    width: 100%;
    height: 100%;
  }

  #s_box {
    width: 400px;
    height: 300px;
    position: relative;
    left: 100px;
    top: 100px;
  }

  #s_box .position_box {
    width: 100px;
    height: 75px;
    background: #b6b6b6;
    opacity: 0.6;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
    display: none;
  }

  #s_box .mark_box {
    width: 400px;
    height: 300px;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 10;
  }

  #b_box {
    width: 400px;
    height: 300px;
    position: relative;
    left: 700px;
    top: -200px;
    display: none;
    border: 5px solid #b6b6b6;
  }

  #b_box_all {
    position: absolute;
    width: 1200px;
    height: 900px;
    left: 0;
    z-index: -10;
  }

相关文章

  • 模拟淘宝放大镜简易效果

    效果预览图 js计算比例 基础样式设置

  • web模拟淘宝评价效果

    最近做的一个项目,正好负责的页面中有商品展示和评价模块,刚开始只是静态页的排版。等静态页面布置好,就开始构思动态的...

  • iOS放大镜效果实现-ASMagnifierManger

    ASMagnifierManger 放大镜效果。可更改放大倍数和放大镜大小 特点介绍 放大镜效果 放大镜形状自定义...

  • 用js实现图片放大镜效果

    很多电商网站比如:京东、天猫、淘宝都有放大镜效果,每当看到心仪的物品时,鼠标移入图片时,便呈现出放大的效果。在没有...

  • 刮刮卡效果——能否刮出一个未来

    前端入坑纪 63 今天来分享一个最近用 canvas 模拟的简易刮刮卡效果 好,详解如下! OK,first th...

  • 「PPT疗程」 放大镜效果动画

    一个简单到哭的动画小技巧 今天的PPT疗程分享一个小技巧,模拟放大镜的移动效果 这个效果我只用了一个动画,你没听错...

  • 淘宝放大镜

    {padding: 0;margin: 0;}li{list-style...

  • 爬虫框架Scrapy之模拟登录淘宝

    模拟登录淘宝

  • 放大镜效果

    写在最前面:一定要注意设置样式的时候大图片和小图片的width,height是有比例关系的;废话不多说,直接放效果...

  • 放大镜效果

    基本原理,左边有一个盛放图片的容器,里面有哥哥小的遮罩用来提示把那一部分展示到右边的大的展示放大之后的图片的部分左...

网友评论

      本文标题:模拟淘宝放大镜简易效果

      本文链接:https://www.haomeiwen.com/subject/odrcxqtx.html