DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> jQuery定義背景動態切換效果的方法
jQuery定義背景動態切換效果的方法
編輯:JavaScript綜合知識     

 這篇文章主要介紹了jQuery定義背景動態切換效果的方法,實例分析了jQuery操作圖片的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

   

本文實例講述了jQuery定義背景動態切換效果的方法。分享給大家供大家參考。具體如下:

通過下面的jQuery插件,你可以將圖片放在一個數組裡,然後告訴jQuery圖片需要在什麼地方背景輪換

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

希望本文所述對大家的jQuery程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved