Javascript 在Jquery中更改动画后的图像Src
因此,我正在制作一个宝丽来幻灯片效果,我想在Jquery中的动画之后更改图像。我试着增加延迟,但也许我做得不对 这是我的代码,也许你可以帮我找到一种添加过渡图像的方法 Jquery HTMLJavascript 在Jquery中更改动画后的图像Src,javascript,jquery,html,animation,Javascript,Jquery,Html,Animation,因此,我正在制作一个宝丽来幻灯片效果,我想在Jquery中的动画之后更改图像。我试着增加延迟,但也许我做得不对 这是我的代码,也许你可以帮我找到一种添加过渡图像的方法 Jquery HTML <div id="polaroid" style="position:absolute ;width: 100%; height: -3000px; margin-top: 35px; opacity:0.0"> <br
<div id="polaroid" style="position:absolute ;width: 100%; height: -3000px; margin-top: 35px; opacity:0.0">
<br /> <br />
<div style="background-color: #EEEEEE; height:145%; width:40%; box-shadow: 0px 10px 5px #888888; display:inline-block; margin-left: 15%;">
<div id="slideshow">
<img id="centerIMG" src="images/cityscape.jpg" style=" margin-top:15px; width: 94%; margin-left:3%;" />
</div>
<div style=""> <br /> <br /> </div>
</div>
</div>
我希望在第三个动画之后添加开关,以便在动画完成后更改图像后调用函数。您只需使用第三个动画中的完成处理程序:
$(function () { // DOCUMENT ready shorthand
var imgs = ["src1.jpg", "src2.jpg", "src3.jpg", "src4.jpg"];
var cntr = 0;
function polariodSlideshow() {
$("#polaroid").animate({ opacity: 1, top: 80 }, 1500);
$("#polaroid").animate({ opacity: 0, top: 250 }, 1800);
$("#polaroid").animate({ opacity: 0, top: 0 }, 1900, function() {
// change the image here
var newImg = imgs[cntr % imgs.length];
++cntr;
// use newImg here
// then kick off the next iteration
polariodSlideshow();
});
}
polariodSlideshow();
});
如果新的image.src尚未缓存,那么您可能还需要添加等待加载的代码,或者更好,确保它已预缓存,这样就不会出现问题。成功了!如果我想更改多个图像,我该怎么做?@jfriend00@コードバリノ - 我不明白你的新问题。如果您想在代码中的那个位置更改多个图像,为什么不能在那里更改多个图像呢?对不起,我对Jquery还比较陌生,所以我想知道它是否会再次循环,src是否会继续朝着相同的图像更改?我想知道是否有一种方法可以在每次从回调@jfriend00返回时将SRC更改为不同的图像@コードバリノ - 您可以在
polariodSlideshow()
之外声明一个计数器,该计数器每次递增,并允许您每次拾取不同的图像。您可以将所有图像URL放入一个数组中,并使用计数器将其索引到数组中。老实说,将索引到数组部分弄糊涂了。我会做一些谷歌搜索来找出答案,因为我不想在问很多问题时打扰你@jfriend00
$(function () { // DOCUMENT ready shorthand
var imgs = ["src1.jpg", "src2.jpg", "src3.jpg", "src4.jpg"];
var cntr = 0;
function polariodSlideshow() {
$("#polaroid").animate({ opacity: 1, top: 80 }, 1500);
$("#polaroid").animate({ opacity: 0, top: 250 }, 1800);
$("#polaroid").animate({ opacity: 0, top: 0 }, 1900, function() {
// change the image here
var newImg = imgs[cntr % imgs.length];
++cntr;
// use newImg here
// then kick off the next iteration
polariodSlideshow();
});
}
polariodSlideshow();
});