简体   繁体   English

将JavaScript函数应用于多个div标签

[英]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()顺序对元素进行动画处理。

HTML: HTML:

<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>

JavaScript: JavaScript:

$(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;
    });
});

Here is the Demo . 这是演示

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替换了setIntervalclearInterval调用,这使代码更简洁,更易读(甚至更有效率)。 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文档中阅读有关它的fadeinhttp : fadein

如何添加多个<div>标签到 javascript object</div><div id="text_translate"><p> 我有几个对象,当我将它们添加到 innerHTML 时,我希望用一个“div”元素包含每个 object。 因此,“feelings”将包含在一组“div”元素中,而“feelings2”将包含在它自己的一组“div”元素中。 谁能帮我?</p><p> 下面是 javascript 代码中引用的 HTML</p><pre> <div class="tables"> <table > <tbody class="row" id="tableData"></tbody> </table></pre><p> 这 2 个是 object 样本。</p><pre> const feelings = [ { menuItem: happy[0], description: happy[1], value: 5 }, { menuItem: excited[0], description: excited[1], value: 6 }, { menuItem: sad[0], description: sad[1], value: 1 }, { menuItem: furious[0], description: furious[1], value: 0 } ]; const feelings2 = [ { menuItem: happier[0], description: happier[1], value: 5 }, { menuItem: excited2[0], description: excited2[1], value: 6 }, { menuItem: sadder[0], description: sadder[1], value: 1 }, { menuItem: furious2[0], description: furious2[1], value: 0 } ];</pre><p> 这是将对象数据添加到 innerHTML 的 javascript</p><pre> let tbody = '<tbody>'; function addToTableBody(feeling) { let returnVal = ""; for (const { menuItem, description, value } of Object.values(feeling)) { returnVal += ` <tr class="rows"> <td class="menuItem">${menuItem}</td> <div class="hide description">${description}</div> <td>${value}</td> </tr> ` } return returnVal; } tbody += addToTableBody(feelings) tbody += addToTableBody(feelings2); tbody += '</tbody>'; document.getElementById('tableData').innerHTML = tbody;</pre></div> - how to add multiple <div> tags to a javascript object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在div上应用JavaScript函数 - Applying a javascript function on a div javascript替换多个div标签 - javascript replace multiple div tags 将 Javascript 函数应用于多个按钮? - Applying a Javascript function to multiple buttons? 如何添加多个<div>标签到 javascript object</div><div id="text_translate"><p> 我有几个对象,当我将它们添加到 innerHTML 时,我希望用一个“div”元素包含每个 object。 因此,“feelings”将包含在一组“div”元素中,而“feelings2”将包含在它自己的一组“div”元素中。 谁能帮我?</p><p> 下面是 javascript 代码中引用的 HTML</p><pre> <div class="tables"> <table > <tbody class="row" id="tableData"></tbody> </table></pre><p> 这 2 个是 object 样本。</p><pre> const feelings = [ { menuItem: happy[0], description: happy[1], value: 5 }, { menuItem: excited[0], description: excited[1], value: 6 }, { menuItem: sad[0], description: sad[1], value: 1 }, { menuItem: furious[0], description: furious[1], value: 0 } ]; const feelings2 = [ { menuItem: happier[0], description: happier[1], value: 5 }, { menuItem: excited2[0], description: excited2[1], value: 6 }, { menuItem: sadder[0], description: sadder[1], value: 1 }, { menuItem: furious2[0], description: furious2[1], value: 0 } ];</pre><p> 这是将对象数据添加到 innerHTML 的 javascript</p><pre> let tbody = '<tbody>'; function addToTableBody(feeling) { let returnVal = ""; for (const { menuItem, description, value } of Object.values(feeling)) { returnVal += ` <tr class="rows"> <td class="menuItem">${menuItem}</td> <div class="hide description">${description}</div> <td>${value}</td> </tr> ` } return returnVal; } tbody += addToTableBody(feelings) tbody += addToTableBody(feelings2); tbody += '</tbody>'; document.getElementById('tableData').innerHTML = tbody;</pre></div> - how to add multiple <div> tags to a javascript object 按类将函数应用于div - Applying function to a div by class 为多个脚本标签定义JavaScript函数 - Define JavaScript function for multiple script tags JavaScript仅适用于一个div - JavaScript applying only to one div 使用JavaScript在跨多个div标签的网页中搜索文本 - Search for text in web page that spans multiple div tags using javascript 打印多个 <p> 内的标签 <div> 使用JavaScript - Printing Multiple <p> Tags within a <div> Using JavaScript 将变量应用于javascript函数 - Applying a variable to a javascript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM