2017-09-12 3 views
0

jqueryコードで簡単に質問したいと思っています。ちょうどJquery/JSを使用し始めたと私はこれを行うためのよりクリーンな方法があるように感じる。それは "さらにはるかに"フェードしますが、それが戻ってくると、それ以上のスイッチは消えません。jQuery fadeinとfadeoutを作成してhtmlテキストを置き換える

$(document).ready(function() { 
 

 
    setInterval(swapText, 4000); 
 

 
    function swapText(){ 
 
    $("#profile-text-fade").fadeOut(500, function() { 
 
     $(this).fadeIn(1000).delay(2000); 
 
     $(this).html('and much more!'); 
 
     $(this).fadeIn(2000, function() { 
 
     $(this).html('In the making, Soon to show all the work I 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 the making, Soon to show all the work I 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>

今、私は何をしようとしているウェブページ上でテキストをフェードアウトし、それを置き換えることです「とはるかに!」その後、元のテキストの「作成中」にフェードバックします。今、最後の部分だけが場所に戻り、消えません。どんな助けも素晴らしいだろう!

私はこれがあなたがあなたのコードを編集し必要なものだと思うあなた

+0

がでHTMLやCSSを含め、関連するすべてのコードを投稿してください質問。 –

+1

あなたのコードはうまくいきます: - https://jsfiddle.net/fo1yyono/ –

+0

申し訳ありませんが、元のテキストに戻ってトランザクションをフェードとして返すという質問を編集しました。今すぐ戻ってフェージングしません。 –

答えて

0

テキストはフェードインしないので、フェードインしません。すなわちそれがない:$("#div").fadeIn().fadeIn()

がにあなたの最後のフェードインを変更

$(this).fadeOut(500, function() { 
    $(this).fadeIn(2000, function() { 
     $(this).html('In the making, Soon to show all the work I can crate!'); 

あなたがそれらを介してメッセージのリストを作成するためにリファクタリングして、単純にループを検討したいと思うかもしれません。これはまた、例えば、再使用可能であるとあなたは厄介なコールバック・チェーンの多くせずに複数のメッセージを追加することができます:

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>

+0

ありがとうございました!私は実際に最初に参加しようとしましたが、それはちょうど奇妙な演技だったので、私はあなたが私に与えた例を使って少し修正しました。できます!洞察に感謝します! –

0

ありがとう、あなたが最後にフェードインする前にそれをフェードアウトする必要があり、(私は少しいくつかの円滑な効果のための間隔を増加している)

$(document).ready(function() { 
 
    swapText(); 
 
    setInterval(swapText, 6000); 
 

 
    function swapText(){ 
 
    $("#profile-text-fade").stop(); 
 
    $("#profile-text-fade").fadeOut(1500, function() { 
 
     $(this).html('and much more!'); 
 
     $(this).fadeIn(1500); 
 
     
 
     $(this).fadeOut(1500, function() { 
 
     $(this).fadeIn(1500); 
 
     $(this).html('In the making, Soon to show all the work I 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 the making, Soon to show all the work I 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>

関連する問題