ami*_*pta 45 javascript css jquery html5-canvas
我想说明一个正在运行的进度条,而我的页面加载喜欢这里,在我的网页.我在我的示例中使用了一个简单的加载图像,但我想在正在运行的进度条中将其转换.这是我的代码:
$(window).load(function() {
alert("hi");
$('#loading').hide();
});
Run Code Online (Sandbox Code Playgroud)
#loading {
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: fixed;
display: block;
opacity: 0.7;
background-color: #fff;
z-index: 99;
text-align: center;
}
#loading-image {
position: absolute;
top: 100px;
left: 240px;
z-index: 100;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id="loading">
<img id="loading-image" src="http://cdn.nirmaltv.com/images/generatorphp-thumb.gif" alt="Loading..." />
</div>
<div id="txt">
<h2>Let AJAX change this text</h2>
</div>
<button>Change Content</button>
Run Code Online (Sandbox Code Playgroud)
这是一个JSFiddle
Tap*_*Pal 13
我已从此页面复制了以下相关代码.希望这可能对你有所帮助.
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
//Download progress
xhr.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data) {
//Do something success-ish
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个鸡蛋问题.您将无法执行此操作,因为您需要加载资源以显示进度条小部件,此时您的页面将被完全或部分下载.此外,您需要知道用户请求之前页面的总大小,以便计算百分比.
这比它的价值更麻烦.
简单的步骤,请遵循它们,我想它将解决您的问题
在页面中包含这些CSS,
.progress {
position: relative;
height: 2px;
display: block;
width: 100%;
background-color: white;
border-radius: 2px;
background-clip: padding-box;
/*margin: 0.5rem 0 1rem 0;*/
overflow: hidden;
}
.progress .indeterminate {
background-color:black; }
.progress .indeterminate:before {
content: '';
position: absolute;
background-color: #2C67B1;
top: 0;
left: 0;
bottom: 0;
will-change: left, right;
-webkit-animation: indeterminate 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite;
animation: indeterminate 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite; }
.progress .indeterminate:after {
content: '';
position: absolute;
background-color: #2C67B1;
top: 0;
left: 0;
bottom: 0;
will-change: left, right;
-webkit-animation: indeterminate-short 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) infinite;
animation: indeterminate-short 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) infinite;
-webkit-animation-delay: 1.15s;
animation-delay: 1.15s; }
@-webkit-keyframes indeterminate {
0% {
left: -35%;
right: 100%; }
60% {
left: 100%;
right: -90%; }
100% {
left: 100%;
right: -90%; } }
@keyframes indeterminate {
0% {
left: -35%;
right: 100%; }
60% {
left: 100%;
right: -90%; }
100% {
left: 100%;
right: -90%; } }
@-webkit-keyframes indeterminate-short {
0% {
left: -200%;
right: 100%; }
60% {
left: 107%;
right: -8%; }
100% {
left: 107%;
right: -8%; } }
@keyframes indeterminate-short {
0% {
left: -200%;
right: 100%; }
60% {
left: 107%;
right: -8%; }
100% {
left: 107%;
right: -8%; } }
Run Code Online (Sandbox Code Playgroud)
然后在进度条中添加您的身体标签,
<div class="progress" id="PreLoaderBar">
<div class="indeterminate"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
然后它将在您的页面加载时开始,现在您所要做的就是在页面加载时将其隐藏,或者将可见性设置为none,或者使用javascript将其隐藏,
document.onreadystatechange = function () {
if (document.readyState === "complete") {
console.log(document.readyState);
document.getElementById("PreLoaderBar").style.display = "none";
}
}
Run Code Online (Sandbox Code Playgroud)
让我知道您是否遇到任何问题,也可以添加任何类型的进度条,您可以轻松地找到它们,在此示例中,我使用了不确定的进度条。