javascript - creating jquery fadein and fadeout to replace html text -


just wanted ask quick question jquery code. started using jquery/js , feel there cleaner way this. fade "and more" when goes back, not fade more of switch.

$(document).ready(function() {      setinterval(swaptext, 4000);      function swaptext(){      $("#profile-text-fade").fadeout(500, function() {        $(this).fadein(1000).delay(2000);        $(this).html('and more!');        $(this).fadein(2000, function () {          $(this).html('in making, show work can crate!');        });      });    }  });
body {      background-color: #ffc85e;  }  .aligh {    top: 50%;    left: 50%;    transform: translate(-50%, -50%);  }    .jumbotron_resize {    padding-top: 18%;    padding-bottom: 10em;    margin-bottom: 10em;    background-color: #ffc85e;    font-size: 1.2em;  }    h1, p{  text-align: center;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <!doctype html>  <html>    <head>      <meta charset="utf-8">      <meta name="description" content="profile test page">      <meta name="keywords" content="html,css,xml,javascript">      <meta name="author" content="salvador cervantes">      <meta name="viewport" content="width=device-width, initial-scale=1.0">      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/y6pd6fv/vv2hjna6t+vslu6fwyxjcftcephbnj0lyafsxtsjbbfadjzaleqsn6m" crossorigin="anonymous">      <link rel='stylesheet' type:'text/css' href='../css/main.css'>      <title>         test page      </title>    </head>        <body>      <div class="align">        <div class="jumbotron jumbotron-fluid jumbotron_resize">          <div class="container">            <h1 class="display-3">test profile</h1>            <p  id='profile-text-fade' class="lead">in making, show work can crate!</p>          </div>        </div>        <footer>          <div>            <p>&copy 2017        </div>      </div>      <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-dzankj/6xz9si04hgrsxu/8s717jcizly3oi35eouye=" crossorigin="anonymous"></script>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/u6ypibehpof/4+1nzfpr53nxss+glckfwbdfntxtclqqenisfwazpkamnfnmj4" crossorigin="anonymous"></script>      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0abixch4zdo7tp9hkz4tshbi047nrkglo3sejag45jxxngifyzk4si90rdiqnm1" crossorigin="anonymous"></script>      <script src='../js/main.js'></script>    </body>    </html>

now i'm trying fadeout text on webpage , replace "and more!" , fade original text of "in making...". right now, last part snaps place , dones't fade. amazing!

thank you

your text not fading in don't fade out. ie does: $("#div").fadein().fadein()

change last fadein to:

$(this).fadeout(500, function() {      $(this).fadein(2000, function () {         $(this).html('in making, show work can crate!'); 

you might consider refactor create list of messages , loop through them. re-usable , allow add more messages without lots of nasty callback-chaining, eg:

var msgs = ["msg1", "msg2"];  var indx = 0;  $("#div").text(msgs[indx]);  setinterval(function() {    $("#div").fadeout(      500,       function() {         indx++;        if (indx>=msgs.length) indx=0;        $(this).text(msgs[indx]);         $(this).fadein(500);      });  }, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id='div'>initial message</div>


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -