资讯专栏INFORMATION COLUMN

展示js中实现鼠标移入图片放大效果示例

3403771864 / 467人阅读

  本篇文章主要讲述的就是JS编写一个鼠标移入图片放大效果,不多说废话,大家一起看看具体内容:

  目标

  给图片添加鼠标移动放大方法效果,移到哪里放大哪里

  先看看效果是不是你想要的,再看代码

  移入前

  移入后


  html

  <!-- css看着写 -->
  <div class="Box" style="width:200px;height:200px;border:1px solid #f00;position: relative;top:0;left:0;overflow: hidden;">
  <Img src="../image/lingtai.jpg" alt="" style="width:200px;height:200px;position:absolute;left:0;top:0;">
  </div>

  javascript

  // 图片放大镜
  // @params Class 目标class string
  // @params Scale 放大倍数 number
  function ScaleImg(Class, Scale){
  this.Box = document.querySelector(Class);
  this.Img = this.Box.querySelector('img');
  this.scale = Scale || 3 ;
  // 盒子中心点
  this.BoxX = this.Box.offsetWidth / 2;
  this.BoxY = this.Box.offsetHeight / 2;
  // 获取盒子初始定位
  this.Left = parseFloat( this.Box.offsetLeft );
  this.Top = parseFloat(this.Box.offsetTop );
  this.Init();
  }
  ScaleImg.prototype = {
  // 鼠标移入
  Mouseover: function(e){
  var e = e || window.event;
  // 放大图片
  this.Img.style.width = this.Img.offsetWidth * this.scale + "px";
  this.Img.style.height = this.Img.offsetHeight * this.scale + "px";
  // 设置放大后的图片定位
  this.Img.style.left = (this.Box.offsetWidth - this.Img.offsetWidth) / 2 + "px";
  this.Img.style.top = (this.Box.offsetHeight - this.Img.offsetHeight) / 2 + "px";
  // 获取图片放大后定位值
  this.ImgLeft = parseFloat(this.Img.style.left);
  this.ImgTop = parseFloat(this.Img.style.top);
  this.Box.left = (this.Box.offsetWidth - this.Img.offsetWidth) / 2;
  this.Box.top = (this.Box.offsetHeight - this.Img.offsetHeight) / 2;
  // 阻止默认事件
  return ;
  },
  // 鼠标移除
  Mouseout: function(e){
  var e = e || window.event;
  // 重置css
  this.Img.style.width = this.Img.offsetWidth / this.scale + "px";
  this.Img.style.height =this.Img.offsetHeight / this.scale + "px";
  this.Img.style.left = Math.floor((this.Box.offsetWidth - this.Img.offsetWidth) / 2) + "px";
  this.Img.style.top = Math.floor((this.Box.offsetHeight - this.Img.offsetHeight) / 2) + "px";
  return ;
  },
  // 鼠标移动
  Mousemove: function(e){
  var e = e || window.event;
  // 图片鼠标位置
  var ImgXY = {"x": this.Left + this.BoxX, "y": this.Top + this.BoxY};
  // 获取偏移量
  var left = (ImgXY.x - e.clientX ) / this.BoxX * this.ImgLeft ,
  top = (ImgXY.y - e.clientY) / this.BoxY * this.ImgTop ;
  this.Img.style.left = Math.floor(this.Box.left - left) + "px";
  this.Img.style.top = Math.floor(this.Box.top - top) + "px";
  return ;
  },
  // 初始化
  Init: function(e){
  var that = this;
  this.Box.onmouseover = function(e){
  that.Mouseover(e);
  }
  this.Box.onmouseout = function(e){
  that.Mouseout(e);
  }
  this.Box.onmousemove = function(e){
  that.Mousemove(e);
  }
  }
  }
  // 实例一个对象
  new ScaleImg('.Box');

  全部内容已讲述完毕,欢迎大家多多学习。


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128140.html

相关文章

  • js放大效果

    效果如淘宝、京东这些电商购物时,查看图片的放大效果。 思路: 先把右边的大图和左边小图里面的方块隐藏 当鼠标移入左边的smallPic,显示其里面的小方块和右边的bigPic 当鼠标移动里面的小方块,右边的bigPic显示图片对应的位置 小方块移动的范围在其父级smallPic的范围内 当鼠标smallPic后,bigPic和smallPic里面的小方块隐藏 用到的方法主要由定位、溢出隐藏、事件...

    Joonas 评论0 收藏0
  • js放大效果

    效果如淘宝、京东这些电商购物时,查看图片的放大效果。 思路: 先把右边的大图和左边小图里面的方块隐藏 当鼠标移入左边的smallPic,显示其里面的小方块和右边的bigPic 当鼠标移动里面的小方块,右边的bigPic显示图片对应的位置 小方块移动的范围在其父级smallPic的范围内 当鼠标smallPic后,bigPic和smallPic里面的小方块隐藏 用到的方法主要由定位、溢出隐藏、事件...

    赵连江 评论0 收藏0
  • js放大效果

    效果如淘宝、京东这些电商购物时,查看图片的放大效果。 思路: 先把右边的大图和左边小图里面的方块隐藏 当鼠标移入左边的smallPic,显示其里面的小方块和右边的bigPic 当鼠标移动里面的小方块,右边的bigPic显示图片对应的位置 小方块移动的范围在其父级smallPic的范围内 当鼠标smallPic后,bigPic和smallPic里面的小方块隐藏 用到的方法主要由定位、溢出隐藏、事件...

    DDreach 评论0 收藏0
  • 使用原生js轮播图效果

    摘要:知乎原文我的博客微信公众号这几天在逛网站的时候,发现很多网站都有轮播图这个效果,所以我就仿照小米的官网用原生写了一个轮播图效果,希望大家喜欢。 知乎原文 我的博客 微信公众号这几天在逛网站的时候,发现很多网站都有轮播图这个效果,所以我就仿照小米的官网用原生js写了一个轮播图效果,希望大家喜欢。这是我发布在github上的最后实现的效果:https://heternally.gith...

    happen 评论0 收藏0
  • 使用原生js轮播图效果

    摘要:知乎原文我的博客微信公众号这几天在逛网站的时候,发现很多网站都有轮播图这个效果,所以我就仿照小米的官网用原生写了一个轮播图效果,希望大家喜欢。 知乎原文 我的博客 微信公众号这几天在逛网站的时候,发现很多网站都有轮播图这个效果,所以我就仿照小米的官网用原生js写了一个轮播图效果,希望大家喜欢。这是我发布在github上的最后实现的效果:https://heternally.gith...

    Anshiii 评论0 收藏0

发表评论

0条评论

3403771864

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<