How to slide down a div then .fadeIn() the content and vice versa?

走远了吗. 提交于 2019-12-30 04:32:08

问题


Goal

When a user clicks the button, the div in question will:

  1. slide down
  2. stop
  3. fade in the content

When the user clicks the button again, the div will:

  1. fade out
  2. stop
  3. slide up

Current position

Here is an example where the fadeIn and fadeOut is happening at the right time but there is no slide effect before and after the fadeIn and fadeOut respectively
http://jsfiddle.net/tkRGU/1/

Also there is this option which has the slideToggle function but does not have the fadeIn and fadeOut occuring after and before the slide respectively.
http://jsfiddle.net/MY8DD/7/


回答1:


This will work:

HTML:

<a href="#" onclick="toggleSlider();">toggle</a>
<div id="panelThatSlides" style="display:none;background:#eee;padding:10px;">
    <div id="contentThatFades" style="opacity:0;filter:alpha(opacity=0);">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ut tortor  erat, et consectetur nisl. Nunc non placerat odio. Cras feugiat  pulvinar diam sed sollicitudin. Quisque ut elit lacus, et gravida nunc.  Maecenas ac enim ligula. Aenean felis nunc, vulputate pellentesque  vehicula nec, tristique a tortor. Curabitur et semper dui. Sed id nisl  turpis. Sed vel nunc et nisi laoreet feugiat. Sed lobortis enim sed arcu  tempor vehicula. Vivamus dui ligula, ultricies id egestas ut, rhoncus  et est. Pellentesque dignissim diam vel nibh tempus condimentum. Etiam  sodales fermentum pharetra. Etiam faucibus tempus malesuada. Mauris  nulla lectus, laoreet sit amet cursus vel, ultricies at enim. Sed  facilisis rutrum eros, nec malesuada eros iaculis ac.
        <br /><br />
        In consectetur faucibus fermentum. Pellentesque habitant morbi tristique  senectus et netus et malesuada fames ac turpis egestas. Cras nunc  magna, vestibulum eget pulvinar hendrerit, tincidunt id arcu. Nullam  dolor ligula, suscipit placerat condimentum ac, feugiat ut mauris.  Suspendisse semper dolor condimentum dui ornare rhoncus. In bibendum  massa vel erat tristique congue. Donec vel mi quam, ac iaculis odio.  Nulla interdum orci quis ligula aliquam viverra. Nam eget egestas  mauris. Sed in massa quis erat venenatis aliquam.
    </div>
</div>

Javascript:

function toggleSlider() {
    if ($("#panelThatSlides").is(":visible")) {
        $("#contentThatFades").animate(
            {
                opacity: "0"
            },
            600,
            function(){
                $("#panelThatSlides").slideUp();
            }
        );
    }
    else {
        $("#panelThatSlides").slideDown(600, function(){
            $("#contentThatFades").animate(
                {
                    opacity: "1"
                },
                600
            );
        });
    }   
}

Working example on JS Fiddle.

For IE just make sure there is a background color behind the content for cleartype.




回答2:


It sounds like since you want the two operations to occur simultaneously that you should use the animate function. Otherwise the actions will come one after another.

If you know the height of the element before running it, then you can set things fairly easily. Here's an extremely rough example: http://jsfiddle.net/tArQu/




回答3:


$("#button").toggle(function(){
    $("#content").slideDown().fadeIn();
    }, function(){
    $("#content").slideUp().fadeOut();
    return false;
});    

That what you're after?




回答4:


If you don't know the height of the element in advance, it is slightly more complicated. You have to animate the opacity directly to fade, and you must hide the content with CSS visibility while it is "sliding".

CSS visibility "hidden" allows content to occupy the space in the document it normally would, but to be hidden from view; CSS display "none" doesn't just hide the element, it removes it from the document flow. By hiding the element using visibility, we can slide it down until it is its full height, while the content of the element remains invisible.

Similarly, fading content in using jQuery's fadeIn function assumes an element is initially hidden with display "none", so it won't work if we use visibility. Instead, we make the element initially fully transparent (opacity 0.0); once the sliding animation is complete, we set visibility to "visible" and then animate the opacity from fully transparent to fully opaque (0.0 to 1.0).

Assuming the element is initially hidden (CSS display "none" or jQuery hide function):

$(element).css("visibility", "hidden").slideDown("slow", function() {
    $(this).css("opacity", 0.0).css("visibility", "visible").animate({
        "opacity": 1.0
    }, "slow");
});

N.B.: Be extra careful typing "visibility" and "visible" as they are easily misspelled -- the source of many frustrating bugs.

You don't HAVE to use visibility, as you can accomplish the same thing by making the content initially transparent, but using it makes it more explicit what is going on. That is, this also works:

$(element).css("opacity", 0.0).slideDown("slow", function() {
    $(this).animate({
        "opacity": 1.0
    }, "slow");
});



回答5:


Give this a shot: http://jsfiddle.net/solidsource/67XZX/



来源:https://stackoverflow.com/questions/4936137/how-to-slide-down-a-div-then-fadein-the-content-and-vice-versa

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