[英]Applying javascript function to multiple div tags
I have a html page that as on the index page 16 boxes, Inside on box 3 other boxes are there, I want to interchange the inner boxes periodically: One box represented as: 我有一个html页面,与索引页面上的16个框一样,在框3上还有其他框,我想定期交换内部框:一个框表示为:
<div class="box" id="Index_one">
<div class="Divbox" id="Box_three"><img src='' /><p class="overlap"></p></div>
<div class="Divbox" id="Box_two"><img src='' /><p class="overlap"> </p></div>
<div class="Divbox" id="Box_one"><img src='' /><p class="overlap"> </p></div>
</div>
As stated before there are 16 of this kind of boxes I have a javascript function that can adequately switch through if it is called once on one box Javascript function is: 如前所述,有16种此类框,我有一个javascript函数,如果在一个框上调用一次,则可以充分切换该Javascript函数为:
function Animation(Id_value) {
/*Image Changer for Index_two*/
var Id_num = Id_value
var x = 0
var Images = new Array("Box_one", "Box_two", "Box_three");
one = setInterval(function() {
var Id = "div#" + Id_num + "> div#" + Images[x]
$(Id).fadeOut(1000);
x++
if (x==2){
clearInterval(one)
two = setInterval(function() {
x--
var Di = "div#" + Id_num + "> div#" + Images[x]
$(Di).fadeIn(1000);
if (x==0) {
clearInterval(two)
three = setInterval(function() {
var Iv = "div#" + Id_num + "> div#" + Images[x]
$(Iv).fadeOut(1000);
x++
if (x==2){
clearInterval(three);
four = setInterval(function() {
x--
var Vi = "div#" + Id_num + "> div#" + Images[x]
$(Vi).fadeIn(1000);
if (x==0) {
clearInterval(four)
five = setInterval(function() {
var Dv = "div#" + Id_num + "> div#" + Images[x]
$(Dv).fadeOut(1000);
x++
if (x==2) {
clearInterval(five)
six = setInterval(function() {
x--
var Vd = "div#" + Id_num + "> div#" + Images[x]
$(Vd).fadeIn(1000);
}, 1000)
}
}, 1000)
}
}, 1000)
}
}, 1000);
}
}, 1000);
}
}, 1000);
/*End of Image Changer*/
};
It does it 3 times, Instead of writing this kind of function custom for all my 16 boxes which i consider redundant, I did this 它执行了3次,而不是为我认为多余的所有16个框编写这种函数自定义,而是这样做了
$(document).ready(function() {
Animation("Index_one");
Animation("Index_two");
Animation("Index_three");
Animation("Index_four");
Animation("Index_five");
Animation("Index_six");
Animation("Mid_one");
Animation("Mid_two");
Animation("Mid_three");
Animation("Mid_four");
Animation("bottom_one");
Animation("bottom_two");
Animation("bottom_three");
Animation("bottom_four");
Animation("bottom_five");
Animation("bottom_six");
}
Thinking this should work but did not according to how the function was written, please any help explained properly would be appreciated. 认为这应该可行,但与函数的编写方式不符,请您对任何正确解释的帮助表示赞赏。
Something like this: 像这样:
// FIRST OPTION
var divlist = ["Index_one","Index_two","Index_three","Index_four","Index_five","Index_six","Mid_one","Mid_two","Mid_three","Mid_four","bottom_one","bottom_two","bottom_three","bottom_four","bottom_five","bottom_six"];
for (div in divlist) {
Animation(divlist[div]);
}
// SECOND OPTION
// (having in mind that box class is applied only on targeted divs
$('.box').each(function() {
Animation(this.id);
});
Second option is using jQuery each function. 第二种选择是使用jQuery的每个函数。 It first finds all of div tags with box class and then iterates over them.
它首先找到具有box类的所有div标签,然后对其进行迭代。 During the iteration, each of the found div is called in Animation(this).
在迭代过程中,每个找到的div在Animation(this)中都被调用。
You can use jQuery.delay()
to animate elements sequentially. 您可以使用
jQuery.delay()
顺序对元素进行动画处理。
<input type="button" id="fadeAllOut" value="Fade all out!" />
<div class="box" id="Index_one">
<div class="Divbox" id="Box_three">
<img src='' />
<p class="overlap"></p>
</div>
<div class="Divbox" id="Box_two">
<img src='' />
<p class="overlap"></p>
</div>
<div class="Divbox" id="Box_one">
<img src='' />
<p class="overlap"></p>
</div>
<!-- as many as you need
<div class="Divbox" id="Box_one">
<img src='' />
<p class="overlap"></p>
</div>
-->
</div>
$(document).on("click", "#fadeAllOut", function (e) {
var elems = $(".Divbox");
//variable for accumulating delay
var delay = 0;
//needed duration
var duration = 500;
$.each(elems, function (index, element) {
$(element).delay(delay).fadeOut(duration);
/*or if you want just hide but not remove elements from layout, use this:
.animate({
opacity: 0
}, duration);*/
//accumulate delay
delay += duration;
});
});
It is just a starting point, trying to understand your requirements 这只是一个起点,试图了解您的要求
I think, from what it looks like, that the way you called the function (each time with a different id) didn't work because they were starting asynchronously and there for getting entangled in each other's animation. 从外观上看,我认为您调用函数的方式(每次使用不同的id)都行不通,因为它们是异步启动的,并在那里纠缠了对方的动画。 You can use a recursion and call the next function from within the function itself.
您可以使用递归并从函数本身内部调用下一个函数。 I also replaced your
setInterval
and clearInterval
calls with setTimeout
, this make the code a little cleaner and more readable (and even a bit more efficient). 我还用
setTimeout
替换了setInterval
和clearInterval
调用,这使代码更简洁,更易读(甚至更有效率)。 NOTE: I've replaced also the sixth (the last) call to setInterval
with setTimeout
although you don't clear that one (which is a bug, I think). 注意:我也用
setTimeout
替换了对setInterval
的第六个(最后一个)调用,尽管您不清楚那个(我认为这是一个错误)。
var elements = ["Index_one","Index_two","Index_three","Index_four","Index_five","Index_six","Mid_one","Mid_two","Mid_three","Mid_four","bottom_one","bottom_two","bottom_three","bottom_four","bottom_five","bottom_six"];
function Animation(listIndex, elementList) {
/*Image Changer for Index_two*/
var Id_value = elementList[listIndex];
var Id_num = Id_value
var x = 0
var Images = new Array("Box_one", "Box_two", "Box_three");
one = setTimeout(function() {
var Id = "div#" + Id_num + "> div#" + Images[x]
$(Id).fadeOut(1000);
x++
if (x==2){
two = setTimeout(function() {
x--
var Di = "div#" + Id_num + "> div#" + Images[x]
$(Di).fadeIn(1000);
if (x==0) {
three = setTimeout(function() {
var Iv = "div#" + Id_num + "> div#" + Images[x]
$(Iv).fadeOut(1000);
x++
if (x==2){
four = setTimeout(function() {
x--
var Vi = "div#" + Id_num + "> div#" + Images[x]
$(Vi).fadeIn(1000);
if (x==0) {
five = setTimeout(function() {
var Dv = "div#" + Id_num + "> div#" + Images[x]
$(Dv).fadeOut(1000);
x++
if (x==2) {
six = setTimeout(function() {
x--
var Vd = "div#" + Id_num + "> div#" + Images[x]
$(Vd).fadeIn(1000, function(){
Animation(++listIndex, elementList); // This will call this function with the next index but only when the fadein animation will finish.
});
}, 1000)
}
}, 1000)
}
}, 1000)
}
}, 1000);
}
}, 1000);
}
}, 1000);
/*End of Image Changer*/
};
Animation(0, elements) // Call the Animation function with the first element index to start it all off
Also, to clean up your code a little, instead of using setInterval (or setTimeout) you can pass the next fadein function as a call back to the jquery fadein method, as the second argument (the complete argument) which will be called when the fadein animation is complete. 另外,为了稍微清理代码,您可以不使用setInterval(或setTimeout),而是将下一个fadein函数作为对jquery fadein方法的调用,作为第二个参数(complete参数)在调用时调用。淡入淡出动画已完成。 Like I'v done for the last (six) call to fadein where I call the Animation function with the next element id).
就像我对fadein的最后一个(六个)调用所做的一样,其中我用下一个元素id调用了Animation函数。 You can read about it in jQuery's
fadein
docs: http://api.jquery.com/fadein/ 您可以在jQuery的
fadein
文档中阅读有关它的fadein
: http : fadein
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.