js圖片放大鏡

來源:互聯網
上載者:User

標籤:event   電商   charset   art   cli   sem   auth   pad   附件   

JS圖片放大鏡效果。

應用情境:各大電商網站。

(附件)

 

<!--Author: XiaoWenCreate a file: 2017-01-13 12:28:36Last modified: 2017-01-13 17:16:46Start to work:Finish the work:Other information:--><!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title>  <style>    *{      margin: 0;      padding: 0;    }    #box{      height: 280px;      width: 480px;      background: url(small.jpg) no-repeat;      float:left;    }    #rec{      height: 100px;      width: 100px;      opacity:.5;      background: #ccc;      position: absolute;      left: 0;      top: 0;      display:none;    }    #box1{      height: 400px;      width: 400px;      background: url(big.jpg) no-repeat;      float:left;      display: none;    }  </style></head><body><div id="box">  <div id="rec"></div></div><div id="box1"></div></body><script>  box.onmouseover=function(){    rec.style.display=‘block‘;    box1.style.display=‘block‘;  }  box.onmousemove=function(ev){    var e=ev||event;    // var ex=e.clientX; //滑鼠位置不在中間    // ex=e.clientX-rec.offsetWidth/2; //滑鼠位置在中間    var ex=e.clientX-rec.offsetWidth/2;    var ey=e.clientY-rec.offsetHeight/2;    //讓小方塊不超出左邊    if(ex<box.offsetLeft){      ex=box.offsetLeft    }    //讓小方塊不超出右邊    if(ex>box.offsetLeft+box.offsetWidth-rec.offsetWidth){ //讓小方塊不超出左邊      ex=box.offsetLeft+box.offsetWidth-rec.offsetWidth    }    //讓小方塊不超出上邊    if(ey<box.offsetTop){      ey=box.offsetTop    }    //讓小方塊不超出下邊    if(ey>box.offsetTop+box.offsetHeight-rec.offsetHeight){      ey=box.offsetTop+box.offsetHeight-rec.offsetHeight    }    rec.style.left=ex+‘px‘;    rec.style.top=ey+‘px‘;    box1.style.backgroundPositionX=-4*ex+‘px‘    box1.style.backgroundPositionY=-4*ey+‘px‘  }  box.onmouseout=function(){    rec.style.display=‘none‘;    box1.style.display=‘none‘;  }</script></html>

 

js圖片放大鏡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.