javascript實(shí)現(xiàn)在指定元素中垂直水平居中
本章節(jié)介紹一下如何實(shí)現(xiàn)未知寬高的元素在指定元素下實(shí)現(xiàn)垂直水平居中效果,下面就以span元素為例子,介紹一下如何實(shí)現(xiàn)span元素在div中實(shí)現(xiàn)水平垂直居中效果,代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<style type="text/css">
#box{
width:200px;
height:150px;
background:blue;
position:relative;
}
#antzone{
background:green;
}
</style>
<script type="text/javascript">
window.onload=function(){
var obox=document.getElementById("box");
var oantzone=document.getElementById("antzone");
var w=oantzone.offsetWidth;
var h=oantzone.offsetHeight;
oantzone.style.position="absolute";
oantzone.style.left="50%";
oantzone.style.top="50%";
oantzone.style.marginLeft=-(w/2)+"px";
oantzone.style.marginTop=-(h/2)+"px";
}
</script>
</head>
<body>
<div id="box">
<spanj id="antzone">腳本之家</span>
</div>
</body>
</html>
上面你的代碼實(shí)現(xiàn)了span元素在div中垂直水平居中效果,下面簡(jiǎn)單介紹一下它的實(shí)現(xiàn)過(guò)程。
一.實(shí)現(xiàn)原理:
雖然css為明確給出span元素的尺寸,但是它畢竟有一個(gè)尺寸的,這個(gè)尺寸可以使用offsetWidth和offsetHeight屬性獲取,然后將此span元素設(shè)置為絕對(duì)定位,然后再將left和top屬性值分別設(shè)置為50%,但是這個(gè)時(shí)候并不是span元素的中心點(diǎn)垂直水平居中,而是span元素的左上角垂直水平居中,然后在設(shè)置span元素的負(fù)的外邊距,尺寸是span元素寬高的一半,這樣就實(shí)現(xiàn)了垂直水平居中效果。
例子二:
思路:實(shí)現(xiàn)起來(lái)最麻煩的其實(shí)是水平居中和垂直居中,其中垂直居中是最麻煩的??紤]到瀏覽器兼容性,網(wǎng)上看了一些資料,發(fā)現(xiàn)在頁(yè)面中垂直居中確實(shí)沒有什么太好的辦法。于是就采用了position:fixed屬性控制時(shí)鐘的絕對(duì)位置,通過(guò)clientWidth和clientHeight來(lái)獲取時(shí)鐘的寬和高,利用javascript控制marginLeft和marginTop來(lái)居中時(shí)鐘。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Centered Clock</title>
<style type="text/css">
body{
background: #fff;
}
body, div, p{
margin: 0;
padding: 0;
}
.center{
position: fixed;
left: 50%;
top: 50%;
}
.box{
border: 1px solid #000;
padding: 20px 30px;
font-size: 1.5em;
font-weight: 500;
margin: auto auto;
}
</style>
</head>
<body>
<div class="center">
<p class="box"></p>
</div>
</body>
<script type="text/javascript">
window.onload = function () {
getTimes();
var box = document.getElementsByClassName("box")[0];
box.style.marginLeft = -box.clientWidth / 2 + "px";
box.style.marginTop = -box.clientHeight / 2 + "px";
setInterval(getTimes, 1000);
}
function getTimes() {
var box = document.getElementsByClassName("box")[0];
var dateTime = new Date();
var year = dateTime.getFullYear();
var date = dateTime.getDate();
var month = dateTime.getMonth() + 1;
var hours = dateTime.getHours();
var minutes = dateTime.getMinutes();
var secondes = dateTime.getSeconds();
box.innerHTML = year + "-" + format(month) + "-" + format(date) + " " + format(hours) + ":"+ format(minutes) +":" + format(secondes);
}
function format(a) {
return a.toString().replace(/^(\d)$/, "0$1");
}
</script>
</html>
例子三:
思路:采用相對(duì)定位,設(shè)定left和top值為(pw-w)/2和(ph-h)/w,其中pw和ph為外部標(biāo)簽的寬與高,w和h為內(nèi)部標(biāo)簽的寬與高。
核心代碼:


以上就是給大家總結(jié)的三種javascript實(shí)現(xiàn)居中的例子,小伙伴們可以參考下,希望對(duì)大家能夠有所幫助。
相關(guān)文章
JS實(shí)現(xiàn)根據(jù)URL批量下載文件并壓縮成zip文件
這篇文章主要為大家學(xué)習(xí)介紹了JS如何實(shí)現(xiàn)根據(jù)URL批量下載文件并壓縮成zip文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-08-08
JS Object構(gòu)造函數(shù)之Object.freeze
這篇文章主要介紹了JS Object構(gòu)造函數(shù)之Object.freeze,對(duì)JS感興趣的同學(xué),可以深入了解下2021-04-04
javascript中使用css需要注意的地方小結(jié)
javascript中使用css需要注意的地方小結(jié),注意保留字問題。2010-09-09
JavaScript高級(jí)程序設(shè)計(jì) 閱讀筆記(十五) 瀏覽器中的JavaScript
Window對(duì)象對(duì)操作瀏覽器窗口非常有用,開發(fā)者可以移動(dòng)或調(diào)整瀏覽器窗口的大小2012-08-08
JS把內(nèi)容動(dòng)態(tài)插入到DIV的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JS把內(nèi)容動(dòng)態(tài)插入到DIV的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
js canvas實(shí)現(xiàn)適用于移動(dòng)端的百分比儀表盤dashboard
這篇文章主要為大家詳細(xì)介紹了js canvas實(shí)現(xiàn)適用于移動(dòng)端的百分比儀表盤dashboard,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

