在制作静态页面中,少不了要学习一些动态的效果。例如图片的跳跃、鼠标移入移出、图表的变化的一些效果等等许多。
如果页面中有许多的效果,每做一个效果都想一个流程的话会很麻烦,所以,将动画效果的框架做一个整理就是很有必要的了。今天就是将一些动画框架做一个整理,方便使用。
运动中,无非改变的是width、left、height、right、opacity等等值,总结如下,有几种框架。
一、一般运动
代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运动框架</title>
<style>
body{
padding: 0px;
margin: 0px;
}
.box{
width: 200px;
height: 100px;
left: -200px;
background: #003EFF;
position: relative;
}
.share{
position: absolute;
background: #122B40;
color: white;
display: block;
width: 20px;
height: 50px;
left: 200px;
top: 20px;
}
</style>
<script>
window.οnlοad=function(){
var oBox=document.getElementById('box');
oBox.οnmοuseοver=function(){
move(0);
}
oBox.οnmοuseοut=function(){
move(-200);
}
}
var time=null;
function move(targat){
var oBox=document.getElementById('box');
var speed=0;
if (oBox.offsetLeft<targat) {
speed=10;
} else{
speed=-10;
}
clearInterval(time);
time=setInterval(function(){
if(oBox.offsetLeft==targat){
clearInterval(time);
}else{
oBox.style.left=oBox.offsetLeft+speed+'px';
}
},30);
}
</script>
</head>
<body>
<div class="box" id="box">
<span class="share" id="share">分享</span>
</div>
</body>
</html>
简单物体移入移出
总结框架:是单一的某个属性的匀速运动
clearinterval(time);
time=setinterval(function(){
if(变化属性==目标值){
clearInterval(time);
}else{
速度变化
}
},30)
二、缓冲运动
分析:距离越大速度越大,距离越小速度越小。成正比
代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运动框架</title>
<style>
body{
padding: 0px;
margin: 0px;
}
.box{
width: 200px;
height: 100px;
left: -200px;
background: #003EFF;
position: relative;
}
.share{
position: absolute;
background: #122B40;
color: white;
display: block;
width: 20px;
height: 50px;
left: 200px;
top: 20px;
}
</style>
<script>
window.οnlοad=function(){
var oBox=document.getElementById('box');
oBox.οnmοuseοver=function(){
move(0);
}
oBox.οnmοuseοut=function(){
move(-200);
}
}
var time=null;
function move(targat){
var oBox=document.getElementById('box');
clearInterval(time);
time=setInterval(function(){
var speed=(targat-oBox.offsetLeft)/15;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(oBox.offsetLeft==targat){
clearInterval(time);
}else{
oBox.style.left=oBox.offsetLeft+speed+'px';
}
},30);
}
</script>
</head>
<body>
<div class="box" id="box">
<span class="share" id="share">分享</span>
</div>
</body>
</html>
三、多物体运动
代码(多物体单属性):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运动框架</title>
<style>
body{
padding: 0px;
margin: 0px;
}
ul,li{
list-style: none;
}
ul li{
width: 200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
}
</style>
<script>
window.οnlοad=function(){
var aLi=document.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
aLi[i].timer=null;
aLi[i].οnmοuseοver=function(){
move(this,300);
}
aLi[i].οnmοuseοut=function(){
move(this,200);
}
}
}
function move(obj,target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var speed=(target-obj.offsetWidth)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==target){
clearInterval(obj.timer);
}else{
obj.style.width=obj.offsetWidth+speed+'px';
}
},30);
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
总结:
多物体的变量都不能公用。可以写成当前物体。
获取行间样式
function getStyle(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else{
return obj.getComputedStyle(obj,false)[attr];
}
}
*代码(多物体多属性):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运动框架</title>
<style>
body{
padding: 0px;
margin: 0px;
}
ul,li{
list-style: none;
}
ul li{
width: 200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
border: 2px solid #000000;
}
</style>
<script>
window.οnlοad=function(){
var aLi=document.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
aLi[i].timer=null;
aLi[i].οnmοuseοver=function(){
move(this,300);
}
aLi[i].οnmοuseοut=function(){
move(this,200);
}
}
}
function move(obj,target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var icur=parseInt(getStyle(obj,'width'));
var speed=(target-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==target){
clearInterval(obj.timer);
}else{
obj.style.width=icur+speed+'px';
}
},30);
}
function getStyle(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
这种多属性变化也不会互相影响
四、任意值运动
分析:将属性作为参数传入
代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运动框架</title>
<style>
body{
padding: 0px;
margin: 0px;
}
ul,li{
list-style: none;
}
ul li{
width: 200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
border: 2px solid #000000;
}
</style>
<script>
window.οnlοad=function(){
var aLi=document.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
aLi[i].timer=null;
aLi[i].οnmοuseοver=function(){
move(this,'width',300);
}
aLi[i].οnmοuseοut=function(){
move(this,'width',200);
}
}
}
function move(obj,attr,target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var icur=parseInt(getStyle(obj,attr));
var speed=(target-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==target){
clearInterval(obj.timer);
}else{
obj.style[attr]=icur+speed+'px';
}
},30);
}
function getStyle(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
有一种属性例外,那就是透明度。首先是parseInt,其次是‘px’的地方有问题。可以用if判断一下。以下对透明度修改地方做出了标注。
代码(透明度):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>运动框架</title>
<style>
body{
padding: 0px;
margin: 0px;
}
ul,li{
list-style: none;
}
ul li{
width: 200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
border: 2px solid #000000;
opacity: 0.3;
filter:alpha(opacity:30);
}
</style>
<script>
window.οnlοad=function(){
var aLi=document.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
aLi[i].timer=null;
aLi[i].οnmοuseοver=function(){
move(this,'opacity',100);
}
aLi[i].οnmοuseοut=function(){
move(this,'opacity',30);
}
}
}
function move(obj,attr,target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
if(attr=='opacity'){
var icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
var icur=parseInt(getStyle(obj,attr));
}
var speed=(target-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==target){
clearInterval(obj.timer);
}else{
if(attr=='opacity'){
obj.style.filter='alpha:(opacity:'+icur+speed+')';
obj.style.opacity=(icur+speed)/100;
}else{
obj.style[attr]=icur+speed+'px';
}
}
},30);
}
function getStyle(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
五、链式运动
六、同时运动
总结时,再次注意到的问题:
1.position属性问题relative和absolute
2.oBox.style.left和oBox.offsetLeft区别
3.Math函数floor()、ceil()、round()