js實(shí)現(xiàn)鼠標(biāo)移入圖片放大效果
使用原生js編寫一個(gè)鼠標(biāo)移入圖片放大效果,供大家參考,具體內(nèi)容如下
目標(biāo)
給圖片添加鼠標(biāo)移動放大方法效果,移到哪里放大哪里
先看看效果是不是你想要的,再看代碼
移入前

移入后

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 目標(biāo)class string
// @params Scale 放大倍數(shù) number
function ScaleImg(Class, Scale){
?? ??? ?
?? ??? ?this.Box = document.querySelector(Class);
?? ?
?? ??? ?this.Img = this.Box.querySelector('img');
?? ??? ?
?? ??? ?this.scale = Scale || 3 ;
?? ?
?? ??? ?// 盒子中心點(diǎn)
?? ??? ?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 = {
?? ?
?? ??? ?// 鼠標(biāo)移入
?? ??? ?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";
?? ?
?? ??? ??? ?// 設(shè)置放大后的圖片定位
?? ??? ??? ?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;
?? ??? ??? ?
?? ??? ??? ?// 阻止默認(rèn)事件
?? ??? ??? ?return ;
?? ?
?? ??? ?},
?? ?
?? ??? ?// 鼠標(biāo)移除
?? ??? ?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 ?;
?? ??? ?},
?? ?
?? ??? ?// 鼠標(biāo)移動
?? ??? ?Mousemove: function(e){
?? ?
?? ??? ??? ?var e = e || window.event;
?? ?
?? ??? ??? ?// 圖片鼠標(biāo)位置
?? ??? ??? ?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);
?? ??? ??? ?}
?? ?
?? ??? ?}
?? ?}
?? ?
?? ?// 實(shí)例一個(gè)對象
?? ?new ScaleImg('.Box');?? ?以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信公眾號開發(fā)之微信支付代碼記錄的實(shí)現(xiàn)
這篇文章主要介紹了微信公眾號開發(fā)之微信支付代碼記錄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
JavaScript實(shí)現(xiàn)各種排序的代碼詳解
這篇文章給大家介紹了js實(shí)現(xiàn)各種排序功能,包括冒泡排序,選擇排序和插入排序,代碼簡單易懂,非常不錯,具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-08-08
Antd中Table列表行默認(rèn)包含修改及刪除功能的封裝方法
這篇文章主要介紹了Antd中Table列表行默認(rèn)包含修改及刪除功能的封裝,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
javascript 網(wǎng)頁跳轉(zhuǎn)的方法
昨天練習(xí)的時(shí)候正好要用到跳轉(zhuǎn)代碼,在網(wǎng)上找了一下,覺得下面幾個(gè)不錯...整理了一下發(fā)上來...2008-12-12
Three.js利用性能插件stats實(shí)現(xiàn)性能監(jiān)聽的方法
Three.js 是一款運(yùn)行在瀏覽器中的 3D 引擎,你可以用它創(chuàng)建各種三維場景,而下面這篇文章主要給大家介紹了關(guān)于Three.js如何利用性能插件stats實(shí)現(xiàn)性能監(jiān)聽的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
發(fā)布BlueShow v1.0 圖片瀏覽器(類似lightbox)blueshow.js 打包下載
發(fā)布BlueShow v1.0 圖片瀏覽器(類似lightbox)blueshow.js 打包下載...2007-07-07
配置Grunt的Task時(shí)通配符支持和動態(tài)生成文件名問題
這篇文章主要介紹了配置Grunt的Task時(shí)通配符支持和動態(tài)生成文件名問題,需要的朋友可以參考下2015-09-09

