I have a div called time-box
. Sometimes I will also include an additional class called countdown
. If countdown
is added then I would like to use a CSS transition effect so the background changes to be red over the course of 60 seconds. In other words, each second that passes the red background gets a little wider until eventually all of the green background has gone.
I have found similar posts here but they all seem to relate to hover
Here is a fiddle
I don't know a "simple" way to get what you want going from left to right, but there's a way you can create it using before and after pseudoelements. The key here is I'm going to create a :before pseudoelement that has the new background that transitions across, and an :after pseudoelement that replicates the content and puts it on top of the before, so it's still visible. This required putting the content in an attribute on the div so I could reference it in the 'content' of the pseudoelement. If you had more complex content inside, you could probably do away with the :after and simply give the internal content position and z-index to make sure it's visible. Here's the resulting CSS
.time-box {
height: 27px;
text-align: center;
background-color: #25E57B;
font-size:2rem;
padding:0px;
font-size:1.2rem;
text-transform:uppercase;
padding:3px 5px 3px 5px;;
font-weight:600;
height:auto;
position: relative;
}
.time-box:before {
background-color: red;
position: absolute;
left:0;
top: 0;
height: 100%;
width: 0;
content: " ";
transition: width 60s ease;
}
.countdown:after {
content: attr(data-content);
width: 100%;
text-align: center;
height: 100%;
position: absolute;
left: 0;
top: center;
z-index: 1;
}
.countdown:before {
width:100%;
}
And updated fiddle: https://jsfiddle.net/tunzwqd7/2/
You would need to add a maximum and a little more math to make the 100% divisible by 60, but this should get you on the right track. Currently this code updates every second and adds 1% to the progress bar width with each iteration.
var time = 0; var bar = document.querySelector('.countdown .progress-bar'); window.setInterval(function(){ time++; bar.style.width = time+"%"; }, 1000);
.time-box { height: 27px; text-align: center; background-color: #25E57B; font-size:2rem; padding:0px; font-size:1.2rem; text-transform:uppercase; padding:3px 5px 3px 5px;; font-weight:600; height:auto; position: relative; } .progress-bar { display: none; } .countdown .progress-bar { display: block; position: absolute; left: 0; top: 0; bottom: 0; background: red; width: 0%; z-index: 1; transition: all 0.3s ease-out; } .countdown p { z-index: 2; position: relative; }
<div class="time-box"> <p>12:00</p> <div class="progress-bar"></div> </div> <div class="time-box countdown"> <p>12:00</p> <div class="progress-bar"></div> </div>
Using CSS animation
property...
.time-box { height: 27px; text-align: center; background-color: #25E57B; font-size: 2rem; padding: 0px; font-size: 1.2rem; text-transform: uppercase; padding: 3px 5px 3px 5px; font-weight: 600; height: auto; position: relative; z-index: 1; } .time-box.countdown:before { content: ''; width: 0; height: 100%; display: block; position: absolute; top: 0; left: 0; background: red; animation: countdown 60s forwards; z-index: -1; } @keyframes countdown { 0% { width: 0; } 100% { width: 100%; } }
<div class="time-box"> 12:00 </div> <div class="time-box countdown"> <span>12:00</span> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.