【发布时间】:2011-02-13 20:47:19
【问题描述】:
我正在尝试在主页上做一个简单的旋转图像。在后台,我正在读取一个目录,然后将图像的 url 填充到一个数组中。我想做的是交叉淡化图像。如果只是显示下一个的问题,这很容易,但由于我需要交叉淡入淡出,所以有点难。我想我想做的是通过在<img> 标记的opacity 值上调用animate() 来进行淡入淡出,并在换出封闭<div> 的css background-image 属性之间。但结果并不是那么好。
我已经使用工具制作更全功能的幻灯片,但如果可以避免的话,我不希望添加插件的开销,而且简单的淡入淡出似乎应该更容易。
这是我的 JavaScript(我使用的是 jQuery 1.3.2):
var slideshow_images = ["http:\/\/example.com\/wordpress\/wp-content\/themes\/testtheme\/sidebar-home-bg\/bg1.jpg","http:\/\/example.com\/wordpress\/wp-content\/themes\/testtheme\/sidebar-home-bg\/bg2.jpg","http:\/\/example.com\/wordpress\/wp-content\/themes\/testtheme\/sidebar-home-bg\/bg3.jpg"];
var slideshow_index = 0;
var delay = 4000;
var swapSlides = function() {
var slideshow_count = slideshow_images.length;
// initialize the background to be the current image
$('#home-slideshow').css({
'background-image': 'url(' + slideshow_images[slideshow_index] + ')',
'background-repeat:': 'no-repeat',
'width': 200,
'overflow': 'hidden'
});
slideshow_index = ((slideshow_index + 1) == slideshow_count) ? 0 : slideshow_index + 1;
// fade out the img
$('#home-slideshow img').animate({opacity: 0}, delay);
// now, the background is visible
// next change the url on the img
$('#home-slideshow img').attr('src', slideshow_images[slideshow_index]);
// and fade it up
$('#home-slideshow img').animate({opacity: 1.0}, delay);
// do it again
setTimeout('swapSlides()', 4000);
}
jQuery(document).ready(function(){
if (swapSlides) {
swapSlides();
}
});
这是我正在使用的标记:
<div id="home-slideshow"><img src="http://example.com/wordpress/wp-content/themes/testtheme/sidebar-home-bg/bg1.jpg" alt="" /></div>
【问题讨论】:
标签: javascript jquery animation slideshow