進度條100%,我試圖讓我的單頁網站進度條,一切工作正常,我多麼希望除了當頁面加載進度條是100%不爲0%,直到我開始滾動。這是我的代碼:負載
HTML
<div class="progressContainer">
<div id="progress" class="progress"></div>
</div>
CSS
.progressContainer{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 10px;
background-color: rgba(0,0,0,0.65);
}
.progress{
height: 10px;
background: #D66A00;
border-radius: 0;
}
的JavaScript
function updateProgress(num1, num2){
var percent = Math.ceil(num1/num2 * 100) + '%';
document.getElementById('progress').style.width = percent;
}
window.addEventListener('scroll', function(){
var top = window.scrollY;
var height = document.body.getBoundingClientRect().height - window.innerHeight;
updateProgress(top, height);
});
DEMO
https://codepen.io/Klak031/pen/xdXYgb/
好吧,它只是.progress元素繼承了父母的寬度,所以它在開始時爲100%,在css聲明中將其更改爲0 –
.progress {width:0%;}只需將此添加到您的css –
工作原理: )謝謝 – Klak031