How jQuery defines the dynamic Switching Effect of background

Source: Internet
Author: User

How jQuery defines the dynamic Switching Effect of background

This article mainly introduces how jQuery defines the dynamic Switching Effect of the background. The example analyzes jQuery's image operation skills and has some reference value. For more information, see

 

 

This article describes how jQuery defines the dynamic Switching Effect of background. Share it with you for your reference. The details are as follows:

Using the jQuery plug-in below, you can put the image in an array and tell jQuery where the background rotation is needed.

?

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

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

(Function ($ ){

Var defaultSettings;

Var divfg, divbg;

Var fadeInterval;

Var fqTimer;

Var currImg = 0;

Var displImg = 0;

Var running = false;

// Setup settings and initialize the plugin

$. Fn. bgFade = function (settings, callback ){

DefaultSettings = $. extend ({

Frequency: 5000,

Speed: 10,

Images: [],

Position: "center ",

Fgz: 1,

Bgz: 0

}, Settings );

Var c = 0;

$ (This). each (function (){

If (c = 0) divfg = $ (this );

If (c = 1) divbg = $ (this );

C ++;

});

SetBackgrounds ();

If (typeof callback = "function "){

Callback ();

}

Return this;

};

// Start the fadder

$. Fn. start = function (){

FqTimer = setTimeout (function (){

NextFade ()}, defaultSettings. frequency

);

Running = true;

Return this;

};

// Stop the fadder

$. Fn. stop = function (){

ClearInterval (fadeInterval );

ClearTimeout (fqTimer );

Running = false;

Return this;

}

// Get the current image info {array id, image url}

$. Current = function (){

Return {pos: displImg, url: defaultSettings. images [displImg]}

}

// Set the first two backgrounds

Function setBackgrounds (){

Image1 = defaultSettings. images [0];

Image2 = defaultSettings. images [1];

Divfg.css ({

BackgroundImage: "url ('" + image1 + "')",

ZIndex: defaultSettings. fgz,

BackgroundPosition: defaultSettings. postion

});

Divbg.css ({

BackgroundImage: "url ('" + image2 + "')",

ZIndex: defaultSettings. bgz,

BackgroundPosition: defaultSettings. postion

});

CurrImg = 1;

DisplImg = 0;

}

// Set the next background after a fade completes

Function setNextBackground (){

Next = arrayNext ();

Image = defaultSettings. images [next];

Divbg.css ({

BackgroundImage: "url ('" + image + "')"

});

SetTimeout (function () {nextFade ()}, defaultSettings. frequency );

}

// Run a fade

Function nextFade (){

FadeInterval = setInterval (function () {fadeIt ()}, 30 );

}

// Decrement the opacity of the div

Function fadeIt (){

If(divfg.css ("opacity") = ''){

Op = 1;

} Else {

Op = divfg.css ("opacity ");

}

Op-= (1000 * defaultSettings. speed)/30) * 0.0001;

Divfg.css ("opacity", op );

If (op <= 0 ){

Bg = divbg;

Bgimg = divbg.css ("background-image ");

Divfg.css ("opacity", "1 ");

Divfg.css ("background-image", bgimg );

ClearInterval (fadeInterval );

SetNextBackground ();

DisplImg = arrayCurrent ();

}

}

// Get the next item in the array

Function arrayNext (){

Var next = currImg + 1;

If (next> = defaultSettings. images. length ){

Next = 0;

}

CurrImg = next;

Return next;

}

// Get the current item in the array

Function arrayCurrent (){

Var cur = currImg-1;

If (cur <0)

Cur = defaultSettings. images. length-1;

Return cur;

}

}) (JQuery );

I hope this article will help you with jQuery programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.