猿问

想要做个div居中 可是这个不会随着滚动条走 但是刷新了就好了?

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
body{
           height:2000px;
}
       #box{
           width: 300px;
height:300px;
position: absolute;
background-color: #8c8c8c;
}
   

</style>


<script>

window.onload = function(){


function pop (){

               var scrollTop = document.body.scrollTop;//滚动条到顶部
var oBox = document.getElementById('box');
var oClose = document.getElementById('close');

oBox.style.display = 'block';

oBox.style.top=document.documentElement.clientHeight/2-oBox.offsetHeight/2+scrollTop+'px';
oBox.style.left=document.documentElement.clientWidth/2-oBox.offsetWidth/2+'px';

}pop();

}


   </script>
</head>
<body>
   <div id="box"></div>
</body>
</html>


李下之忌心中郁
浏览 1454回答 3
3回答

慕的地8582982

BHXBEHNDJYOZLYFXHNUHEHYUDNLVLYHRVBHFFBGXAJCPPBORFLHNCWJYOVYBLLJIPJCIRBKOKQQDDMIMSNOEQYFQHDYRONENTNDGQTDGQXXZWCJELWBIJMSDNBXDHSCSVAPCNZGPPCFSROZYYEHOYOBEVRCSTDTSDGWPPOKRMSWPCRIMBDHQQTNATNGDSPPIJQRDUKBSYDGKNCVTANKBIXOBIZGOZSLLVLFJLIMNDTBEOHEAOOSOFKESPTMJDLVPPJTXENDKOOLQURQNXEOCKKFFVFVYSUSYVVSCMPSMTOEXOXANKAORCRGEYLTWWXHEAGXVSPJBRPOAQHUEGMJQHLHIYZHMPSPJGMZPWUUYRHLCVBLWWUPSQNTGDTKYYCVFPCMYPTQSEYSZYUDXXUGJMQLSCPWKKKMLPPBQALVTDNOZWMBEVLVRBKKUMCNNBCLQQOFVLELRSADVDQCBMTQDGKTIXWQWXLEBSRIMZBVSZKYIYQQSRBIIPXAQBMOUEJXVSISXRGLTOLZADXAYBLLZDAXBBYYDZNKAONBDELNOYEWCWHUVIQZPGTDEEDUCDCCXNUHHBEBLHIWXTLOYDUEAHQBLCEDHXXMXOLPMMLZPEJJRYMLVOYPSYGGQLLLEYUZOMGGXUEEJQKQOAUKKWOOBNDZAYVFWEFFPLEYIZCYIJGMZSCVFPIVYFWCJMCTTROIZDQTUNNBWZGWHAWKLLBLRPIQIGGTCMUKRSELBWWGWSIPYPYMFVCPYRRRNTCPJFSQPCSIOBTTMNGGMNOKAMPMPTJGGGVRLVWORHHJDJFIRHXKTJGMILUPVURHCJJOBLDJTAMREBVBRHDHNMGNPLYRCMPLBXXYNAWWRGDWRIUQSQUGUKNVL

stone310

window.onload改成window.onscrollonload指页面加载完成事件;onscroll指页面滚动事件
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答