Javascript模拟加速运动与减速运动代码分享
作者:bea
加速运动,即一个物体运动时速度越来越快;减速运动,即一个物体运动时速度越来越慢。现在用Javascript来模拟这两个效果,原理就是用setInterval或setTimeout动态改变一个元素与另外一个元素的距离,如xxx.style.left或xxx.style.marginLeft,然后每次运动后都使速度增加,这样加速运动的效果就出现了,减速运动是同样的道理。 下面是两个示例: 加速运动 代码如下: <!DOCTYPE html> <html&
加速运动,即一个物体运动时速度越来越快;减速运动,即一个物体运动时速度越来越慢。现在用Javascript来模拟这两个效果,原理就是用setInterval或setTimeout动态改变一个元素与另外一个元素的距离,如xxx.style.left或xxx.style.marginLeft,然后每次运动后都使速度增加,这样加速运动的效果就出现了,减速运动是同样的道理。
下面是两个示例:
加速运动
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Javascript加速运动</title>
<style type="text/css">
* {margin: 0; padding: 0;}
.div1 {width: 100px; height: 100px; background: #f60 url(qiuweiguan.gif) no-repeat center center;}
</style>
<script type="text/javascript">
var $$ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
var oBtn = $$("btn1");
var oDiv = $$("div1");
var timer = null;
var speed = 0;
oBtn.onclick = function () {
clearInterval(timer);
timer = setInterval(function () {
speed ++;
oDiv.style.marginLeft = oDiv.offsetLeft + speed + "px";
}, 30);
}
}
</script>
</head>
<body id = "body">
<button id="btn1" class="btn1">GO</button>
<div id="div1" class="div1"></div>
</body>
</html>
注:本示例中,点击GO后,div块会一直向右做加速运动
减速运动
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Javascript减速运动</title>
<style type="text/css">
* {margin: 0; padding: 0;}
.div1 {width: 100px; height: 100px; background: #f60 url(qiuweiguan.gif) no-repeat center center;}
</style>
<script type="text/javascript">
var $$ = function (id) {
return document.getElementById(id);
}
window.onload = function () { var oBtn = $$("btn1"); var oDiv = $$("div1"); var timer = null; var speed = 30; oBtn.onclick = function () { clearInterval(timer); timer = setInterval(function () { speed — ; oDiv.style.marginLeft = oDiv.offsetLeft + speed + "px"; }, 30); } } </script> </head> <body id = "body"> <button id="btn1" class="btn1">GO</button> <div id="div1" class="div1"></div> </body> </html>
注:本示例中,点击GO后,div块会一直向右做减速运动,直到速度减为零后,速度变为负值,再向左做加速运动
有用 | 无用
下面是两个示例:
加速运动
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Javascript加速运动</title>
<style type="text/css">
* {margin: 0; padding: 0;}
.div1 {width: 100px; height: 100px; background: #f60 url(qiuweiguan.gif) no-repeat center center;}
</style>
<script type="text/javascript">
var $$ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
var oBtn = $$("btn1");
var oDiv = $$("div1");
var timer = null;
var speed = 0;
oBtn.onclick = function () {
clearInterval(timer);
timer = setInterval(function () {
speed ++;
oDiv.style.marginLeft = oDiv.offsetLeft + speed + "px";
}, 30);
}
}
</script>
</head>
<body id = "body">
<button id="btn1" class="btn1">GO</button>
<div id="div1" class="div1"></div>
</body>
</html>
注:本示例中,点击GO后,div块会一直向右做加速运动
减速运动
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Javascript减速运动</title>
<style type="text/css">
* {margin: 0; padding: 0;}
.div1 {width: 100px; height: 100px; background: #f60 url(qiuweiguan.gif) no-repeat center center;}
</style>
<script type="text/javascript">
var $$ = function (id) {
return document.getElementById(id);
}
window.onload = function () { var oBtn = $$("btn1"); var oDiv = $$("div1"); var timer = null; var speed = 30; oBtn.onclick = function () { clearInterval(timer); timer = setInterval(function () { speed — ; oDiv.style.marginLeft = oDiv.offsetLeft + speed + "px"; }, 30); } } </script> </head> <body id = "body"> <button id="btn1" class="btn1">GO</button> <div id="div1" class="div1"></div> </body> </html>
注:本示例中,点击GO后,div块会一直向右做减速运动,直到速度减为零后,速度变为负值,再向左做加速运动
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- javascript里使用php代码实例
- jQuery使用height()获取高度需要注意的地方
- js与css实现弹出层覆盖整个页面的方法
- jQuery对于显示和隐藏等常用状态的判断方法
- js QQ客服悬浮效果实现代码
- 重写document.write实现无阻塞加载js广告(补充)
- js中document.write的那点事
- 让javascript加载速度倍增的方法(解决JS加载速度慢的问题)
- jQuery实现瀑布流布局
- jquery+ajax验证不通过也提交表单问题处理
- js 左右悬浮对联广告代码示例
- 原生JavaScript+LESS实现瀑布流
- jquery禁止回车触发表单提交
- 完美兼容各大浏览器的jQuery插件实现图片切换特效
- windows8.1+iis8.5下安装node.js开发环境
- jQuery 和 CSS 的文本特效插件集锦
- js使用递归解析xml
- 做web开发 先学JavaScript
- 兼容主流浏览器的JS复制内容到剪贴板