本文实例讲述了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程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“jQuery定义背景动态切换效果的方法”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?