代码描述:javascript动态设置 div垂直水平居中。javascript动态设置div垂直水平居中代码实例
利用javascript设置div的垂直水平效果其实还是利用css的原理。 代码实例:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <style type="text/css"> #thediv{ background-color:blue; width:400px; height:200px; border:1px solid #00F; } </style> <script type="text/javascript"> window.onload=function(){ var odiv=document.getElementById("thediv"); odiv.style.position="absolute"; odiv.style.left="50%"; odiv.style.top="50%"; odiv.style.marginLeft="-200px"; odiv.style.marginTop="-100px"; } </script> <body> <div id="thediv"></div> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能