js实现鼠标移入图片放大效果

使用原生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');    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 使用JS实现鼠标放上图片进行放大离开实现缩小功能

    使用JS实现鼠标放上图片进行放大离开实现缩小功能,具体代码如下所示: <!DOCTYPE html> <html> <head> <title></title> </head> <body> <div id= 'div_jpg' style="width: 200px;height: 200px;"> <img src="./128206.jpg" id="

  • js实现图片旋转 js滚动鼠标中间对图片放大缩小

    从开通博客园到今天,有两个多月了.我发现之前没有开通博客记录自己所做的东西,真是后悔啊. 现在一点一点把自己所做的功能以博客的形式记录下来,一方面可以给大家分享,大家一起学习,同时自己也从新回顾一下. 这个图片放大,缩小和旋转,我采用canvas画布这个来做的,核心点就在js中去控制鼠标状态及事件. 我先给大家展示一下效果图. 鼠标移到画布范围内就会出现下方的操作栏,每次以90度选择. 1.在引入js的时候一定要注意了,由于在使用画布canvas时,需要等图片加载完成后才可以执行画布里的内容.j

  • 鼠标滑上去后图片放大浮出效果的js代码

    复制代码 代码如下: <script> function GetAbsPosition(obj) { var curleft = 0, curtop = 0; do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); return [curleft,curtop]; } function ShowFloatingImage(image, width, height) {

  • JS实现鼠标移动到缩略图显示大图的图片放大效果

    一个网页上用的图片提示效果,当把鼠标移动到图片缩略图的时候,会显示图片大图,似乎在网上这是个很常见的效果,实现方法也比较多,有人用CSS,有人用JavaScript,有人用jQuery,总之,选择自己习惯的方式去实现,就是最棒的. 图片提示效果 body{margin:0 ;padding:40px;line-height:180%;} img{border:none;} ul,li{margin:0 ;padding:0;} li{list-style:none;display:inline;

  • js实现图片放大并跟随鼠标移动特效

    图片跟随鼠标移动并放大js特效,供大家参考,具体内容如下 很多网站有类似于淘宝放大镜的效果,只不过这里说的是 " 不仅能直接放大,而且会跟随鼠标移动 " ! 类似于" DEDECMS "官网的案例中心的效果 ! 本案例代码,效果图,代码,参考如下: JS代码: <script> //展示 function showBigPic(filepath) { //将文件路径传给img大图 document.getElementById('bigPic').src

  • JS与CSS3实现图片响应鼠标移动放大效果示例

    本文实例讲述了JS与CSS3实现图片响应鼠标移动放大效果.分享给大家供大家参考,具体如下: 今天看网易的网站上,当我把鼠标放上去的时候发现图片放大,移开图片缩小,于是自行尝试,结果如下. 方法一:使用js和css3 效果如图: 这样的实现非常简单,就是利用js的mouseover和 mouseout事件,但是不知道如何使图片从中间放大,日后再行尝试吧,代码如下: <!DOCTYPE html> <html> <head> <title>网易图片动画</

  • js实现鼠标移入图片放大效果

    使用原生js编写一个鼠标移入图片放大效果,供大家参考,具体内容如下 目标 给图片添加鼠标移动放大方法效果,移到哪里放大哪里 先看看效果是不是你想要的,再看代码 移入前 移入后 html <!-- css看着写 -->     <div class="Box" style="width:200px;height:200px;border:1px solid #f00;position: relative;top:0;left:0;overflow: hidde

  • js实现鼠标触发图片抖动效果的方法

    本文实例讲述了js实现鼠标触发图片抖动效果的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: <html> <head> <title>鼠标触发图片抖动效果</title> <style> .shakeimage{ position:relative } </style> <script language="JavaScript1.2"> //configure shake degr

  • Layui实现数据表格中鼠标悬浮图片放大效果,离开时恢复原图的方法

    如下所示: var tableIns = window.demoTable = table .render({ elem : '#idTest', id : 'idTest', url : '<%=path%>/partyMember/getPartyMembersByOrgCode', //width : 1500, height : 'full', cols : [ [ //标题栏 {checkbox : true,LAY_CHECKED : false,filter : 'test'},

  • 基于jquery的仿百度的鼠标移入图片抖动效果

    1.查看源文件,在查看后很纳闷的发现,此页并没有包含那些奖品信息.这样就断定代码在另一个页面中.于是想当然的以为是用的框架连接的地址.结果没查到,却看到了一个这样的信息: 复制代码 代码如下: <div id ="task-intro-box"><!--活动说明--></div> <div id ="task-awards"><!--活动奖励--></div> <div id ="

  • 一个跟随鼠标的图片放大效果,与FF兼容

    <div align=center style="overfow: hidden; width: 100px; height: 100px"><img src="http://files.jb51.net/upload/2007426163751315.jpg" width="100" height="100" border="0" onmouseover="show(this)

  • JS图片放大效果简单实现代码

    本文实例为大家分享了JS实现图片放大效果 ,供大家参考,具体内容如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> <style type="text/css"> #div1 { width:300px; height:300px; position:relative; margin:30p

  • js 图片放大效果 修正版

    首先:我们需要一个可以移动的DIV 复制代码 代码如下: <div style="border:1px solid #CCC;position:absolute; width:200px; height:100px; cursor:move;" id="jelle_test_divquot;> 这个DIV 可以移动,你可以测试下. </div> <script type="text/javascript"> var ge

  • cloudgamer出品ImageZoom 图片放大效果

    一般用于放大查看商品图片,在凡客,京东商城,阿里巴巴等都有类似的效果. 好处是能在原图附近对图片进行局部放大查看,而且可以通过鼠标控制查看的部位. 前一阵子看到sohighthesky的图片放大效果,心血来潮自己也写一个看看. 这个程序有以下特点: 1,支持使用原图放大或新图片设置大图: 2,大图完成载入前使用原图放大代替,减少操作等待时间: 3,支持鼠标滚动缩放大图: 4,可以通过设置显示范围或显示框大小设置显示尺寸: 5,可以设置是否自动隐藏显示框: 6,支持插件形式的扩展来实现更多的功能(

  • JavaScript 图片放大效果及代码说明

    Magnifier #magnifier{ width:342px; height:420px; position:absolute; top:100px; left:250px; font-size:0; border:1px solid #000; } #img{ width:342px; height:420px; } #Browser{ border:1px solid #000; z-index:100; position:absolute; background:#555; } #m

随机推荐