1. 程式人生 > >原生js實現放大鏡

原生js實現放大鏡

dde script 原生js etl 是個 abs 不讓 llb 過濾器

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>放大鏡</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }
        #demo {
            display: block;
            width: 323px;
            height
: 430px; margin: 5px; position: relative; border: 1px solid #ccc; } #small-box { position: relative; z-index: 1; } #float-box { display: none; width: 160px; height
: 120px; position: absolute; background: #ffffcc; border: 1px solid #ccc; filter: alpha(opacity=50); opacity: 0.5; } #mark {/*應該是把圖片放在這個上面,它的透明度為0,是個block*/ position: absolute; display: block; width
: 323px; height: 430px; background-color: #fff; filter: alpha(opacity=0);/*過濾器,目前只有少數瀏覽器支持*/ opacity: 0; z-index: 10; } #big-box { display: none; position: absolute; top: 0; left: 330px; width: 323px; height: 430px; overflow: hidden; border: 1px solid #ccc; z-index: 1;; } #big-box img { position: absolute; z-index: 5 } </style> <script> //頁面加載完畢後執行 window.onload = function () { //找六個個元素:demo,smallBox,foatBox,mark,bigfloatBox,imgs, var objDemo=document.getElementById("demo"); var objSmallBox=document.getElementById("small-box"); var objMarkBox=document.getElementById("mark"); var objFloatBox=document.getElementById("float-box"); var objBigBox=document.getElementById("big-box"); var objBigBoxImg=objBigBox.getElementsByTagName("img")[0]; //給小盒子添加事件,移入和移出 //移入:浮動的box和和bigBox顯示 objSmallBox.onmouseover=function(){ objFloatBox.style.display="block"; objBigBox.style.display="block"; } //移除:浮動的box和bigBox隱藏 objSmallBox.onmouseout=function(){ objFloatBox.style.display="none"; objBigBox.style.display="none"; } //給小盒子添加鼠標移動事件 objMarkBox.onmousemove=function(ev){ var _event=ev||window.event;//做兼容性,兼容IE //1計算值: var left=_event.clientX-objDemo.offsetLeft-objSmallBox.offsetLeft-objFloatBox.offsetWidth/2; var top=_event.clientY-objDemo.offsetTop-objSmallBox.offsetTop-objFloatBox.offsetHeight/2; //5.優化,在前面加判斷,不讓其溢出,加判斷 if(left<0) left=0; if(top<0) top=0; if(left>objSmallBox.offsetWidth-objFloatBox.offsetWidth) left=objSmallBox.offsetWidth-objFloatBox.offsetWidth; if(top>objSmallBox.offsetHeight-objFloatBox.offsetHeight) top=objSmallBox.offsetHeight-objFloatBox.offsetHeight; //2把值賦值給放大鏡 objFloatBox.style.left=left+"px"; objFloatBox.style.top=top+"px"; //3計算比例 var percentX=left/(objMarkBox.offsetWidth-objFloatBox.offsetWidth); var percentY=top/(objMarkBox.offsetHeight-objFloatBox.offsetHeight); //4利用這個比例計算距離後賦值給右側的圖片 objBigBoxImg.style.left=-percentX*(objBigBoxImg.offsetWidth-objBigBox.offsetWidth)+"px"; objBigBoxImg.style.top=-percentY*(objBigBoxImg.offsetHeight-objBigBox.offsetHeight)+"px"; } } </script> </head> <body> <div id="demo"> <div id="small-box"> <div id="mark"></div> <div id="float-box"></div> <img src="https://img.alicdn.com/imgextra/https://img.alicdn.com/bao/uploaded/i2/1026430696/O1CN011H0o7UD6ZwLT3vl_!!1026430696.jpg_430x430q90.jpg"/> </div> <div id="big-box"> <img src="https://img.alicdn.com/imgextra/https://img.alicdn.com/bao/uploaded/i2/1026430696/O1CN011H0o7UD6ZwLT3vl_!!1026430696.jpg"/> </div> </div> </body> </html>

原生js實現放大鏡