[前端] js实现鼠标移入图片放大效果

2053 0
黑夜隐士 2022-10-21 16:00:19 | 显示全部楼层 |阅读模式
使用原生js编写一个鼠标移入图片放大效果,供大家参考,具体内容如下
目标

给图片添加鼠标移动放大方法效果,移到哪里放大哪里
先看看效果是不是你想要的,再看代码
移入前


移入后


html
  1. <!-- css看着写 -->
  2. <div class="Box" style="width:200px;height:200px;border:1px solid #f00;position: relative;top:0;left:0;overflow: hidden;">
  3.   <Img src="../image/lingtai.jpg" alt="" style="width:200px;height:200px;position:absolute;left:0;top:0;">
  4. </div>
复制代码
javascript
  1. // 图片放大镜
  2. // @params Class 目标class string
  3. // @params Scale 放大倍数 number
  4. function ScaleImg(Class, Scale){
  5.   this.Box = document.querySelector(Class);
  6.   this.Img = this.Box.querySelector('img');
  7.   this.scale = Scale || 3 ;
  8.   // 盒子中心点
  9.   this.BoxX = this.Box.offsetWidth / 2;
  10.   this.BoxY = this.Box.offsetHeight / 2;
  11.   // 获取盒子初始定位
  12.   this.Left = parseFloat( this.Box.offsetLeft );
  13.   this.Top = parseFloat(this.Box.offsetTop );
  14.   this.Init();
  15. }
  16. ScaleImg.prototype = {
  17.   // 鼠标移入
  18.   Mouseover: function(e){
  19.    var e = e || window.event;
  20.    / 放大图片
  21.    this.Img.style.width = this.Img.offsetWidth * this.scale + "px";
  22.    this.Img.style.height = this.Img.offsetHeight * this.scale + "px";
  23.    // 设置放大后的图片定位
  24.    this.Img.style.left = (this.Box.offsetWidth - this.Img.offsetWidth) / 2 + "px";
  25.    this.Img.style.top = (this.Box.offsetHeight - this.Img.offsetHeight) / 2 + "px";
  26.    // 获取图片放大后定位值
  27.    this.ImgLeft = parseFloat(this.Img.style.left);
  28.    this.ImgTop = parseFloat(this.Img.style.top);
  29.    this.Box.left = (this.Box.offsetWidth - this.Img.offsetWidth) / 2;
  30.    this.Box.top = (this.Box.offsetHeight - this.Img.offsetHeight) / 2;
  31.    // 阻止默认事件
  32.    return ;
  33.   },
  34.   // 鼠标移除
  35.   Mouseout: function(e){
  36.    var e = e || window.event;
  37.    // 重置css
  38.    this.Img.style.width = this.Img.offsetWidth / this.scale + "px";
  39.    this.Img.style.height =this.Img.offsetHeight / this.scale + "px";
  40.    this.Img.style.left = Math.floor((this.Box.offsetWidth - this.Img.offsetWidth) / 2) + "px";
  41.    this.Img.style.top = Math.floor((this.Box.offsetHeight - this.Img.offsetHeight) / 2) + "px";
  42.    return ;
  43.   },
  44.   // 鼠标移动
  45.   Mousemove: function(e){
  46.    var e = e || window.event;
  47.    // 图片鼠标位置
  48.    var ImgXY = {"x": this.Left + this.BoxX, "y": this.Top + this.BoxY};
  49.    // 获取偏移量
  50.    var left = (ImgXY.x - e.clientX ) / this.BoxX * this.ImgLeft ,
  51.     top = (ImgXY.y - e.clientY) / this.BoxY * this.ImgTop ;
  52.    this.Img.style.left = Math.floor(this.Box.left - left) + "px";
  53.    this.Img.style.top = Math.floor(this.Box.top - top) + "px";
  54.    return ;
  55.   },
  56.   // 初始化
  57.   Init: function(e){
  58.    var that = this;
  59.    this.Box.onmouseover = function(e){
  60.     that.Mouseover(e);
  61.    }
  62.    this.Box.onmouseout = function(e){
  63.     that.Mouseout(e);
  64.    }
  65.    this.Box.onmousemove = function(e){
  66.     that.Mousemove(e);
  67.    }
  68.   }
  69. }
  70. // 实例一个对象
  71. new ScaleImg('.Box');
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行