I have problem with temporary modifying div CSS for creating loading screen while something is computing or modifying area under shadow div.
Expected result:
- Click button.
- Div is masked with a shadow (for loading animation).
- Code is doing something time hungry.
- After 2 is done, shadow is removed.
Current result
- Click button.
- Code is doing something time hungry.
- Div is masked with a shadow (Tested with commented PART 3).
- Then is instantly removed, without being visible.
Tried:
This code with and without promise.
Code snipped:
$('#butt').on('click', function() {
console.log('test1');
document.getElementById('info').innerHTML = '';
new Promise((resolve, reject) => {
//PART 1 ------------------------------------------------------
let height = document.getElementById('el1').getBoundingClientRect().height
+ document.getElementById('el2').getBoundingClientRect().height
+ document.getElementById('el3').getBoundingClientRect().height;
document.getElementById('info').innerHTML += height+'<br>';
document.getElementById('shadow').style.height = ''+height+'px';
document.getElementById('info').innerHTML += document.getElementById('shadow').style.height+'<br>';
document.getElementById('shadow').style.top = ''+(-height)+'px';
document.getElementById('shadow').classList.add("w3-show");
document.getElementById('butt').classList.add("w3-disabled");
resolve();
}).then(() => {
//PART 2 ------------------------------------------------------
//Do anything that not finish instantly.
let a = 0;
let i = 10000;
while(i!=0) {
i--;
document.getElementById('info').innerHTML += a;
a = a + Math.log(a);
};
console.log('test2');
}).then(() => {
//PART 3 ------------------------------------------------------
console.log('test3');
document.getElementById('shadow').classList.remove("w3-show");
document.getElementById('butt').classList.remove("w3-disabled");
});
});
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<body>
<button id='butt'>Show modal</button>
<br>
<p id='info'></p>
<br>
<div>
<div id='el1' style="background-color: blue; heigh:10px; width:100px;">s</div>
<div id='el2' style="background-color: green; heigh:10px; width:100px;">s</div>
<div id='el3' style="background-color: red; heigh:10px; width:100px;">s</div>
<div id='shadow' class='w3-hide' style="position: relative; background-color: black; heigh:5px; z-index: 2; opacity: 0.5; width:100px;">s</div>
<br><br><br><br><br><br><br>
</div>
</body>
</html>