原生js實(shí)現(xiàn)淘寶放大鏡效果
大家經(jīng)常逛淘寶、天貓、京東這類網(wǎng)站的時(shí)候往往會(huì)看到一些圖片展示的效果,例如:把鼠標(biāo)放在圖片上右側(cè)會(huì)出現(xiàn)一個(gè)放大的預(yù)覽區(qū)域,這就是所謂放大鏡效果。今天閑著沒事干,就打算復(fù)習(xí)一下JavaScript基礎(chǔ),用一下基礎(chǔ)知識(shí)制作一個(gè)類似于淘寶的放大鏡效果。
先說一下這個(gè)效果需要用到的一些基礎(chǔ)知識(shí):
css相對(duì)定位:position:absolute;
鼠標(biāo)移入移出以及移動(dòng)事件:onmouseover、onmouseout、onmousemove,記住這些事件一般不會(huì)單個(gè)出現(xiàn)
獲取鼠標(biāo)點(diǎn)擊坐標(biāo):X軸:clientX,Y軸:clientY
當(dāng)前元素相對(duì)于父元素的左位移:offsetLeft
當(dāng)前元素相對(duì)于父元素的上位移:offsetTop
當(dāng)前元素的實(shí)際高、寬度(不包括滾動(dòng)條):offsetWidth、offsetHeight
球當(dāng)前元素的最小值,最大值:Math.min()、Math.max();
話不多說直接上代碼吧!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>放大鏡效果</title> <style type="text/css"> *{ padding: 0; margin: 0; } #demo{ display: block; width: 400px; height: 255px; margin: 50px; position: relative; border: 1px solid #ccc; } #float-box{ position: relative; z-index: 1; } #small-box{ display: none; width: 160px; height: 120px; position: absolute; background: #ffffcc; border: 1px solid #ccc; filter: alpha(opacity=50); opacity: 0.5; cursor: move; } #big-box{ display: none; position: absolute; top: 0; left: 460px; width: 400px; height: 300px; overflow: hidden; border: 1px solid #ccc; z-index: 1; } #big-box img{ position: absolute; z-index: 5; } </style> </head> <body> <div id="demo"> <div id="float-box"> <div id="small-box"></div> <img src="../images/macbook-small.jpg"> </div> <div id="big-box"> <img src="../images/macbook-big.jpg"> </div> </div> <script type="text/javascript"> window.onload = function(){ //獲取到需要的元素 var demo = document.getElementById('demo'); var smallBbox = document.getElementById('small-box'); var floatBox = document.getElementById('float-box'); var bigBox = document.getElementById('big-box'); var bigBoxImg = bigBox.getElementsByTagName('img')[0]; floatBox.onmouseover = function(){ smallBbox.style.display = "block"; bigBox.style.display = "block"; } floatBox.onmouseout = function(){ smallBbox.style.display = "none"; bigBox.style.display = "none"; } floatBox.onmousemove = function(e){ var _event = e || event; console.log(_event.clientY); var l = _event.clientX - demo.offsetLeft - floatBox.offsetLeft - smallBbox.offsetWidth/2;//除2是因?yàn)樽屖髽?biāo)點(diǎn)出現(xiàn)在放大遮罩的中心位置 var t = _event.clientY - demo.offsetTop - floatBox.offsetTop - smallBbox.offsetHeight/2; var demoWidth = demo.offsetWidth; var demoHeight = demo.offsetHeight; var smallBboxWidth = smallBbox.offsetWidth; var smallBboxHeight = smallBbox.offsetHeight; //鼠標(biāo)可以移動(dòng)的最大XY的距離 var maxX = demoWidth - smallBboxWidth; var maxY = demoHeight - smallBboxHeight; l = Math.min(maxX,Math.max(0,l)); t = Math.min(maxY,Math.max(0,t)); smallBbox.style.left = l +"px"; smallBbox.style.top = t +"px"; var percentX = l / (floatBox.offsetWidth - smallBboxWidth);//求出小圖遮罩的坐標(biāo)占可移動(dòng)區(qū)域的比例 var percentY = t / (floatBox.offsetHeight - smallBboxHeight); bigBoxImg.style.left = -percentX *(bigBoxImg.offsetWidth - bigBox.offsetWidth) +"px";//大圖對(duì)的移動(dòng)方向和小圖遮罩的移動(dòng)方向相反 bigBoxImg.style.top = -percentY*(bigBoxImg.offsetHeight - bigBox.offsetHeight)+"px"; } } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
根據(jù)判斷瀏覽器類型屏幕分辨率自動(dòng)調(diào)用不同CSS的代碼
根據(jù)判斷瀏覽器類型屏幕分辨率自動(dòng)調(diào)用不同CSS的代碼...2007-02-02基于JavaScript實(shí)現(xiàn)一個(gè)簡單的事件觸發(fā)器
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)一個(gè)簡單的事件觸發(fā)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01JavaScript實(shí)現(xiàn)動(dòng)畫打開半透明提示層的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)畫打開半透明提示層的方法,涉及javascript操作DOM的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04解決頁面整體使用transform scale后高德地圖點(diǎn)位點(diǎn)擊偏移錯(cuò)位問題
這篇文章主要介紹了解決頁面整體使用transform scale后高德地圖點(diǎn)位點(diǎn)擊偏移錯(cuò)位問題的方法,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-01-01實(shí)例講解JavaScript中call、apply、bind方法的異同
這篇文章主要以實(shí)例講解的方式為大家總結(jié)了JavaScript中call、apply、bind方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09js遍歷詳解(forEach, map, for, for...in, for...of)
在本篇文章里小編給大家整理的是關(guān)于js中的各種遍歷(forEach, map, for, for...in, for...of)相關(guān)知識(shí)點(diǎn)用法總結(jié),需要的朋友們參考下。2019-08-08JavaScript常見數(shù)組方法之如何轉(zhuǎn)置矩陣
這篇文章主要給大家介紹了關(guān)于JavaScript常見數(shù)組方法之如何轉(zhuǎn)置矩陣的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03JS實(shí)現(xiàn)倒計(jì)時(shí)和文字滾動(dòng)的效果實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)倒計(jì)時(shí)和文字滾動(dòng)的效果,以實(shí)例的形式分析了倒計(jì)時(shí)與文字滾動(dòng)效果的具體實(shí)現(xiàn)方法,并附有js時(shí)間變量的說明,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10