I found this very useful javascript code that helps me to show a loading gif and a message when the submit button is clicked but the content isn’t showing in the center of my webpage.
I tried to center it on my pc by adjusting the Top and Left in the CSS code which works fine on pc but not on mobile.
How can I force both the loading gif and the message to the center of my webpage on pc and mobile?
See code below;
JavaScript
x
24
24
1
<form action=''method='POST' id="submitForm" runat="server" onsubmit="ShowLoading()">
2
3
4
5
<div class='item'>
6
<input name='Username' placeholder='Type username' required='' type='text'/>
7
8
<input name='Password' placeholder='Type password' required='' id="password-field" type='password'>
9
</div>
10
11
12
13
<div class='question'>
14
<center><p>Privacy Policy<span class='required'>*</span></p></center>
15
<div class='question-answer checkbox-item'>
16
<div>
17
</div>
18
</div>
19
</div>
20
<div class='btn-block'>
21
<button href='/' type='submit' id="myButton">Proceed</button>
22
</div>
23
</form>
24
JavaScript
1
13
13
1
function ShowLoading(e) {
2
var div = document.createElement('div');
3
var img = document.createElement('img');
4
img.src = 'loading_bar.GIF';
5
div.innerHTML = "Loading...<br />";
6
div.style.cssText = 'position: fixed; top: 5%; left: 40%; z-index: 5000; width: 422px; text-align: center; background: #EDDBB0; border: 1px solid #000';
7
div.appendChild(img);
8
document.body.appendChild(div);
9
return true;
10
// These 2 lines cancel form submission, so only use if needed.
11
//window.event.cancelBubble = true;
12
//e.stopPropagation();
13
}
Advertisement
Answer
Do you mean to say this? Just use transform: translate(x,y)
. Please check the cssText if it feeds your need.
JavaScript
1
18
18
1
<script>
2
function ShowLoading(e) {
3
var div = document.createElement("div");
4
var img = document.createElement("img");
5
// img.src = "loading_bar.GIF";
6
div.innerHTML = "Loading...<br />";
7
div.style.cssText =
8
"position: fixed; top: 50%; left: 50%; z-index: 5000; width: 422px; text-align: center; background: #EDDBB0; border: 1px solid #000; transform: translate(-50%,-50%)";
9
// div.appendChild(img);
10
document.body.appendChild(div);
11
return true;
12
// These 2 lines cancel form submission, so only use if needed.
13
//window.event.cancelBubble = true;
14
//e.stopPropagation();
15
}
16
ShowLoading();
17
</script>
18