Jquery 逐个淡入淡出元素,如果超过限制则删除最后一个元素
问题 1: 我想淡化每个 href 或 img 标签,因为它们加载时有延迟,所以看起来好像它们是单独加载的。我不知道该使用哪个, delay()
、 fadeToggle()
或 fadeIn()
`
目前我使用代码得到了所有这些下面,将一次性显示一次,每 20 秒一次。任何重复的图像都不会显示,因为它们已经加载到数组中。
$(document).ready(function(){
var existingElements= new Array(); // array of existing images
setInterval(function(){
$.get("/helloworld", function(data){
data = $.parseJSON(data);
for(i = 0; i < data.length; i++){
if($.inArray(data[i]["id"], existingElements) == -1){
var imagelinks = '<a class="removethis" href="link">'
+'<img src="'+data[i]["img"]+'"/> </a>';
$(imagelinks).prependTo("#somediv").delay(300);
existingElements.push(data[i]["id"]);
}
/// add limit here, see below
}
});
}, 20000);
});
问题 2: div somediv
最多只能包含 20 张图像。所以如果json传入的图片超过20张(超过)。我想删除最后设置的图像,因为上面的代码总是会在 20 秒后引入新图像,我应该设置如下限制:
if(i > 20){
$("#somediv .removethis:last").fadeOut(); // should i use remove()
}
Question 1:
I want to fade each href or img tag as they are loaded with delay, so it looks as if they are loaded separately. I dont know which to use, delay()
, fadeToggle()
or fadeIn()
`
At moment i get all of them using the code below, which will display in one go, every 20 second. any duplicate images are not shown as they would already be loaded in the array.
$(document).ready(function(){
var existingElements= new Array(); // array of existing images
setInterval(function(){
$.get("/helloworld", function(data){
data = $.parseJSON(data);
for(i = 0; i < data.length; i++){
if($.inArray(data[i]["id"], existingElements) == -1){
var imagelinks = '<a class="removethis" href="link">'
+'<img src="'+data[i]["img"]+'"/> </a>';
$(imagelinks).prependTo("#somediv").delay(300);
existingElements.push(data[i]["id"]);
}
/// add limit here, see below
}
});
}, 20000);
});
Question 2:
The div somediv
can only have at max of 20 images. so if more than 20(exceed) images are brought in by json. i want to remove that last set images, since the above code will always bring in new images after 20 seconds, should i set a limit as follow:
if(i > 20){
$("#somediv .removethis:last").fadeOut(); // should i use remove()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情应该做:
Something like this should do: