大家經(jīng)常逛淘寶、天貓、京東這類網(wǎng)站的時候往往會看到一些圖片展示的效果,例如:把鼠標(biāo)放在圖片上右側(cè)會出現(xiàn)一個放大的預(yù)覽區(qū)域,這就是所謂放大鏡效果。今天閑著沒事干,就打算復(fù)習(xí)一下JavaScript基礎(chǔ),用一下基礎(chǔ)知識制作一個類似于淘寶的放大鏡效果。
先說一下這個效果需要用到的一些基礎(chǔ)知識:
css相對定位:position:absolute;
鼠標(biāo)移入移出以及移動事件:onmouseover、onmouseout、onmousemove,記住這些事件一般不會單個出現(xiàn)
獲取鼠標(biāo)點擊坐標(biāo):X軸:clientX,Y軸:clientY
當(dāng)前元素相對于父元素的左位移:offsetLeft
當(dāng)前元素相對于父元素的上位移:offsetTop
當(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是因為讓鼠標(biāo)點出現(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)可以移動的最大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)占可移動區(qū)域的比例 var percentY = t / (floatBox.offsetHeight - smallBboxHeight); bigBoxImg.style.left = -percentX *(bigBoxImg.offsetWidth - bigBox.offsetWidth) +"px";//大圖對的移動方向和小圖遮罩的移動方向相反 bigBoxImg.style.top = -percentY*(bigBoxImg.offsetHeight - bigBox.offsetHeight)+"px"; } } </script> </body> </html>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com