Reputation: 85
I am building my first javascript project and making a random quote generator. I want to implement a smooth background transition in my project but I am not able to achieve that.
I already tried messing with transition property and animate property but nothing seems to work.
HTML code:
<body>
<div id="quote-box" class="quotes">
<div id="text"></div>
<div id="author"></div>
<div class="links">
<a id="twitter-quote" href="https://twitter.com/intent/tweet?text=" target="_blank"><i class="fab fa-twitter"></i></a>
<a id="tumblr-quote" href="https://twitter.com/intent/tweet?text=${quote}" target="_blank"><i class="fab fa-tumblr"></i></a>
<a id="new-quote" href="#">New Quote</a>
</div>
</div>
<script src="./main.js"></script>
</body>
CSS code:
body{
width: 100vw;
height: 100vh;
}
.quotes{
position: absolute;
width: 37.5rem;
bottom: 60px;
left: -37.5rem;
min-height: 100px;
padding: 1.4rem;
background-color: #eee;
border-top-right-radius: .3rem;
border-bottom-right-radius: .3rem;
overflow: hidden;
text-align: justify;
background-color: rgba(255, 255, 255, 0.6);
opacity: 0;
}
.slide{
transform: translateX(37.5rem);
opacity: 1;
transition: all 0.7s ease-in-out 0.3s;
}
JavaScript Code:
function setupQuotes() {
fetch('https://raw.githubusercontent.com/smartdev007/Avengers-Quote/master/quotes.json')
.then(response => response.json())
.then(data => {
function getRandomQuote() {
if (quoteBox.classList.contains('slide')) {
quoteBox.classList.remove('slide')
}
const randomIdx = parseInt(Math.random() * data.length);
const { quote, author, url } = data[randomIdx];
tweetBtn.setAttribute('href', `https://twitter.com/intent/tweet?text=${quote}`);
tumblrBtn.setAttribute('href', `https://twitter.com/intent/tweet?text=${quote}`);
setTimeout(() => {
document.getElementById('text').innerHTML = `<span class="big-quote">“</span>
${quote}`;
document.getElementById('author').innerHTML = `-${author}`;
document.body.style.background = `url('${url}') no-repeat center top/cover`;
quoteBox.classList.add('slide');
}, 500)
}
newQuote.addEventListener('click', getRandomQuote);
getRandomQuote();
})
.catch(e => console.log(e));
}
Codepen link for the full project: https://codepen.io/FuriousJK/pen/YMoNgZ
I am trying to achieve this kind of transition: https://codepen.io/bradtraversy/full/oVPBaa
Upvotes: 0
Views: 83
Reputation: 138
Add this to body:
transition: background 1s ease-in-out;
-moz-transition: background 1s ease-in-out;
-webkit-transition: background 1s ease-in-out;
https://codepen.io/anon/pen/bybGew
Upvotes: 1