jQuery定義背景動態轉場效果的方法,jquery定義動態切換

來源:互聯網
上載者:User

jQuery定義背景動態轉場效果的方法,jquery定義動態切換

本文執行個體講述了jQuery定義背景動態轉場效果的方法。分享給大家供大家參考。具體如下:

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

(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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.