js可以实现定时器功能,本文主要和大家分享js定时器+简单的动画效果实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助大家用js做出一个更好的定时器。
1.向下滑动
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>向下滑动</title>
<style>
body {
margin: 0px;
}
#show {
width: 200px;
/* 高度为 0 */
height: 100px;
background-color: lightcoral;
margin: 0 auto;
/* 设置为隐藏 */
/*display: none;*/
}
</style>
</head>
<body>
<p id="show"></p>
<script>
var show = document.getelementbyid('show');
/*show.style.display = 'block';
var t = setinterval(function(){
var style = window.getcomputedstyle(show,null);
var height = parseint(style.height);
// 判断当前的高度是否为 400
if (height >= 400){
clearinterval(t);
} else {
height++;
show.style.height = height + 'px';
}
},50);*/
slidedown(show,400);
/*
将上述实现的向下滑动效果,封装在一个固定的函数中
* 设计当前实现向下滑动效果函数的形参
* elem - 表示实现向下滑动效果的元素
* maxheight - 表示元素向下滑动的最大高度值
* 函数的逻辑与默认设置css样式属性的值无关
*/
function slidedown(elem, maxheight){
// 操作的元素默认的display值为none
elem.style.display = 'block';
elem.style.height = '0px';
var t = setinterval(function(){
var style = window.getcomputedstyle(elem,null);
var height = parseint(style.height);
// 判断当前的高度是否为 400
if (height >= maxheight){
clearinterval(t);
} else {
height++;
elem.style.height = height + 'px';
}
},50);
}
</script>
</body>
</html>
2.移动效果
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>移动效果</title>
<style>
body {
margin: 0px;
}
#box {
width: 100px;
height: 100px;
background-color: lightcoral;
position: absolute;
left: 100px;
top: 100px;
}
</style>
</head>
<body>
<p id="box"></p>
<script>
var box = document.getelementbyid('box');
box.onclick = function(){
clearinterval(t);
}
/*
* 向右移动
* 当前元素移动到页面的最右边时 -> 向左移动
* 向左移动
* 当前元素移动到页面的最左边时 -> 向右移动
*/
var flag = false;// 默认表示向右
var speed = 1;// 表示每次变化的值
t = setinterval(function(){
//speed += 0.01;
// 获取当前页面的宽度
var width = window.innerwidth;
var style = window.getcomputedstyle(box,null);
var left = parseint(style.left);
var width = parseint(style.width);
// 判断当前元素移动的方向
if (flag){// 向左移动
left -= speed;
} else {// 向右移动
left += speed;
}
// 判断什么情况下,向左移动;判断什么情况下,向右移动
if ((left + width) >= width){// 向左移动
flag = true;
} else if (left <= 0){// 向右移动
flag = false;
}
box.style.left = left + 'px';
},10);
</script>
</body>
</html>
3.事件与动画结合
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>事件与动画结合</title>
<style>
body {
margin: 0px;
}
</style>
</head>
<body>
<script>
// 获取<body>元素
var body = document.body;
// 当页面加载完毕后,设置当前<body>元素的高度为当前浏览器窗口的高度
window.onload = function(){
setheight(body);
};
// 当用户改变浏览器窗口的大小时,重新设置<body>元素的高度(等于当前窗口的高度)
window.onresize = function(){
setheight(body);
};
// 定义函数 - 设置<body>元素的高度等于当前窗口的高度
function setheight(elem){
elem.style.height = window.innerheight + 'px';
}
var width = 100,height = 100;
// 为<body>元素绑定click事件
body.onclick = function(event){
var x = event.clientx;
var y = event.clienty;
// 创建<p>元素,显示的位置在鼠标当前的坐标值
var p = document.createelement('p');
p.setattribute('class','circle');
body.appendchild(p);
// rgb(0,0,0)格式 -> 颜色随机
var r = parseint(math.random()*255);
var g = parseint(math.random()*255);
var b = parseint(math.random()*255);
p.style.width = width + 'px';
p.style.height = height + 'px';
p.style.backgroundcolor = 'rgb('+r+','+g+','+b+')';
p.style.borderradius = '50%';
p.style.opacity = 1;
p.style.position = 'absolute';
p.style.left = x - width/2 + 'px';
p.style.top = y - height/2 + 'px';
animate(p);
}
// 定义函数 -> 实现动画效果
function animate(elem){
var style = window.getcomputedstyle(elem,null);
/*var width = parseint(style.width);
var height = parseint(style.height);
var left = parseint(style.left);
var top = parseint(style.top);
width++;
height++;
elem.style.width = width + 'px';
elem.style.height = height + 'px';
elem.style.left = (left - 0.5) + 'px';
elem.style.top = (top - 0.5) +'px';*/
var opacity = style.opacity;
if (opacity <= 0){
cleartimeout(t);
// 删除当前元素
}
opacity -= 0.01;
elem.style.opacity = opacity;
// 设定定时器
var t = settimeout(function(){
animate(elem);
},50);
}
</script>
</body>
</html>
相关推荐:
javascript中定时器的设置与清除详解
关于javascript中定时器的原理解析
javascript中如何创建和使用高级定时器实例代码详解
以上就是js实现简单动画效果的定时器的详细内容。