鼠标悬停图片放大效果显示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠标悬停图片放大提示效果</title>
<style type="text/css">
/*图库样式*/
#gallery {
width:460px;
height: 270px;
margin: 0 auto;
padding: 10px;
background: #383131;
}

ul {
list-style-type: none;
position: absolute;
width: 460px;
}

#gallery ul li {
float: left;
margin: 10px;
background: white;
height: 100px;
width: 100px;
}

#gallery ul li:hover {
border: 3px solid white;
margin: 7px;
cursor: pointer;
}

#gallery ul li img{
height: 100px;
width: 100px;
}
/* 工具提示的样式 */
#tooltip{
position:absolute;
border:1px solid #ccc;
background:#333;
padding:2px;
display:none;
color:#fff;
}
</style>

<script type="text/javascript" src="jquery/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function(){
var x = 10;
var y = 20;
$("a.tooltip").mouseover(function(e){
this.myTitle = this.title; //得到当前链接的titl,即标题
this.title = "";
var imgTitle = this.myTitle? "<br/>" + this.myTitle : "";
var tooltip = "<div id='tooltip'> "+
"<img src='"+ this.href +"' alt='放大提示'/>"+
imgTitle+"</div>"; //新建一个id为tooltip的div元素
$("body").append(tooltip); //元素内部包含一个img,用来显示图片并且显示标题,追加到body区
$("#tooltip") //更改tooptip的顶部和左侧位置
.css({
"top": (e.pageY+y) + "px",
"left": (e.pageX+x) + "px"
}).show("fast"); //使用show函数快速显示
}).mouseout(function(){
this.title = this.myTitle;
$("#tooltip").remove(); //当鼠标移出时,从body区删除该div元素
}).mousemove(function(e){ //当鼠标移动时,调整div的位置位于鼠标箭头的下方
$("#tooltip")
.css({
"top": (e.pageY+y) + "px",
"left": (e.pageX+x) + "px"
});
});
})
</script>
</head>

<body>
<!--实现简单的相册效果-->
<div id="gallery">
<ul>
<!--相册所需要的图片集-->
<li><a href="images/sample1.jpg" class="tooltip" title="喜鹊看花"><img src="images/sample1.jpg"/></a></li>
<li><a href="images/sample2.jpg" class="tooltip" title="繁花朵朵"><img src="images/sample2.jpg"/></a></li>
<li><a href="images/sample3.jpg" class="tooltip" title="姹紫嫣红"><img src="images/sample3.jpg"/></a></li>
<li><a href="images/sample5.jpg" class="tooltip" title="含苞怒放"><img src="images/sample5.jpg"/></a></li>
</ul>
</div>
</body>
</html>
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现鼠标悬停图片放大效果,可以通过以下几种方式: 1. 使用CSS transform属性 可以使用CSS的transform属性来实现图片放大效果。将图片的缩放比例设为1.2或其他比例,当鼠标悬停图片上时,将缩放比例改为1或原来的比例。 示例代码: ```html <style> .img-zoom { transition: transform .2s; } .img-zoom:hover { transform: scale(1.2); } </style> <img src="image.jpg" class="img-zoom"> ``` 2. 使用JavaScript和CSS 通过JavaScript来改变图片的样式,实现图片放大效果。当鼠标悬停图片上时,将图片的宽高增加一定比例,当鼠标移开时,将图片宽高恢复到原来的大小。 示例代码: ```html <style> .img-zoom { transition: width .2s, height .2s; } </style> <img src="image.jpg" class="img-zoom" onmouseover="zoomIn(this)" onmouseout="zoomOut(this)"> <script> function zoomIn(img) { img.style.width = (img.clientWidth * 1.2) + "px"; img.style.height = (img.clientHeight * 1.2) + "px"; } function zoomOut(img) { img.style.width = ""; img.style.height = ""; } </script> ``` 3. 使用jQuery插件 可以使用一些开源的jQuery插件来实现图片放大效果,比如zoom.js、fancybox等。 使用zoom.js实现图片放大效果的示例代码: ```html <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-zoom/1.7.21/jquery.zoom.min.js"></script> <img src="image.jpg" data-zoom-image="image-large.jpg"> <script> $('img').zoom(); </script> ``` 以上是实现鼠标悬停图片放大的几种方式,可以根据实际需求选择适合的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值