效果图
FromDtata.html
<style>
.info {
width: 500px;
min-height: 200px;
border: 1px solid red;
margin: 0 auto;
}
.out {
width: 80%;
height: 20px;
margin: 50px auto;
border: 1px solid red;
overflow: hidden;
}
.in {
height: 100%;
width: 5%;
background-color: blue;
}
</style>
<form>
用户名:
<input type="text" name="username">
密码:
<input type="password" name="password">
头像:
<input type="file" name="photo">
<input type="submit" class="btn" value="提交">
</form>
<div class="out">
<div class="in"></div>
</div>
<h1></h1>
<script>
document.querySelector('.btn').onclick = function (e) {
e.preventDefault();
var fd = new FormData(document.querySelector('form'));
var xhr = new XMLHttpRequest();
xhr.open('post', './03-progress.php');
xhr.upload.onprogress = function (e) {
var value = e.loaded / e.total;
console.log(parseInt(value * 100) + '%');
document.querySelector('.in').style.width = parseInt(value * 100) + '%';
document.querySelector('h1').innerText = parseInt(value * 100) + '%';
}
xhr.send(fd);
xhr.onload = function () {
console.log(xhr.responseText);
}
}
</script>
FromDtata.php
<?php
echo 'formData';
?>
FromDtata.css
*{
margin: 0;
padding: 0;
}
body{
background:#ccc;
}
h1,h2,h3,h4,h5,h6{
text-align: center;
}
form{
width: 400px;
margin:50px auto;
padding:20px;
border: 1px solid #eee;
border-radius: 10px;
background:#fff;
}
input[type='text'],
input[type='select'],
input[type='password'],
input[type='submit'],
input[type='password'],
input[type='button'],
input[type='file'],
select{
width: 100%;
display: block;
height: 24px;
margin-bottom:15px;
border-radius: 3px;
border:1px solid #aaa;
}
input[type='submit']{
height: 40px;
background:rgb(209, 92, 92);
border:none;
color:#fff;
font-size: 24px;;
}
input[type='radio'],
input[type='checkbox']{
height: 20px;
width:20px;
margin:10px;
}
input[type='file']{
border:none;
}