js實(shí)現(xiàn)放大鏡效果

前言:在拿到一個(gè)需求之前,我們要先進(jìn)行分析,再設(shè)計(jì),最后實(shí)現(xiàn)代碼的編寫(xiě)以及調(diào)試。

放大鏡的原理分析

  • 鼠標(biāo)在小圖片上移動(dòng)時(shí),通過(guò)捕獲鼠標(biāo)在小圖片上的位置,定位大圖片的相應(yīng)位置

放大鏡特效設(shè)計(jì)

  • 頁(yè)面元素
  • 技術(shù)點(diǎn):事件捕獲、定位
  • 難點(diǎn):計(jì)算

代碼編寫(xiě),Github源碼下載 [https://github.com/galyean-gong/function.git ]

<!DOCTYPE html>
<html>
<head>
   <title>放大鏡特效</title>
</head>
<style type="text/css">
      * {
           margin: 0;
           padding: 0
       }

       #demo {
           display: block;
           width: 400px;
           height: 255px;
           margin: 50px;
           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 {
           position: absolute;
           display: block;
           width: 400px;
           height: 255px;
           background-color: #fff;
           filter: alpha(opacity=0);
           opacity: 0;
           z-index: 10;
       }

       #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>
<body>
<div id="demo">
   <div id="small-box">
       <div id="mark"></div>
       <div id="float-box"></div>
       ![](image/saml.jpg)
   </div>
   <div id="big-box">
       ![](image/big.jpg)
   </div>
</div>
<!-- offsetLeft 子元素想當(dāng)父元素的左位移 
offsetTop 子元素想當(dāng)父元素的上位移
offsetWidth 盒子本身的寬高 不包括滾動(dòng)條
event.clientX 鼠標(biāo)的X,Y軸相對(duì)與整個(gè)頁(yè)面,而非子元素
-->
<script type="text/javascript">

    //頁(yè)面加載完畢后執(zhí)行
       window.onload = function () {

           var objDemo = document.getElementById("demo");
           var objSmallBox = document.getElementById("small-box");
           var objMark = document.getElementById("mark");
           var objFloatBox = document.getElementById("float-box");
           var objBigBox = document.getElementById("big-box");
           var objBigBoxImage = objBigBox.getElementsByTagName("img")[0];

           objMark.onmouseover = function () {
               objFloatBox.style.display = "block"
               objBigBox.style.display = "block"
           }

           objMark.onmouseout = function () {
               objFloatBox.style.display = "none"
               objBigBox.style.display = "none"
           }

           objMark.onmousemove = function (ev) {

               var _event = ev || window.event;  //兼容多個(gè)瀏覽器的event參數(shù)模式

               var left = _event.clientX - objDemo.offsetLeft - objSmallBox.offsetLeft - objFloatBox.offsetWidth / 2;
               var top = _event.clientY - objDemo.offsetTop - objSmallBox.offsetTop - objFloatBox.offsetHeight / 2;

               if (left < 0) {
                   left = 0;
               } else if (left > (objMark.offsetWidth - objFloatBox.offsetWidth)) {
                   left = objMark.offsetWidth - objFloatBox.offsetWidth;
               }

               if (top < 0) {
                   top = 0;
               } else if (top > (objMark.offsetHeight - objFloatBox.offsetHeight)) {
                   top = objMark.offsetHeight - objFloatBox.offsetHeight;

               }

               objFloatBox.style.left = left + "px";   //oSmall.offsetLeft的值是相對(duì)什么而言
               objFloatBox.style.top = top + "px";

               var percentX = left / (objMark.offsetWidth - objFloatBox.offsetWidth);
               var percentY = top / (objMark.offsetHeight - objFloatBox.offsetHeight);

               objBigBoxImage.style.left = -percentX * (objBigBoxImage.offsetWidth - objBigBox.offsetWidth) + "px";
               objBigBoxImage.style.top = -percentY * (objBigBoxImage.offsetHeight - objBigBox.offsetHeight) + "px";
           }
       }
</script>


</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容