JS 动画
程序员文章站
2024-01-29 21:44:16
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JS动画案例</title>
<meta charset="utf-8"/>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
*{
margin:0;
padding:0;
}
#move{
padding:10px;
width:300px;
background:#ccc;
margin:10px auto;
border:1px solid #000;
}
#move a{
display:inline-block;
width:58px;
height:40px;
border:1px solid #ddd;
border-radius:3px;
background-color:#fff;
text-align:center;
margin:10px 17px;
position:relative;
padding-top:40px;
color:#9c9c9c;
font-size:12px;
text-decoration:none;
line-height:25px;
overflow:hidden;
}
#move a i{
position:absolute;
top:20px;
left:0;
display:inline-block;
width:100%;
text-align:center;
filter:alpha(opacity=100);
opacity:1;
}
#move a:hover{
color:#f00;
}
#move img{
border:none;
}
</style>
<script type="text/javascript" src="js/jsonmove.js"></script>
<script type="text/javascript">
window.onload = function(){
//通过改变时间值,可以改变动画的速度;
var oMove = document.getElementById('move');
var aList = oMove.getElementsByTagName('a');
for(var i=0;i<aList.length;i++){
aList[i].onmouseover = function(){
var icon = this.getElementsByTagName('i')[0];
startMove(icon,{top:-25,opacity:0},function(){
icon.style.top=30+'px';
startMove(icon,{top:20,opacity:100});
});
}
}
}
</script>
</head>
<body>
<div id="move">
<a href=""><i><img src="images/caipiao.png" alt="" /></i><p>彩票</p></a>
<a href=""><i><img src="images/movie.png" alt="" /></i><p>电影</p></a>
<a href=""><i><img src="images/food.png" alt="" /></i><p>外卖</p></a>
<a href=""><i><img src="images/game.png" alt="" /></i><p>游戏</p></a>
<a href=""><i><img src="images/insure.png" alt="" /></i><p>保险</p></a>
<a href=""><i><img src="images/travel.png" alt="" /></i><p>旅行</p></a>
</div>
</body>
</html>
js/jsonmove.js
//从样式表中根据id和属性名取值
function getstyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name];
}else{
return getComputedStyle(obj,false)[name];
}
}
//fn是一个函数,当完成任务时,没有马上结束,而失去执行fn函数(回调函数)
//fn表示结束一个运动后,然后执行另一个运动,
//attr,target表示JSON中的一对值
//startMove(obj,{attr1:targte1,attr2:targte2},fn)
function startMove(obj,json,fn){
var flag=true; //假设所有属性都能达到目标值
clearInterval(obj.timer);
obj.timer = setInterval(function(){
for(var attr in json){
//1.取当前的值
var cur = 0;
if(attr == 'opacity'){
cur = Math.round(parseFloat(getstyle(obj,attr))*100);
}else{
cur = parseInt(getstyle(obj,attr));
}
//2.算速度
var speed = (json[attr] - cur)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
//3.检测停止
//要判断所有目标到达终点
if(cur != json[attr]){
//如果某一属性没有达到目标值,则置假
flag = false;
}
if(attr == 'opacity'){
obj.style.filter = 'aplha(opacity:'+(cur+speed)+')';//IE
obj.style.opacity = (cur+speed)/100; //chrome或火狐
}else{
obj.style[attr] = cur+speed+'px';
}
}
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30);
}
<html>
<head>
<title>JS动画案例</title>
<meta charset="utf-8"/>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
*{
margin:0;
padding:0;
}
#move{
padding:10px;
width:300px;
background:#ccc;
margin:10px auto;
border:1px solid #000;
}
#move a{
display:inline-block;
width:58px;
height:40px;
border:1px solid #ddd;
border-radius:3px;
background-color:#fff;
text-align:center;
margin:10px 17px;
position:relative;
padding-top:40px;
color:#9c9c9c;
font-size:12px;
text-decoration:none;
line-height:25px;
overflow:hidden;
}
#move a i{
position:absolute;
top:20px;
left:0;
display:inline-block;
width:100%;
text-align:center;
filter:alpha(opacity=100);
opacity:1;
}
#move a:hover{
color:#f00;
}
#move img{
border:none;
}
</style>
<script type="text/javascript" src="js/jsonmove.js"></script>
<script type="text/javascript">
window.onload = function(){
//通过改变时间值,可以改变动画的速度;
var oMove = document.getElementById('move');
var aList = oMove.getElementsByTagName('a');
for(var i=0;i<aList.length;i++){
aList[i].onmouseover = function(){
var icon = this.getElementsByTagName('i')[0];
startMove(icon,{top:-25,opacity:0},function(){
icon.style.top=30+'px';
startMove(icon,{top:20,opacity:100});
});
}
}
}
</script>
</head>
<body>
<div id="move">
<a href=""><i><img src="images/caipiao.png" alt="" /></i><p>彩票</p></a>
<a href=""><i><img src="images/movie.png" alt="" /></i><p>电影</p></a>
<a href=""><i><img src="images/food.png" alt="" /></i><p>外卖</p></a>
<a href=""><i><img src="images/game.png" alt="" /></i><p>游戏</p></a>
<a href=""><i><img src="images/insure.png" alt="" /></i><p>保险</p></a>
<a href=""><i><img src="images/travel.png" alt="" /></i><p>旅行</p></a>
</div>
</body>
</html>
js/jsonmove.js
//从样式表中根据id和属性名取值
function getstyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name];
}else{
return getComputedStyle(obj,false)[name];
}
}
//fn是一个函数,当完成任务时,没有马上结束,而失去执行fn函数(回调函数)
//fn表示结束一个运动后,然后执行另一个运动,
//attr,target表示JSON中的一对值
//startMove(obj,{attr1:targte1,attr2:targte2},fn)
function startMove(obj,json,fn){
var flag=true; //假设所有属性都能达到目标值
clearInterval(obj.timer);
obj.timer = setInterval(function(){
for(var attr in json){
//1.取当前的值
var cur = 0;
if(attr == 'opacity'){
cur = Math.round(parseFloat(getstyle(obj,attr))*100);
}else{
cur = parseInt(getstyle(obj,attr));
}
//2.算速度
var speed = (json[attr] - cur)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
//3.检测停止
//要判断所有目标到达终点
if(cur != json[attr]){
//如果某一属性没有达到目标值,则置假
flag = false;
}
if(attr == 'opacity'){
obj.style.filter = 'aplha(opacity:'+(cur+speed)+')';//IE
obj.style.opacity = (cur+speed)/100; //chrome或火狐
}else{
obj.style[attr] = cur+speed+'px';
}
}
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30);
}