CSS-滑鼠移上去顯示大圖

來源:互聯網
上載者:User

最終效果

--------------------------------------------------css部分-----------------------------------

#demo{overflow:hidden;;text-align:center;padding:10px;}
#demo img{border:none;border:5px solid #f4f4f4}
#enlarge_images{position:absolute;display:none;z-index:1;border:5px solid #f4f4f4}

--------------------------------------------------頁面部分-----------------------------------

<div id="demo" class="k_bg_demo">

    <img src="images/batno/02.jpg" width="200" height="150"  > </div><div id="enlarge_images">

</div>

--------------------------------------------------JS部分-----------------------------------

  var demo = document.getElementById("demo");
  var gg = demo.getElementsByTagName("img");
  var ei = document.getElementById("enlarge_images");
  for(i=0; i<gg.length; i++){
   var ts = gg[i];
   ts.onmousemove = function(event){
    event = event || window.event;
    ei.style.display = "block";
    ei.innerHTML = '<img src="' + this.src + '" />';
    ei.style.top  = document.body.scrollTop + event.clientY + 10 + "px";
    ei.style.left = document.body.scrollLeft + event.clientX + 10 + "px";
   }
   ts.onmouseout = function(){
    ei.innerHTML = "";
    ei.style.display = "none";
   }
   ts.onclick = function(){
    window.open( this.src );
   }
  }

註:如果頁面中有多張圖片要應用此效果,可以此js代碼外再加一層迴圈

如頁面部分有兩張圖片要應用此效果

<div id="demo0" class="k_bg_demo">

    <img src="images/batno/02.jpg" width="200" height="150"  > </div><div id="enlarge_images">

</div>

<div id="demo1" class="k_bg_demo">

    <img src="images/batno/02.jpg" width="200" height="150"  > </div><div id="enlarge_images">

</div>

js部分改為:
for(var j=0;j<8;j++){

   上js部分的代碼

   var demo = document.getElementById("demo"+j);

}

相關文章

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.