简体   繁体   English

只有第一个随机引用淡入

[英]Only first random quote fades in

I am building a random quote generator app. 我正在构建一个随机报价生成器应用程序。 When I try to add the fading effect in my JS (on click), it only works for the first quote. 当我尝试在我的JS中添加淡入淡出效果(单击时)时,它仅适用于第一引号。

 var quotes = { quote1: "Life isn't about getting and having, it's about giving and \\ being - Kevin Kruse - ", quote2: "Whatever the mind of man can conceive and believe, it can \\ achieve - Napoleon Hill - " } $(document).ready(function() { // function generator var randQuote = function(obj) { var keys = Object.keys(obj); //print key values randomly return obj[keys[Math.floor(keys.length * Math.random())]]; }; // function display $("button").click(function myQuote() { $("#demo").fadeIn(); document.getElementById("demo").innerHTML = randQuote(quotes); }); }); 
 #quote { background-color: white; border-radius: 2%; box-shadow: 0 0 25px #161616; } .paragraph { line-break: auto; font-family: 'Satisfy', cursive; font-size: xx-large; margin: 20px; display: none; } .display { background-color: #dfdfdf; } .title { font-family: 'Dancing Script', cursive; margin: 20px; } .btn-custom { margin: 20px; font-family: 'Dancing Script', cursive; font-size: x-large; } .container { position: fixed; top: 35%; left: 30%; margin-top: -100px; margin-left: -200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-4"> <h1 class="title"><strong>Random Quotes For You</strong></h1> </div> </div> <div class="row"> <div class="col-md-8 col-md-offset-2" id="quote"> <p id="demo" class="paragraph"></p> </div> </div> <div class="row"> <div class="col-md-8 col-md-offset-5"> <button onclick="myQuote()" class="btn btn-primary btn-custom">New Quote</button> </div> </div> </div> 

You've already called the fadeIn on that element. 您已经在该元素上调用了fadeIn。 You need to fade it out then flip the html and fade it back in. Once you understand the WHY you must do that then you should look at fadeToggle as it provides and animation complete callback so you can have a smother transition with loading your html then showing it again with the fade in. 您需要将其淡出,然后翻转html,然后再将其淡入。了解了为什么之后,您应该查看fadeToggle(它提供的内容)和动画完成回调,以便在加载html时进行更平滑的过渡,然后淡入时再次显示。

$("button").click(function(){
    var flip = $("#div1");
    flip.fadeToggle('slow', function (){flip.html('weee').fadeToggle();}); 
});

This should fade in the quote at the beginning, then fade it out and replace it with the new quote, then fade back in. You can change the "500" to whatever fade time suits best. 这应该在开始时淡入报价,然后淡出并用新报价替换,然后淡入。您可以将“ 500”更改为最适合的淡入时间。

Also, you can get rid of the onclick="myQuote()" on the <button> element. 另外,您可以摆脱<button>元素上的onclick="myQuote()"

$("button").click(function myQuote(){
  $("#demo").fadeOut(500, function(){
      document.getElementById("demo").innerHTML = randQuote(quotes);
  });
  $("#demo").fadeIn(500);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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