当鼠标移动到图片上时,图片有上下波动的效果。感觉还不错,大家可以运行下看效果!
其实主要是运用了position以及top的CSS改变来让图片产生动画效果。
好了贴代码了!
<!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=gb2312" />
<title>jjs图片鼠标移上去的波动效果-幸凡在线学习网</title>
<style>
.clear{height:0;display:none;font-size:0;clear:both;zoom:1;overflow:hidden;}
.clearfix:after {content:".";display:block;height:0;clear:both;visibility:hidden;}
*html .clearfix{height:1%;}
*+html .clearfix{height:1%;}
.clearfix{display:inline-block;}
.clearfix {display:block;}
#imgtest{padding:10px;background:#fff;}
#imgtest p{float:left;width:166px;height:163px;margin:0 47px 8px 10px;text-align:center;background:url(http://ok22.org/upload/images/20110926111724675.gif) no-repeat right 0;position:relative;}
#imgtest p img{position:relative;border:1px solid #ddd;margin-top:7px;}
#imgtest a:hover{background-position: left 0;}
#imgtest a:hover img{border:1px solid #ff9703;}
#imgtest .hover{background-position: left 0;}
#imgtest .hover img{border:1px solid #ff9703;}
</style>
<body>
<div id="imgtest" class="clearfix">
<p><img width="148" height="96" src="http://www.86y.org/upload/images/2014115162412_2010021523181215.gif" /></p>
<p><img width="148" height="96" src="http://www.86y.org/upload/images/2014115162412_2010021523181215.gif" /></p>
<p><img width="148" height="96" src="http://www.86y.org/upload/images/2014115162412_2010021523181215.gif" /></p>
<p><img width="148" height="96" src="http://www.86y.org/upload/images/2014115162412_2010021523181215.gif" /></p>
</div>
</body>
<script>
function JumpObj(elem, range,div,startFunc, endFunc) {
var curMax = range = range || 6;
startFunc = startFunc || function(){};
endFunc = endFunc || function(){};
var drct = 0;
var step = 1;
init();
function init() { elem.style.position = ''relative'';active() }
function active() {elem.onmouseover = function(e) {div.className="hover";if(!drct)jump();out();} }
function out() {elem.onmouseout=function(edf) {div.className="";}}
function deactive() {elem.onmouseover = null}
function jump() {
var t = parseInt(elem.style.top);
if (!drct) motionStart();
else {
var nextTop = t - step * drct;
if (nextTop >= -curMax && nextTop <= 0) elem.style.top = nextTop + ''px'';
else if(nextTop < -curMax) drct = -1;
else {
var nextMax = curMax / 2;
if (nextMax < 1) {motionOver();return;}
curMax = nextMax;
drct = 1;
}
}
setTimeout(function(){jump()}, 200 / (curMax+3) + drct * 3);
}
function motionStart() {
startFunc.apply(this);
elem.style.top=''0'';
drct = 1;
}
function motionOver() {
endFunc.apply(this);
curMax = range;
drct = 0;
elem.style.top = ''0'';
}
this.jump = jump;
this.active = active;
this.deactive = deactive;
}
var dis=document.getElementById("imgtest").getElementsByTagName(''img'');
var div=document.getElementById("imgtest").getElementsByTagName(''p'');
for(i=0;i<dis.length;i++)
{
new JumpObj(dis[i],10,div[i]);
}
</script>
</html>
完
大家有什么问题或技术上的想法可以在此与大家分享,也可以加入前端爱好者QQ群(141999928)一起学习进步:
【幸凡前端技术交流群】
如果您觉得本文的内容对您的学习有所帮助,捐赠与共勉,支付宝(左)或微信(右)