开发者

fadeIn, fadeOut not working (or I don't know how to make it work)

开发者 https://www.devze.com 2023-03-10 11:55 出处:网络
I have a div with the following class: #instrucPanel { background-color:black; opacity:0; position: absolute;

I have a div with the following class:

#instrucPanel {
    background-color:black;
    opacity:0;
    position: absolute;
    left: 180px;
    top: 71px;
    开发者_StackOverflow社区height: 226px
}

At page load is hidden. I want to display it when user click on a button. And make it hidden if the user click on the same button.

To do that I'm using the following JavaScript code:

function ShowHideInstruc()
{
    if ($.myNameSapace.instShown)
    {
        $.myNameSpace.instShown = false;
        $('#instrucPanel').fadeOut('fast');
    }
    else
    {
        $.myNameSpace.instShown = true;
        $('#instrucPanel').fadeIn('slow');
    }
}

But I don't see anything, it is always hidden.

What I'm doing wrong?


First, you must use display:none; as opposed to opacity:0.

Then you can make use of the .fadeToggle() method, like this:

function ShowHideInstruc()
{
     var show = $.viacognitaspace.instShown = !$.viacognitaspace.instShown; 
     $('#instrucPanel').fadeToggle((show) ? 'fast' : 'slow');
}

Note how this approach is a whole lot DRYer.


opacity: 0; makes your Panel transparent.

Use the display: none; to hide the panel at the beginning and then just fadein and fadeout with jQuery


UPDATE Here is the working demo of what you want on JSFIDDLE

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号