使用 jQuery 在循环背景图像之间淡入淡出
我正在尝试获取旧 div 的背景图像(我的意思是它已经有一个背景图像,我无法控制它,因此必须首先覆盖)以在新图像之间无限期地平滑淡出。到目前为止,这是我所拥有的:
var images = [
"/images/home/19041085158.jpg",
"/images/home/19041085513.jpg",
"/images/home/19041085612.jpg"
];
var counter = 0;
setInterval(function() {
$(".home_banner").css('backgroundImage', 'url("'+images[counter]+'")');
counter++;
if (counter == images.length) {
counter = 0;
}
}, 2000);
问题是,它并不顺利(我的目标是像innerfade插件这样的东西)。
编辑:问题最初说“并且它不是不确定的(它只在数组中运行一次)。”,但马里奥纠正了我愚蠢的命名错误。
编辑2:我现在正在使用Reigel的答案(见下文),它工作得很好,但我仍然找不到任何方法可以在图像之间平滑地淡入淡出。
非常感谢所有帮助:)
I'm trying to get the background image of a legacy div (by which I mean it already has a background image, which I cannot control & thus have to initially over-write) to smoothly fade between new images indefinitely. Here's what I have so far:
var images = [
"/images/home/19041085158.jpg",
"/images/home/19041085513.jpg",
"/images/home/19041085612.jpg"
];
var counter = 0;
setInterval(function() {
$(".home_banner").css('backgroundImage', 'url("'+images[counter]+'")');
counter++;
if (counter == images.length) {
counter = 0;
}
}, 2000);
Trouble is, it's not smooth (I'm aiming for something like the innerfade plugin).
EDIT: question originally said "and it's not indefinite (it only runs once through the array).", but Mario corrected my stupid naming error.
EDIT2: I'm now using Reigel's answer (see below), which works perfectly, but I still can't find any way to fade between the images smoothly.
All help greatfully appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在设置backgroundImage之前预加载图像并替换
为
编辑:
图像之间的淡入淡出(注意我不是在谈论背景图像)是通过使用两个图像并更改不透明度直到一个不可见而另一个不可见来完成的。这意味着您需要在短时间内同时看到两个图像。如果您只使用一张背景图像,这是不可能的。
您可以尝试删除现有 div 上的背景图像
并将 img 元素放置在其后面(使用较低的 z-index)并淡入和淡出它们。您甚至可以在这些上使用内部淡入淡出插件,并让它为您处理淡入淡出和循环。
Pre-load the images before you set the backgroundImage and also replace
with
EDIT:
Fading between images (note I'm not talking background images) is done by using two images and changing the opacity until one is invisible and the other isn't. This means you need to have two images visible at the same time for a short while. This is not possible if you just use one background image.
You could try removing the background image on the existing div
and placing img elements behind it (using a lower z-index) and fade these in and out. You could probably even use the innerfade plugin on these and let that handle the fading and looping for you.
您需要首先加载图像,然后切换到它
jQuery 淡入淡出到新图像:
jQuery 淡入新图像
You need to first load the image then switch to it
jQuery fade to new image:
jQuery fade to new image
试试这个...
try this...
经过多次实验后,我被迫以否定的方式回答我自己的问题 - 即在这一点上我 99% 确定它无法完成。
我应该说我的情况并不理想,因为我试图淡化背景的 div 里面有其他 html 内容。如果没有,那么 Reigel 的代码和代码的组合Mario 的最后一个建议肯定会起作用,即:
尽管使用 jQuery 删除旧的 div 背景和样式仍然会更简单。然后将图像插入 div“前景”中。
不管怎样,留下这个以防其他人试图做同样的事情。另外我讨厌没有答案的问题:)
After much experimentation, I'm forced to answer my own question in the negative - i.e. at this point I'm 99% sure it can't be done.
I should say that my situation is not ideal in that the div that I'm trying to fade the background for has other html content inside it. If it didn't, then a combination of Reigel's code & Mario's last suggestion would certainly work, namely:
Though it would be simpler still to just use jQuery to remove the legacy div background & then insert images into the div 'foreground'.
Anyway, just leaving this in case someone else is trying to do the same thing. Plus I hate unanswered questions :)
myobj.stop()
结束循环。它未经测试,但与此类似的东西应该可以工作。myobj.stop()
ends the loop. It's untested but something simliar to this should work.只需在要更改背景的类的 CSS 中添加
-webkit-transition:1s;
即可。这对我来说效果很好。
Just put a
-webkit-transition:1s;
in your CSS for the class in which you are changing the background.That worked fine for me.