jq鼠标悬浮小图弹出大图效果详解

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery的鼠标悬停时放大图片的效果制作</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style>
* {
  margin:0;
  padding:0;
}
img{
  vertical-align:bottom;
  border:none;
}
body{
  background:#f0f0f0;
  height:800px;
  font-family:Arial;
}
ul {
  list-style:none;
}
h1{
  font-size:20px;
  width:680px;
  margin:3% auto 5px;
  color:#202020;
}
ul#gallery{
  width:660px;
  margin:0 auto 10px;
  padding-left:20px;
  border:1px solid #d3d3d3;
  background:#fff;
  overflow:hidden;
}
ul#gallery li{
  width:200px;
  height:200px;
  float:left;
  margin:20px 20px 20px 0;
  display:inline;
}
ul#gallery li a.smallimage{
  background:#333;
  display:block;
  width:200px;
  height:200px;
}
#bigimage{
  position:absolute;
  display:none;
}
#bigimage img{
  width:400px;
  height:400px;
  padding:5px;
  background:#fff;
  border:1px solid #e3e3e3;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){ 
  var x = 22;
  var y = 20;
    
  $("a.smallimage").hover(function(e){
    $("body").append('<p id="bigimage"><img src="'+ this.rel + '" alt="" /></p>');
    $(this).find('img').stop().fadeTo('slow',0.5);  
    widthJudge(e);
    $("#bigimage").fadeIn('fast');
  },function(){
    $(this).find('img').stop().fadeTo('slow',1);
    $("#bigimage").remove();
  }); 
  $("a.smallimage").mousemove(function(e){
    widthJudge(e);
  }); 
  function widthJudge(e){
    var marginRight = document.documentElement.clientWidth - e.pageX; 
    var imageWidth = $("#bigimage").width();
    if(marginRight < imageWidth){
      x = imageWidth + 22;
      $("#bigimage").css({top:(e.pageY - y ) + 'px',left:(e.pageX - x ) + 'px'}); 
    }
   else{
      x = 22;
      $("#bigimage").css({top:(e.pageY - y ) + 'px',left:(e.pageX + x ) + 'px'});
    }; 
  }
})
</script>
</head>
<body>
<ul id="gallery">
<li>
  <a href="#" class="smallimage" rel="mytest/code/726/images/001_big.jpg">
   <img src="mytest/code/726/images/001_small.jpg" alt="" />
  </a>
</li>
<li>
<a href="#" class="smallimage" rel="mytest/code/726/images/002_big.jpg">
  <img src="mytest/code/726/images/002_small.jpg" alt="" />
</a>
</li>
<li>
<a href="#" class="smallimage" rel="mytest/code/726/images/003_big.jpg">
  <img src="mytest/code/726/images/003_small.jpg" alt="" />
</a>
</li>
</ul>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值