简体   繁体   English

如何缓慢更改Bootstrap div的内容

[英]How to change contents of bootstrap div slowly

I have a html page that change some content on a button click (I am using onClick). 我有一个html页面,可以在单击按钮时更改某些内容(我正在使用onClick)。 I have it working fine. 我工作正常。

Now I want to try to add some effect to it so that when button is clicked, the text, background color, etc should change slowly (in 3-5 seconds). 现在,我想尝试向其中添加一些效果,以便在单击按钮时,文本,背景色等应缓慢更改(在3-5秒内)。 It think in bootstrap it's called fade effect. 它认为在引导程序中它称为淡入淡出效果。

Can I achieve this by modifying my existing code here: 我可以通过在这里修改现有代码来实现这一点:

 function myFunction() { document.getElementById("idQuote").innerHTML = 'It is better to play than do nothing.'; document.getElementById("idQuoteBy").innerHTML = '- Confucius (Philosopher, 551 BC-479 BC)'; document.body.style.background = '#ffc0cb'; document.getElementById("my-content").style.backgroundColor = '#db7093'; document.body.style.color = 'pink'; } 
  body { background-color: #fff8dc; font: Verdana, Helvetica, sans-serif; color: maroon; } #my-content { background-color: #ffdead; margin: 200px 50px 100px 50px; border-radius: 10px; } .quote { margin: 0px 50px 0px 50px; text-align: center; } .quoteBy { margin: 0px 25px 0px 0px; text-align: right; } .buttonLine { margin: 50px 0px 0px 0px; } #newQuoteButton { display: inline-block; vertical-align: top; margin: 0px 0px 50px 450px; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="container-fluid"> <div id="my-content"> <br /><br /><h1 class="quote" id="idQuote">Quick Brown Fox Jumped Over The Lazy Dog! Quick Brown Fox Jumped Over The Lazy Dog!</h1> <br /><h4 class="quoteBy" id="idQuoteBy">....The Quick Fox</h4> <div class="buttonLine"> <button type="button" id="newQuoteButton" class="btn btn-primary btn-sm" onclick="myFunction()">New Quote</button> </div> </div> <footer> <p class="text-center">Compiled by: Someones Name</p> </footer> </div> 

Just add some transition property to body. 只需向主体添加一些过渡属性即可。 See below in my example. 请参阅下面的示例。 I hope it will help you. 希望对您有帮助。

 function myFunction() { document.getElementById("idQuote").innerHTML = 'It is better to play than do nothing.'; document.getElementById("idQuoteBy").innerHTML = '- Confucius (Philosopher, 551 BC-479 BC)'; document.body.style.background = '#ffc0cb'; document.getElementById("my-content").style.backgroundColor = '#db7093'; document.body.style.color = 'pink'; } 
  body { transition: all 1.5s ease-in-out; // ADD SOME TRANSITION background-color: #fff8dc; font: Verdana, Helvetica, sans-serif; color: maroon; } #my-content { background-color: #ffdead; margin: 200px 50px 100px 50px; border-radius: 10px; } .quote { margin: 0px 50px 0px 50px; text-align: center; } .quoteBy { margin: 0px 25px 0px 0px; text-align: right; } .buttonLine { margin: 50px 0px 0px 0px; } #newQuoteButton { display: inline-block; vertical-align: top; margin: 0px 0px 50px 450px; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="container-fluid"> <div id="my-content"> <br /><br /><h1 class="quote" id="idQuote">Quick Brown Fox Jumped Over The Lazy Dog! Quick Brown Fox Jumped Over The Lazy Dog!</h1> <br /><h4 class="quoteBy" id="idQuoteBy">....The Quick Fox</h4> <div class="buttonLine"> <button type="button" id="newQuoteButton" class="btn btn-primary btn-sm" onclick="myFunction()">New Quote</button> </div> </div> <footer> <p class="text-center">Compiled by: Someones Name</p> </footer> </div> 

Use transition property in body, my-content, idQuote, idQuoteBy like 在body,my-content,idQuote,idQuote中使用transition属性

 -webkit-transition-duration: all 5s;
  transition-duration: all 5s;

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

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