js實(shí)現(xiàn)簡(jiǎn)單放大鏡效果
用js實(shí)現(xiàn)簡(jiǎn)單放大鏡效果,供大家參考,具體內(nèi)容如下
此處放大鏡實(shí)現(xiàn)的效果就是當(dāng)鼠標(biāo)放置在圖片上會(huì)有半透明遮罩,圖片的一個(gè)區(qū)域就會(huì)被放大,然后展示在右邊。當(dāng)鼠標(biāo)移動(dòng)時(shí)右邊的大圖片也會(huì)局部移動(dòng)。這里的放大并不是真正的放大,而是等比例移動(dòng)。下面是實(shí)現(xiàn)的代碼:
css樣式代碼如下:
<style> .s_box{width:400px;height: 300px;position: absolute;left: 50px;top:100px;} .s_box img{width: 400px;height: 300px;} .s_box span{width: 130px;height: 100px;background: rgba(200,200,200,0.5);position: absolute;left:0;top:0;display: none;cursor:move;} .b_box{width: 400px;height: 300px;overflow: hidden;position: absolute;left:500px;top:100px;display: none;} .b_box img{width: 1200px;height: 900px;position: absolute;left:0;top:0;} .list{margin: 0;padding: 0;list-style: none;position: absolute;left:50px;top:430px;} .list li{float: left;margin: 0 10px;} .list li img{width: 100px;height: 80px;} </style>
html代碼如下:
<body> <div class="s_box"> <img src="../img/large1.jpg" alt=""> <span></span> </div> <div class="b_box"> <img src="../img/large1.jpg" alt=""> </div> <ul class="list"> <li><img src="../img/large1.jpg" alt=""></li> <li><img src="../img/large2.jpg" alt=""></li> </ul> </body>
js主要代碼如下:
// 分析: // 1.選擇元素 // 2.綁定事件 // 3.進(jìn)入的時(shí)候顯示元素 // 4.移動(dòng):遮罩層跟隨鼠標(biāo)移動(dòng)的同時(shí)計(jì)算遮罩層的移動(dòng)比例 、右側(cè)大圖,等比例移動(dòng) // 5.離開的時(shí)候隱藏元素 <script> class Large{ constructor(){ this.sBox = document.querySelector(".s_box"); this.sImg = document.querySelector(".s_box img"); this.sSpan = document.querySelector(".s_box span"); this.bBox = document.querySelector(".b_box"); this.bImg = document.querySelector(".b_box img"); // 點(diǎn)擊小圖切換大圖的按鈕 this.li = document.querySelectorAll(".list li"); } addEvent(){ var that = this; this.sBox.onmouseover = function(){ that.over(); } this.sBox.onmousemove = function(eve){ var e = eve || window.event; that.move(e); } this.sBox.onmouseout = function(){ that.out(); } // 切換圖片按鈕的點(diǎn)擊事件:根據(jù)布局做出調(diào)整 for(var i=0;i<this.li.length;i++){ this.li[i].onclick = function(){ that.sImg.src = this.children[0].src; that.bImg.src = this.children[0].src; } } } over(){ this.sSpan.style.display = "block"; this.bBox.style.display = "block"; } move(e){ // 計(jì)算遮罩層跟隨鼠標(biāo)移動(dòng)時(shí)的left和top var l = e.pageX - this.sBox.offsetLeft - this.sSpan.offsetWidth/2; var t = e.pageY - this.sBox.offsetTop - this.sSpan.offsetHeight/2; // 邊界限定 if(l<0) l=0; if(t<0) t=0; if(l > this.sBox.offsetWidth - this.sSpan.offsetWidth){ l = this.sBox.offsetWidth - this.sSpan.offsetWidth; } if(t > this.sBox.offsetHeight - this.sSpan.offsetHeight){ t = this.sBox.offsetHeight - this.sSpan.offsetHeight; } // 設(shè)置遮罩層的位置 this.sSpan.style.left = l + "px"; this.sSpan.style.top = t + "px"; // 根據(jù)遮罩層移動(dòng)的距離計(jì)算比例 var x = l / (this.sBox.offsetWidth - this.sSpan.offsetWidth); var y = t / (this.sBox.offsetHeight - this.sSpan.offsetHeight); // 根據(jù)上一步得到的比例,計(jì)算右側(cè)大圖要移動(dòng)的當(dāng)前值 this.bImg.style.left = (this.bBox.offsetWidth - this.bImg.offsetWidth) * x + "px"; this.bImg.style.top = (this.bBox.offsetHeight - this.bImg.offsetHeight) * y + "px"; } out(){ this.sSpan.style.display = "none"; this.bBox.style.display = "none"; } } // 啟動(dòng) var l = new Large(); l.addEvent(); </script>
實(shí)現(xiàn)效果:
更多關(guān)于放大鏡的精彩文章,請(qǐng)點(diǎn)擊鏈接查看:《放大鏡效果》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS中Json數(shù)據(jù)的處理和解析JSON數(shù)據(jù)的方法詳解
JSON (JavaScript Object Notation)一種簡(jiǎn)單的數(shù)據(jù)格式,比xml更輕巧,這篇文章主要介紹了JS中Json數(shù)據(jù)的處理和解析JSON數(shù)據(jù)的方法詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06如何實(shí)現(xiàn)動(dòng)態(tài)刪除javascript函數(shù)
如何實(shí)現(xiàn)動(dòng)態(tài)刪除javascript函數(shù)...2007-05-05JSON基本語(yǔ)法及與JavaScript的異同實(shí)例分析
這篇文章主要介紹了JSON基本語(yǔ)法及與JavaScript的異同,結(jié)合實(shí)例形式分析了json簡(jiǎn)單值、對(duì)象、數(shù)組三種類型值使用技巧,需要的朋友可以參考下2019-01-01基于JavaScript實(shí)現(xiàn)圖片點(diǎn)擊彈出窗口而不是保存
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)圖片點(diǎn)擊彈出窗口而不是保存的相關(guān)資料,需要的朋友可以參考下2016-02-02深入解析ECMAScript?2023?中的新數(shù)組方法
ECMAScript?是一種標(biāo)準(zhǔn)化的腳本語(yǔ)言,它是?JavaScript?的規(guī)范。ECMAScript?2023?是?JavaScript?編程語(yǔ)言的更新,旨在帶來(lái)改進(jìn)并使?JavaScript?程序可預(yù)測(cè)和可維護(hù),這篇文章主要介紹了探索?ECMAScript?2023?中的新數(shù)組方法,需要的朋友可以參考下2023-12-12微信小程序?qū)崿F(xiàn)登錄注冊(cè)tab切換效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)登錄注冊(cè)切換效果,簡(jiǎn)易版tab切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11微信小程序audio組件在ios端無(wú)法播放的解決辦法
audio是音頻組件,用于播放一個(gè)基于http協(xié)議的音頻資源,這篇文章主要給大家介紹了關(guān)于微信小程序audio組件在ios端無(wú)法播放的解決辦法,需要的朋友可以參考下2021-07-07Easyui form combobox省市區(qū)三級(jí)聯(lián)動(dòng)
這篇文章主要介紹了Easyui form combobox省市區(qū)三級(jí)聯(lián)動(dòng) 的相關(guān)資料,需要的朋友可以參考下2016-01-01