js實現(xiàn)鼠標移入圖片放大效果
更新時間:2022年07月13日 08:27:40 作者:checkMa
這篇文章主要為大家詳細介紹了js實現(xiàn)鼠標移入圖片放大效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
使用原生js編寫一個鼠標移入圖片放大效果,供大家參考,具體內(nèi)容如下
目標
給圖片添加鼠標移動放大方法效果,移到哪里放大哪里
先看看效果是不是你想要的,再看代碼
移入前

移入后

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 放大倍數(shù) 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";
?? ?
?? ??? ??? ?// 設(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;
?? ??? ??? ?
?? ??? ??? ?// 阻止默認事件
?? ??? ??? ?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');?? ?以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript 網(wǎng)頁跳轉(zhuǎn)的方法
昨天練習的時候正好要用到跳轉(zhuǎn)代碼,在網(wǎng)上找了一下,覺得下面幾個不錯...整理了一下發(fā)上來...2008-12-12
Three.js利用性能插件stats實現(xiàn)性能監(jiān)聽的方法
Three.js 是一款運行在瀏覽器中的 3D 引擎,你可以用它創(chuàng)建各種三維場景,而下面這篇文章主要給大家介紹了關(guān)于Three.js如何利用性能插件stats實現(xiàn)性能監(jiān)聽的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起學習學習吧。2017-09-09
發(fā)布BlueShow v1.0 圖片瀏覽器(類似lightbox)blueshow.js 打包下載
發(fā)布BlueShow v1.0 圖片瀏覽器(類似lightbox)blueshow.js 打包下載...2007-07-07
配置Grunt的Task時通配符支持和動態(tài)生成文件名問題
這篇文章主要介紹了配置Grunt的Task時通配符支持和動態(tài)生成文件名問題,需要的朋友可以參考下2015-09-09

