javascript多物体运动实现方法分析
作者:bea
本文实例讲述了javascript多物体运动实现方法。分享给大家供大家参考,具体如下: 这里需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用。 运行效果截图如下: 例子: <!doctype html><html><head><meta charset="utf-8"><title>多物体运动</title><style>div{ width:1
本文实例讲述了javascript多物体运动实现方法。分享给大家供大家参考,具体如下:
这里需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用。
运行效果截图如下:
例子:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>多物体运动</title>
<style>
div{ width:100px; height:100px; background:red; float:left; margin:10px; border:1px solid black; opacity:0.3; filter:alpha(opacity=30);}
</style>
<script>
window.onload = function()
{
var aDiv = document.getElementsByTagName('div');
aDiv[0].onmouseover = function()
{
startMove(this, 'width', 300);
};
aDiv[0].onmouseout = function()
{
startMove(this, 'width', 100);
};
aDiv[1].onmouseover = function()
{
startMove(this, 'height', 300);
};
aDiv[1].onmouseout = function()
{
startMove(this, 'height', 100);
};
aDiv[2].onmouseover = function()
{
startMove(this, 'opacity', 100);
};
aDiv[2].onmouseout = function()
{
startMove(this, 'opacity', 30);
};
aDiv[3].onmouseover = function()
{
startMove(this, 'borderWidth', 20);
};
aDiv[3].onmouseout = function()
{
startMove(this, 'borderWidth', 1);
};
};
function getStyle(obj, attr)
{
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}
function startMove(obj, attr, iTarget)
{
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iCur = 0;
if(attr == 'opacity'){
iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100);
}else{
iCur = parseInt(getStyle(obj, attr));
}
var iSpeed = (iTarget - iCur) / 8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur == iTarget){
clearInterval(obj.timer);
}else{
if(attr == 'opacity'){
obj.style.filter = 'alpha(opacity='+ (iCur+iSpeed) +')';
obj.style.opacity = (iCur+iSpeed)/100;
}else{
obj.style[attr] = iCur + iSpeed + 'px';
}
}
}, 30);
}
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
更多关于JavaScript运动效果相关内容可查看本站专题:《JavaScript运动效果与技巧汇总》
希望本文所述对大家JavaScript程序设计有所帮助。
有用 | 无用
这里需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用。
运行效果截图如下:
例子:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>多物体运动</title>
<style>
div{ width:100px; height:100px; background:red; float:left; margin:10px; border:1px solid black; opacity:0.3; filter:alpha(opacity=30);}
</style>
<script>
window.onload = function()
{
var aDiv = document.getElementsByTagName('div');
aDiv[0].onmouseover = function()
{
startMove(this, 'width', 300);
};
aDiv[0].onmouseout = function()
{
startMove(this, 'width', 100);
};
aDiv[1].onmouseover = function()
{
startMove(this, 'height', 300);
};
aDiv[1].onmouseout = function()
{
startMove(this, 'height', 100);
};
aDiv[2].onmouseover = function()
{
startMove(this, 'opacity', 100);
};
aDiv[2].onmouseout = function()
{
startMove(this, 'opacity', 30);
};
aDiv[3].onmouseover = function()
{
startMove(this, 'borderWidth', 20);
};
aDiv[3].onmouseout = function()
{
startMove(this, 'borderWidth', 1);
};
};
function getStyle(obj, attr)
{
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}
function startMove(obj, attr, iTarget)
{
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iCur = 0;
if(attr == 'opacity'){
iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100);
}else{
iCur = parseInt(getStyle(obj, attr));
}
var iSpeed = (iTarget - iCur) / 8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur == iTarget){
clearInterval(obj.timer);
}else{
if(attr == 'opacity'){
obj.style.filter = 'alpha(opacity='+ (iCur+iSpeed) +')';
obj.style.opacity = (iCur+iSpeed)/100;
}else{
obj.style[attr] = iCur + iSpeed + 'px';
}
}
}, 30);
}
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
更多关于JavaScript运动效果相关内容可查看本站专题:《JavaScript运动效果与技巧汇总》
希望本文所述对大家JavaScript程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- js父页面中使用子页面的方法
- jquery调整表格行tr上下顺序实例讲解
- 实例讲解js验证表单项是否为空的方法
- 小心!AngularJS结合RequireJS做文件合并压缩的那些坑
- javascript跑马灯抽奖实例讲解
- jQuery页面刷新(局部、全部)问题分析
- javascript返回顶部的按钮实现方法
- javascript类型系统 Array对象学习笔记
- JS中生成随机数的用法及相关函数
- JavaScript电子时钟倒计时
- bootstrap-treeview自定义双击事件实现方法
- WordPress中利用AJAX异步获取评论用户头像的方法
- 学习JavaScript设计模式之状态模式
- jQuery CSS3相结合实现时钟插件
- js实现对ajax请求面向对象的封装
- javascript弹性运动效果简单实现方法
- javascript运动效果实例总结(放大缩小、滑动淡入、滚动)
- javascript运动框架用法实例分析(实现放大与缩小效果)
- jquery实现简单的遮罩层