(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 ); |