Tom*_*ica 5 html javascript css progress-bar
I'm trying to make a progress bar that looks like emptying glass of beer. Unfortunately I'm not very artistic person but I'm doing my best.
My concept goes like this:
<div> with "beer" background, 100% high vertically. It hides any overflow<div>, positioned relatively to the parent. It contains:
<div> to obscure "beer" pattern when it's goneHere's the concept on picture:
I almost succeeded in my goal, as you can see in this snippet:
Note: the animation requires ES6 async/await
Promise.timeout = function (delay) {
return new Promise((resolve) => {
setTimeout(resolve, delay);
});
};
Promise.animationFrame = function () {
return new Promise((resolve) => {
requestAnimationFrame(resolve);
});
};
(async function () {
const progress = document.querySelector("div.top");
for (let i = 0, l = 1000; i < l; ++i) {
progress.style.bottom = (100 - i / 10) + "%";
await Promise.animationFrame();
}
})();Run Code Online (Sandbox Code Playgroud)
html, body, div {
margin: 0px;
padding: 0px;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
}
div.progress {
max-width:300px;
width: 100vw;
float: left;
position: relative;
overflow: hidden;
height: 100%;
background-image: url("https://i.imgur.com/SKfSqEv.jpg");
background-size: 500px;
border-right: 1px solid black;
}
div.progress div.top {
height: 100vh;
position: relative;
bottom: 100%;
}
div.progress div.top div.white{
background-color: white;
width:100%;
height:100%;
}
div.progress div.top div.beer-top {
/*background-image is at the bottom since it is bse64 */
background-repeat: repeat-x;
background-position-y: bottom;
background-position-x: left;
background-size: 302px, 100px;
height: 100px;
}
/*div.progress div.top {
background-position: bottom;
height: 100%;
background-image: url("img/beer-top.png");
}*/
div.main {
float: left;
}
div.progress div.top div.beer-top {
background-image: url('https://preview.ibb.co/k2x2V6/beer_top.png');
}Run Code Online (Sandbox Code Playgroud)
<div class="progress">
<div class="top">
<div class="white"></div>
<div class="beer-top"></div>
</div>
<!--<div class="progress-area">
</div>-->
</div>
<div class="main">
</div>Run Code Online (Sandbox Code Playgroud)
But there are two big issues:

div.white height to 100%, the foam is pushed down and is visible even at "100%" state. It also disappears completely at the end. The desired state is this:
How to fix those two issues?
I'm looking for CSS only solution, so that the bar can be displayed statically as well (eg. at 40% when the page loads without javascript).
Also note: setting the foam as background for the "white div" is not an option, because the bottom of the foam image is transparent.
因为需要一个纯 CSS 解决方案。您可以将泡沫添加到“白色”div 作为背景和白色渐变(当然,需要进行一些调整才能使图像完美显示)。
参见JSfiddle:
Promise.timeout = function (delay) {
return new Promise((resolve) => {
setTimeout(resolve, delay);
});
};
Promise.animationFrame = function () {
return new Promise((resolve) => {
requestAnimationFrame(resolve);
});
};
(async function () {
const progress = document.querySelector("div.top");
for (let i = 0, l = 1000; i < l; ++i) {
progress.style.bottom = (100 - i / 10) + "%";
await Promise.animationFrame();
}
})();Run Code Online (Sandbox Code Playgroud)
html, body, div {
margin: 0px;
padding: 0px;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
}
div.progress {
max-width:300px;
width: 100vw;
float: left;
position: relative;
overflow: hidden;
height: 100%;
background-image: url("https://i.imgur.com/SKfSqEv.jpg");
background-size: 500px;
border-right: 1px solid black;
}
div.progress div.top {
height: 100vh;
position: relative;
bottom: 100%;
}
div.progress div.top div.white{
background-color: white;
width:100%;
height:100%;
}
div.progress div.top div.beer-top {
/*background-image is at the bottom since it is bse64 */
background-repeat: repeat-x;
background-position-y: bottom;
background-position-x: left;
background-size: 302px, 100px;
height: 100px;
}
/*div.progress div.top {
background-position: bottom;
height: 100%;
background-image: url("img/beer-top.png");
}*/
div.main {
float: left;
}
div.progress div.top div.beer-top {
background-image: url('https://preview.ibb.co/k2x2V6/beer_top.png');
}
.top {
background: no-repeat;
background-position: bottom;
background-image: url('https://preview.ibb.co/k2x2V6/beer_top.png'),linear-gradient(to top, rgba(0,0,0,0) 1%, rgba(255,255,255,1) 5%);
}Run Code Online (Sandbox Code Playgroud)
<div class="progress">
<div class="top">
</div>
<!--<div class="progress-area">
</div>-->
</div>
<div class="main">
</div>Run Code Online (Sandbox Code Playgroud)