最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery .fadeOut then wait, then .fadeIn doesn't work - Stack Overflow

matteradmin5PV0评论

I'm trying to fade out a div, then fade in another div, and that works for me, the problem is I'm not able to delay it, so first div doesn't push the other div down...

This is my HTML (everything is inline):

<!doctype html>
<html>
    <head>
        <title>JavaScript popup</title>
        <meta charset="utf-8">
        <script src=".12.0/jquery.min.js"></script>
        <script src="main.js" type="text/javascript"></script>
    </head>
    <script type="text/javascript">
        $(document).ready(function(){
            $(".main").hide();
        });
        $("#continue-button").click(function(){
            $(".vge-eksponering").hide();
            $(".main").show();      
        });        
    </script>
    
    <body style="background-color:lightgrey;">
        <div class="vge-eksponering" style="text-align:center;">
            <h1>Tjek VGEs hjemmeside ud på:
            <br>
            <a href="" target="_blank">www.vge.dk</a></h1>
            <button id="continue-button" style="font-size:2em;">Fortsæt til VGE News</button>
        </div>
        <script>
            $("#continue-button").click(function(){
                $(".vge-eksponering").fadeOut(1200);
                $(".main").fadeIn(1200);
            });  
        </script>
        <div class="main" style="background-color:lightblue;">
            <h1>JavaScript popup</h1>
            <p>Hopefully this will work soon...</p>
        </div>
    </body>
</html>

I'm trying to fade out a div, then fade in another div, and that works for me, the problem is I'm not able to delay it, so first div doesn't push the other div down...

This is my HTML (everything is inline):

<!doctype html>
<html>
    <head>
        <title>JavaScript popup</title>
        <meta charset="utf-8">
        <script src="https://ajax.googleapis./ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script src="main.js" type="text/javascript"></script>
    </head>
    <script type="text/javascript">
        $(document).ready(function(){
            $(".main").hide();
        });
        $("#continue-button").click(function(){
            $(".vge-eksponering").hide();
            $(".main").show();      
        });        
    </script>
    
    <body style="background-color:lightgrey;">
        <div class="vge-eksponering" style="text-align:center;">
            <h1>Tjek VGEs hjemmeside ud på:
            <br>
            <a href="http://www.vge.dk" target="_blank">www.vge.dk</a></h1>
            <button id="continue-button" style="font-size:2em;">Fortsæt til VGE News</button>
        </div>
        <script>
            $("#continue-button").click(function(){
                $(".vge-eksponering").fadeOut(1200);
                $(".main").fadeIn(1200);
            });  
        </script>
        <div class="main" style="background-color:lightblue;">
            <h1>JavaScript popup</h1>
            <p>Hopefully this will work soon...</p>
        </div>
    </body>
</html>

EDIT: I used this, and it worked.

$(".vge-eksponering").fadeOut(1200);
$(".main").delay(1200).fadeIn(1200);
Share Improve this question edited Feb 4, 2016 at 20:24 Kevin From asked Feb 4, 2016 at 18:16 Kevin FromKevin From 1522 silver badges10 bronze badges 1
  • You want to fade out .vge-eksponering and, when it's done fading out, fade in .main? – T.J. Crowder Commented Feb 4, 2016 at 18:19
Add a ment  | 

2 Answers 2

Reset to default 6

You have (at least) two choices:

If there's only one .vge-eksponering element, then use the pletion callback fadeOut gives you:

$(".vge-eksponering").fadeOut(1200, function() {
    $(".main").fadeIn(1200);
});

Or just use delay with a value equivalent to the first fadeOut:

$(".vge-eksponering").fadeOut(1200);
$(".main").delay(1200).fadeIn(1200);

use setInterval

setInterval(function() {
  $(".main").fadeIn(1200);
}, 1200);

a tutorial on setInterval http://www.w3schools./jsref/met_win_setinterval.asp

Post a comment

comment list (0)

  1. No comments so far