<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body,ul,li{
margin: 0;
padding:0;
}
ul,li{
list-style: none;
}
ul li{
width: 200px;
height: 100px;
background: yellow;
border:4px solid #000000;
filter: alpha(opacity:30);
opacity: 0.3;
}
</style>
<script src="js/move.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
window.onload = function(){
var oLi = document.getElementById('li1');
oLi.onmouseover = function(){
starMove(oLi,{width:400,height:200});
}
// oLi.onmouseout = function(){
// starMove(oLi,'width',200);
// }
}
</script>
</head>
<body>
<ul>
<li id="li1"></li>
</ul>
</body>
</html>
//移动函数1
// starMove(obj,{attr1:iTarget1,attr2:iTarget2},fn);
function starMove(obj,json,fn){
clearInterval(obj.timer); //清除定时器,避免重复生成多个定时器
obj.timer = setInterval(function(){
for(var attr in json){
var icur = 0;
if(attr =='opacity'){
//round() 四舍五入
icur = Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icur = parseInt(getStyle(obj,attr));
}
var speed = (json[attr]-icur)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(icur == json[attr]){
clearInterval(obj.timer);
if(fn){
fn();
}
}else{
if(attr == 'opacity'){
//IE
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];
}
}
引入外部js文件的时候不需要 charset="utf-8",这是<meta>中用来说明编码方式的