Skip to content

Commit ee90752

Browse files
authored
Create move(属性改变 封装).html
1 parent f42283b commit ee90752

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

move(属性改变 封装).html

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<!Doctype HTML>
2+
<html>
3+
<head>
4+
<meta charset='utf-8'>
5+
<title></title>
6+
<style>
7+
div{
8+
width: 50px;
9+
height: 50px;
10+
float: left;
11+
background: red;
12+
margin-top: 50px;
13+
border: 1px solid black;
14+
font-size: 14px;
15+
}
16+
#div4{
17+
filter:alpha(opacity:20);
18+
opacity: 0.2;
19+
}
20+
</style>
21+
</head>
22+
<body>
23+
<div id=""></div>
24+
<div id=""></div>
25+
<div id="">currentStyle</div>
26+
<div id=""></div>
27+
<div id="div4"></div>
28+
</body>
29+
<script>
30+
window.onload = function ()
31+
{
32+
var oDiv = document.getElementsByTagName('div');
33+
// oDiv1.onmouseover = function (){
34+
// startMove(300);
35+
// }
36+
// oDiv1.onmouseout = function (){
37+
// startMove(100);
38+
// }
39+
// var i =0;
40+
// for(i=0; i<oDiv.length; i++)
41+
// {
42+
43+
// oDiv[i].timer=null;
44+
// oDiv[i].onmouseover = function ()
45+
// {
46+
// startMove(this, 'width',100);
47+
// }
48+
// oDiv[i].onmouseout = function ()
49+
// {
50+
// startMove(this, 'width' ,50);
51+
// }
52+
// }
53+
oDiv[0].onclick=function()
54+
{
55+
startMove(this,'width',300);
56+
57+
}
58+
oDiv[1].onclick=function()
59+
{
60+
startMove(this, 'height', 300);
61+
}
62+
oDiv[2].onclick=function()
63+
{
64+
startMove(this, 'fontSize', 100);
65+
}
66+
oDiv[3].onclick=function()
67+
{
68+
startMove(this, 'borderWidth', 50);
69+
}
70+
oDiv[4].onmouseover=function()
71+
72+
{
73+
74+
startMove(this, 'opacity', 100);
75+
}
76+
oDiv[4].onmouseout=function()
77+
{
78+
startMove(this, 'opacity', 20);
79+
}
80+
81+
}
82+
// var timer = null;
83+
84+
function startMove(obj,attr,iTarget)/*运动框架主函数*/
85+
{
86+
clearInterval(obj.timer);
87+
obj.timer = setInterval(function()
88+
{
89+
var iCur=0;
90+
if(attr=='opacity')
91+
{
92+
iCur=parseInt(parseFloat(getStyle(obj, attr))*100);
93+
}
94+
else
95+
{
96+
iCur=parseInt(getStyle(obj, attr));
97+
}
98+
var iSpeed = (iTarget - iCur)/8;//获取速度 = (目标点 - 当前位置)/x
99+
iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
100+
101+
if(iCur == iTarget)
102+
{
103+
clearInterval(obj.timer);
104+
}
105+
else
106+
{
107+
if(attr=='opacity')
108+
{
109+
obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+' )';/*字符串链接 有优先级*/
110+
obj.style.opacity=(iCur+iSpeed)/100;
111+
}
112+
else
113+
{
114+
obj.style[attr] = iCur + iSpeed +'px';
115+
}
116+
}
117+
},50)
118+
}
119+
function getStyle(obj, attr)/*获取对象 属性值*/
120+
{
121+
122+
if(obj.currentStyle)
123+
{
124+
return obj.currentStyle[attr];
125+
}
126+
else
127+
{
128+
return getComputedStyle(obj, false)[attr];
129+
}
130+
131+
}
132+
133+
</script>
134+
</html>

0 commit comments

Comments
 (0)