HTML 图片鼠标悬停动态效果
2016-02-17 17:01
471 查看
法一:
在head中添加:
图片标签:
<imgid="imgTest"src="imgs/icon.jpg"/>
法二:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title></title>
<scripttype="text/javascript"src="js/jquery-1.11.1.min.js"></script>
<styletype="text/css">
body{background:#000;}
.img{float:left;width:200px;height:200px;overflow:hidden;margin:01px1px0;}
.img.inner{position:relative;}
.img.innerdiv{position:absolute;top:0;left:0;width:200px;height:200px;}
.img.innerimg{width:100%;height:100%;}
.img.innerdiv:last-child{opacity:0;}
</style>
</head>
<body>
<divstyle="width:620px;margin:40pxauto0auto;">
<divclass="img">
<divclass="inner">
<div><imgsrc="image/1-1.jpg"/></div>
<div><imgsrc="image/1-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/2-1.jpg"/></div>
<div><imgsrc="image/2-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/3-1.jpg"/></div>
<div><imgsrc="image/3-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/4-1.jpg"/></div>
<div><imgsrc="image/4-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/1-1.jpg"/></div>
<div><imgsrc="image/1-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/2-1.jpg"/></div>
<div><imgsrc="image/2-2.jpg"/></div>
</div>
</div>
</div>
<scripttype="text/javascript">
$(".img").mouseenter(function(){
var$this=$(this);
var$div=$this.find(".innerdiv");
$div.eq(1).stop();
$div.eq(1).css({"top":"0px","left":"0px","width":"200px","height":"200px"});
$div.eq(0).stop().animate({"top":"-25px","left":"-25px","width":"250px","height":"250px","opacity":"0"},500);
$div.eq(1).stop().animate({"top":"-25px","left":"-25px","width":"250px","height":"250px","opacity":"1"},500);
}).mouseleave(function(){
var$this=$(this);
var$div=$this.find(".innerdiv");
$div.eq(0).stop().animate({"top":"0","left":"0","width":"200px","height":"200px","opacity":"1"},500);
$div.eq(1).stop().animate({"top":"0","left":"0","width":"200px","height":"200px","opacity":"0"},500);
});
</script>
</body>
</html>
在head中添加:
<script>
window.onload=
function
(){
var
img=document.getElementById(
"imgTest"
);
if
(document.addEventListener){
img.addEventListener(
"mouseover"
,doMouseover,
false
);
img.addEventListener(
"mouseout"
,doMouseout,
false
);
}
else
if
(document.attachEvent){
img.attachEvent(
"mouseover"
,doMouseover);
img.attachEvent(
"mouseout"
,doMouseout);
}
else
{
img.onmouseover=doMouseover;
img.onmouseout=doMouseout;
}
}
function
doMouseover(){
this
.width=
this
.width*1.5;
this
.height=
this
.height*1.5;
}
function
doMouseout(){
this
.width=
this
.width/1.5;
this
.height=
this
.height/1.5;
}
</script>
注意doMouseout函数中不可写作绝对大小:
this
.width=
50px
;
this
.height=
50px
;
图片标签:
<imgid="imgTest"src="imgs/icon.jpg"/>
法二:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""
<htmlxmlns="
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title></title>
<scripttype="text/javascript"src="js/jquery-1.11.1.min.js"></script>
<styletype="text/css">
body{background:#000;}
.img{float:left;width:200px;height:200px;overflow:hidden;margin:01px1px0;}
.img.inner{position:relative;}
.img.innerdiv{position:absolute;top:0;left:0;width:200px;height:200px;}
.img.innerimg{width:100%;height:100%;}
.img.innerdiv:last-child{opacity:0;}
</style>
</head>
<body>
<divstyle="width:620px;margin:40pxauto0auto;">
<divclass="img">
<divclass="inner">
<div><imgsrc="image/1-1.jpg"/></div>
<div><imgsrc="image/1-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/2-1.jpg"/></div>
<div><imgsrc="image/2-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/3-1.jpg"/></div>
<div><imgsrc="image/3-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/4-1.jpg"/></div>
<div><imgsrc="image/4-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/1-1.jpg"/></div>
<div><imgsrc="image/1-2.jpg"/></div>
</div>
</div>
<divclass="img">
<divclass="inner">
<div><imgsrc="image/2-1.jpg"/></div>
<div><imgsrc="image/2-2.jpg"/></div>
</div>
</div>
</div>
<scripttype="text/javascript">
$(".img").mouseenter(function(){
var$this=$(this);
var$div=$this.find(".innerdiv");
$div.eq(1).stop();
$div.eq(1).css({"top":"0px","left":"0px","width":"200px","height":"200px"});
$div.eq(0).stop().animate({"top":"-25px","left":"-25px","width":"250px","height":"250px","opacity":"0"},500);
$div.eq(1).stop().animate({"top":"-25px","left":"-25px","width":"250px","height":"250px","opacity":"1"},500);
}).mouseleave(function(){
var$this=$(this);
var$div=$this.find(".innerdiv");
$div.eq(0).stop().animate({"top":"0","left":"0","width":"200px","height":"200px","opacity":"1"},500);
$div.eq(1).stop().animate({"top":"0","left":"0","width":"200px","height":"200px","opacity":"0"},500);
});
</script>
</body>
</html>
相关文章推荐
- 通过url,获取html内容,并解析
- 浅谈HTML移动Web开发(转)
- 有关HTML的相关基础问题:
- HTML简单实例
- power designer导出word或html
- IE版本判断
- 从html传到后台转成utf-8
- html的布局
- html容器与元素代码编写时的缩进
- HTML DOM的基本知识
- <转载>HTMl转义字符大全
- html中的alt和title用法区别
- ODOO Unable To Find Wkhtmltopdf On This System. Error/Bug ?
- html中<a href> </a>的用法
- html HTML 文本格式化
- HTML初学者适用的十五条最佳实践
- HTML标签(按功能排序)
- HTML文件的基本结构
- 动态生成的html绑定click事件没有效果不执行 click事件重复绑定问题
- html 复习