Javascript 带有平滑交叉淡入的循环jQuery幻灯片
我试图在主页上做一个简单的旋转图像。在引擎盖下,我读取一个目录,然后将图像的URL填充到一个数组中。我想做的是交叉淡入淡出图像。如果只是为了展示下一个,这很容易,但因为我需要交叉淡入,所以有点难。我想我要做的是通过调用Javascript 带有平滑交叉淡入的循环jQuery幻灯片,javascript,jquery,animation,slideshow,Javascript,Jquery,Animation,Slideshow,我试图在主页上做一个简单的旋转图像。在引擎盖下,我读取一个目录,然后将图像的URL填充到一个数组中。我想做的是交叉淡入淡出图像。如果只是为了展示下一个,这很容易,但因为我需要交叉淡入,所以有点难。我想我要做的是通过调用的opacity值上的animate()来进行淡入淡出 您应该注意的第一件事,这肯定会导致代码出现问题:动画方法不同步!所以当你这样做的时候: <div id="home-slideshow"><img src="http://example.com/wordpr
的opacity
值上的animate()
来进行淡入淡出
您应该注意的第一件事,这肯定会导致代码出现问题:动画方法不同步!所以当你这样做的时候:
<div id="home-slideshow"><img src="http://example.com/wordpress/wp-content/themes/testtheme/sidebar-home-bg/bg1.jpg" alt="" /></div>
开始设置动画,但该方法立即返回。您可以将动画想象为一个背景线程,尽管JavaScript中没有线程,所有内容都是使用settimeout调用实现的
因此,在您的代码中,在更改src属性时,图像可能仍然99%可见。然后你开始将其动画设置回100%不透明度,但此时它仍然是98%,两个“线程”将尝试同时使其出现/消失
因此,在您的代码中,您需要设置超时以正确的顺序执行任务(始终在中间留出几毫秒的余量),或者,当您有许多连续的函数调用时,使用animate方法的回调函数更安全,但可读性可能较低。例如:
$('#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]);
最后,你要做的是淡出+淡入。如果你想要一个真正的交叉淡入淡出,你需要在某个点同时有两个元素:
您应该注意的第一件事,这肯定会导致代码出现问题:动画方法是不同步的!所以当你这样做的时候:
<div id="home-slideshow"><img src="http://example.com/wordpress/wp-content/themes/testtheme/sidebar-home-bg/bg1.jpg" alt="" /></div>
开始设置动画,但该方法立即返回。您可以将动画想象为一个背景线程,尽管JavaScript中没有线程,所有内容都是使用settimeout调用实现的
因此,在您的代码中,在更改src属性时,图像可能仍然99%可见。然后你开始将其动画设置回100%不透明度,但此时它仍然是98%,两个“线程”将尝试同时使其出现/消失
因此,在您的代码中,您需要设置超时以正确的顺序执行任务(始终在中间留出几毫秒的余量),或者,当您有许多连续的函数调用时,使用animate方法的回调函数更安全,但可读性可能较低。例如:
$('#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]);
最后,你要做的是淡出+淡入。如果你想要一个真正的交叉淡入淡出,你需要在某个点同时有两个元素:
$('#home-slideshow img').animate({opacity: 0}, delay, function(){
// 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, function(){
// do it again
setTimeout('swapSlides()', 4000);
});
});
两行CSS
$(function(){
$('#home-slideshow img:gt(0)').hide();
setInterval(function(){
$('#home-slideshow :first-child').fadeOut()
.next('img').fadeIn()
.end().appendTo('#home-slideshow');},
3000);
});
您的HTML
#home-slideshow { position:relative; height:332px; width:500px; }
#home-slideshow img { position:absolute; left:0; top:0; }
...
...
试试这个:
演示:
jQuery的几行
$('#home-slideshow img').animate({opacity: 0}, delay, function(){
// 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, function(){
// do it again
setTimeout('swapSlides()', 4000);
});
});
两行CSS
$(function(){
$('#home-slideshow img:gt(0)').hide();
setInterval(function(){
$('#home-slideshow :first-child').fadeOut()
.next('img').fadeIn()
.end().appendTo('#home-slideshow');},
3000);
});
您的HTML
#home-slideshow { position:relative; height:332px; width:500px; }
#home-slideshow img { position:absolute; left:0; top:0; }
...
...