Animate Divs One Right After the Other with Jquery, Part 2

和自甴很熟 提交于 2019-12-12 04:14:56

问题


Im basically trying to load a page that has a set of widgets that are supposed to fade in animate one after the other when the nav button for that page is clicked and the page is loaded, and then fade out animate, before going to the next page, when another nav button is clicked.

The fade in animation is not smooth...the widgets dont fade in animate right after the other is finished animating the way Im pushing for... outside of the first 2 widgets, which do animate correctly. The rest however, start animating at the same time, just more slowly than the widget before it, based on my timing settings.

I also cant get the fadeout portion of the animation to play when I click to go to the next page.

Please keep in mind that the page with the widgets, is being loaded into a div on the parent page, so basically the widgets are on their own seperate html page (widgets.html) if you will.

How can I solve this issue?

My Navigation Links on the parent page:

<!--The dynamic load part loads the outside page into the div-->    
<ul id="Navbar">
       <li><a href="Page1.html" class="dynamicLoad">Page1</a></li>
       <li><a href="Page2.html" class="dynamicLoad">Page2</a></li>
       <li><a href="Page3.html" class="dynamicLoad">Page3</a></li>
       <li><a href="Page4.html" class="dynamicLoad">Page4</a></li>
    </ul>

The animation code on the widget page:

//Declare FadeIn Animation Rules
    <script>
    function animateIn(divId, callback) {
        $(divId).animate(
            {opacity:1.0},
            700,
            callback);
    }

    function animateIn1(divId, callback) {
        $(divId).animate(
            {opacity:1.0},
            1400,
            callback);
    }

    function animateIn2(divId, callback) {
        $(divId).animate(
            {opacity:1.0},
            2100,
            callback);
    }



    //Declare FadeOut Animation Rules

    function animateOut(divId, callback) {
        $(divId).animate(
            {opacity:0},
            700,
            callback);
    }

    function animateOut1(divId, callback) {
        $(divId).animate(
            {opacity:0},
            1400,
            callback);
    }

    function animateOut2(divId, callback) {
        $(divId).animate(
            {opacity:0},
            2100,
            callback);
    }





    //Start FadeIn Animation

    $("#MyWidget").animate(
        {opacity:1.0}, 300, 
        function() {
            animateIn1("#MyWidget1");
            animateIn2("#MyWidget2");
            animateIn3("#MyWidget3");
            animateIn4("#MyWidget4");
        }
    );


    //Start FadeOut Animation

    $("#next-page").click(function($event) {
        $event.preventDefault();
        var href = $(this).attr("href");

        animateOut4("#MyWidget4");
        animateOut3("#MyWidget3");
        animateOut2("#MyWidget2");
        animateOut1("#MyWidget1", function() {
           animateOut("#MyWidget");
       });
    });


    </script>

回答1:


I would suggest chaining the fades with callbacks instead of time offsets, perhaps something like this: http://jsfiddle.net/p8gj8/6/




回答2:


Use the ready() function:

$(document).ready(function() {
  // your functions
});

That way the animations will not run while the page loads.

Alternatively you could try:

$(window).load(function() {
      // your functions
});


来源:https://stackoverflow.com/questions/4516171/animate-divs-one-right-after-the-other-with-jquery-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!