简体   繁体   中英

Div fading in and out

I need your help, I am working on a website where I want a div to delay for 2 seconds then fade in when another div is clicked, but when I want to click it again (to close it) I want it to fade out instantly. Any help?

If you can use Jquery, the following code will help you do it as i got what you want: this is default css:

.invisElem{display:none}

and the jquery code:

$('body').on('click', '.boxbutton1', function(){
        var counter = $(this).data('count');
        if(counter == undefined){
            counter = 0;
            setTimeout(function() {
                $('.gymtext').fadeIn(500)//fadeIn after 2 seconds(2000 ms)
            }, 2000);
        }
        else if(counter == 0){
            $('.gymtext').fadeOut(function(){
                $('.gymtext').remove()
            });//fadeout quickly then remove
        }
    })

i tried to write it down novice friendly if you needed help add comment

    <script>
    $(document).ready(function(){
      $("button").click(function(){
        $("#div").fadeToggle(240);
      });
    });
    </script>

<button>Click to fade DIV</button>

<div id="div" style="width:100px;height:100px;background-color:blue;"></div>

Html:

<div><a href="#" id="btn">Show div1 and hide div2</a></div>

<div id="div1">Div1</div>
<div id="div2">Div2</div>

Css:

 #div2 {display:none;}

Jquery:

  $('#btn').click(function(e){    
$('#div1').fadeOut('slow', function(){
    $('#div2').fadeIn('slow');
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM