js實作類別似jquery裡animate動畫效果的方法

來源:互聯網
上載者:User

js實作類別似jquery裡animate動畫效果的方法

 該執行個體可實現滑鼠移上,先寬度變化,再高度變化,最後透明度變化,滑鼠移出,再依次變回去的效果。

要點一:

1

2

3

4

5

6

7

8

startrun(obj,attr,target,fn)

box.onmouseover = function(){

startrun(box,"width",200,function(){

startrun(box,"height",200,function(){

startrun(box,"opacity","100")

});

});

}

如上面,函數也可以做為參數使用,就可以達到先執行某個動作,再執行某個動作的效果了。

要點二:

1

2

3

4

5

6

if(cur == target){

clearInterval(obj.timer);

if(fn){

fn();

}

}

當運動到達目標點,關閉定時器,然後就可以執行新的函數了。

最後,上代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>無標題文檔</title>

<style>

<!--

body{margin:0; padding:0; font:12px/1.5 arial;}

#box{width:100px; height:100px; position:absolute;

background:#06c; left:0;filter:alpha(opacity=30); opacity:0.3;}

-->

</style>

<script>

<!--

function getstyle(obj,name){

if(obj.currentStyle){

return obj.currentStyle[name];

}else{

return getComputedStyle(obj,false)[name];

}

}

window.onload = function(){

var box = document.getElementById("box");

box.onmouseover = function(){

startrun(box,"width",200,function(){

startrun(box,"height",200,function(){

startrun(box,"opacity","100")

});

});

}

box.onmouseout = function(){

startrun(box,"height",100,function(){

startrun(box,"width",100,function(){

startrun(box,"opacity","30");

});

});

}

}

function startrun(obj,attr,target,fn){

clearInterval(obj.timer);

obj.timer = setInterval(function(){

var cur = 0;

if(attr == "opacity"){

cur = Math.round(parseFloat(getstyle(obj,attr))*100);

}else{

cur = parseInt(getstyle(obj,attr));

}

var speed = (target-cur)/8;

speed = speed>0?Math.ceil(speed):Math.floor(speed);

 

if(cur == target){

clearInterval(obj.timer);

if(fn){

fn();

}

}else{

if(attr == "opacity"){

obj.style.filter = "alpha(opacity="+(cur+speed)+")";

obj.style.opacity = (cur+speed)/100;

}else{

obj.style[attr] = cur + speed + "px";

}

}

},30)

}

//-->

</script>

</head>

<body>

<div id="box">

</div>

</body>

</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.