JQuery method for defining the dynamic Switching Effect of background _ jquery

Source: Internet
Author: User
This article mainly introduces jQuery's method for defining the dynamic Switching Effect of background, and analyzes jQuery's image operation skills for reference, for more information, see the examples in this article to describe 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.

(function($){ var defaultSettings; var pfg, pbg; 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) pfg = $(this);   if(c == 1) pbg = $(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];  pfg.css({   backgroundImage: "url('"+image1+"')",   zIndex: defaultSettings.fgz,   backgroundPosition: defaultSettings.postion  });  pbg.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];  pbg.css({   backgroundImage: "url('"+image+"')"  });  setTimeout(function(){nextFade()}, defaultSettings.frequency); } // Run a fade function nextFade(){  fadeInterval = setInterval(function(){fadeIt()}, 30); } // Decrement the opacity of the p function fadeIt(){  if(pfg.css("opacity") == ''){   op = 1;  }else{   op = pfg.css("opacity");  }  op -= ((1000 * defaultSettings.speed) / 30) * 0.0001;  pfg.css("opacity", op);  if(op <= 0){   bg = pbg;   bgimg = pbg.css("background-image");   pfg.css("opacity", "1");   pfg.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.