1、匀速运动
例子1:分享到
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 200px;
background: olive;
position: absolute;
left: -100px;
top: 100px;
}
#div2{
width: 30px;
height: 60px;
background: olivedrab;
position: absolute;
top: 70px;
right: -30px;
text-align: center;
color: #fff;
display: table;
}
#div2 p{
display: table-cell;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="div1">
<div id="div2">
<p>分享到</p>
</div>
</div>
<script type="text/javascript">
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var iTimer=null;
oDiv1.onmouseover=function(){
startMove(this,0,10)
}
oDiv1.onmouseout=function(){
startMove(this,-100,-10)
}
function startMove(obj,target,ispeed){
clearInterval(iTimer);//先清定时器,防止连续多次点击,速度叠加
iTimer=setInterval(function(){
if (obj.offsetLeft==target) {//运动停止的条件
clearInterval(iTimer);
}else{
obj.style.left=obj.offsetLeft+ispeed+'px';//开始运动
}
},30)
}
</script>
</body>
</html>
2、透明度
例子2:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#img1{
opacity: 0.3;
filter: alpha(opacity=30);
}
</style>
</head>
<body>
<img src="img/1.jpg" id="img1" />
<script type="text/javascript">
var oImg=document.getElementById("img1");
var iTimer=null;
oImg.onmouseover=function(){
startMove2(this,100,10);
}
oImg.onmouseout=function(){
startMove2(this,30,-10);
}
function startMove2(obj,target,ispeed){
clearInterval(iTimer);
var iCur=0;//定义一个代表透明度的变量
iTimer=setInterval(function(){
iCur=Math.round(css(obj,'opacity')*100); //非标准ie同样能取到opacity的值,同时解决小数精度问题
if (iCur==target) {
clearInterval(iTimer);
}else{
obj.style.opacity=( iCur+ispeed )/100; //分别给opacity和filter赋值
obj.style.filter='alpha(opacity='+(iCur+ispeed)+')';
}
},30)
}
function css(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle;
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</body>
</html>
3、多物体的运动
例子3:前面例子1和例子2的结合
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 200px;
background: olive;
position: absolute;
left: -100px;
top: 100px;
}
#div2{
width: 30px;
height: 60px;
background: olivedrab;
position: absolute;
top: 70px;
right: -30px;
text-align: center;
color: #fff;
display: table;
}
#div2 p{
display: table-cell;
vertical-align: middle;
}
#img1{
margin-left: 200px;
opacity: 0.3;
filter: alpha(opacity=30);
}
</style>
</head>
<body>
<div id="div1">
<div id="div2">
<p>分享到</p>
</div>
</div>
<img src="img/1.jpg" id="img1" />
<script type="text/javascript">
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var oImg=document.getElementById("img1");
//var iTimer=null;
oDiv1.onmouseover=function(){
startMove(this,'left',0,10)
}
oDiv1.onmouseout=function(){
startMove(this,'left',-100,-10)
}
oImg.onmouseover=function(){
startMove(this,'opacity',100,10);
}
oImg.onmouseout=function(){
startMove(this,'opacity',30,-10);
}
function startMove(obj,attr,target,ispeed){
clearInterval(obj.iTimer); //用obj.iTimer代替iTimer防止多个运动物体共用一个定时器而相互影响
var iCur=0;
obj.iTimer=setInterval(function(){
if (attr=='opacity') {
iCur=Math.round(css(obj,'opacity')*100);
}else{
iCur=parseInt( css(obj,attr) ); //css函数取出来带单位,所以要去掉单位
}
if (iCur==target) {
clearInterval(obj.iTimer);
}else{
if (attr=='opacity') {
obj.style.opacity=( iCur+ispeed )/100;
obj.style.filter='alpha(opacity='+(iCur+ispeed)+')';
}else{
obj.style[attr]=iCur+ispeed+'px';
}
}
},30)
}
function css(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle;
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</body>
</html>
4、多属性同时运动
例子4:(注意物体的多个属性并非同时到达目标点)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#div1{
width: 100px;
height: 100px;
background: olive;
position: absolute;
left: 100px;
top: 100px;
}
</style>
</head>
<body>
<div id="div1"></div>
<script type="text/javascript">
var oDiv1=document.getElementById("div1");
oDiv1.onclick=function(){
startMove(this,{
width:200,
height:300
},10);
}
function startMove(obj,json,ispeed){
clearInterval(obj.iTimer);
var iCur=0;
obj.iTimer=setInterval(function(){
var iBtn=true;
for (var attr in json) {
if (attr=='opacity') {
iCur=Math.round(css(obj,'opacity')*100);
}else{
iCur=parseInt( css(obj,attr) );
}
var target=json[attr];
if (iCur!=target) {
iBtn=false;
if (attr=='opacity') {
obj.style.opacity=( iCur+ispeed )/100;
obj.style.filter='alpha(opacity='+(iCur+ispeed)+')';
}else{
obj.style[attr]=iCur+ispeed+'px';
}
}
}
if (iBtn) {
clearInterval(obj.iTimer);
}
},30)
}
function css(obj,attr){
if (obj.currentStyle) {
return obj.currentStyle;
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</body>
</html>