簡體   English   中英

如何在JS中使用數學設置div的高度?

[英]How do I set the height of a div using math in JS?

我希望能夠輸入div的寬高比,而不必每次都在CSS中手動計算和輸入高度。

沒什么大不了的,但是這樣會更優雅,我想學習錯誤的地方以供將來參考。

 window.onload = function() { var x = document.getElementById("movie"); var ar = 1.33; x.style.height = x.style.height / ar; }; 
 body { background: black; margin: 0 auto; max-width: 200px; height: auto; } #movie { width: 200px; height: 100px; background: navy; } .t { -webkit-animation: change 48s infinite; -moz-animation: change 48s infinite; -o-animation: change 48s infinite; animation: change 48s infinite; } .name { font-family: 'Alegreya Sans', Helvetica, sans-serif; font-size: 13px; color: gold; text-align: center; letter-spacing: 3px; margin-top: 36px; opacity: 0.3; } @keyframes change { from { background-color: black; } to { background-color: white; } } 
 <body> <div id="movie" class="t"></div> <p class="name">Pedro Costa</p> </body> 

CSS實際上有一個可以使用的calc方法。

width: calc(100% - 100px);

例如: https//www.w3schools.com/cssref/tryit.asp? filename = trycss_func_calc

x.style.height是一個空字符串,因為它正在查看#movie元素的style屬性(該屬性最初為空)。 該錯誤可能是由於嘗試將字符串除以數字而引起的。

要弄清楚通過樣式表或<style>塊應用了哪些樣式,可以查看document.getComputedStyle() 但是,如果您對元素的高度感興趣,可能會發現最好改為查看元素的offsetHeight屬性。 計算完新高度后,還需要確保在將其作為樣式添加到元素之前,添加正確的單位(例如px )。

x.style.height = x.offsetHeight / ar + 'px';

 window.onload = function() { var x = document.getElementById('movie'); var ar = 1.33; x.style.height = x.offsetHeight / ar + 'px'; }; 
 body { background: black; margin: 0 auto; max-width: 200px; height: auto; } #movie { width: 200px; height: 100px; background: navy; } .t { -webkit-animation: change 48s infinite; -moz-animation: change 48s infinite; -o-animation: change 48s infinite; animation: change 48s infinite; } .name { font-family: 'Alegreya Sans', Helvetica, sans-serif; font-size: 13px; color: gold; text-align: center; letter-spacing: 3px; margin-top: 36px; opacity: 0.3; } @keyframes change { from { background-color: black; } to { background-color: white; } } 
 <body> <div id="movie" class="t"></div> <p class="name">Pedro Costa</p> </body> 

或者,您可以將#movie元素的高度設置為嵌入式樣式,但這通常被認為是較差的做法。

x.style.height = parseInt(x.style.height) / ar + 'px';

 window.onload = function() { var x = document.getElementById('movie'); var ar = 1.33; x.style.height = parseInt(x.style.height) / ar + 'px'; }; 
 body { background: black; margin: 0 auto; max-width: 200px; height: auto; } #movie { width: 200px; background: navy; } .t { -webkit-animation: change 48s infinite; -moz-animation: change 48s infinite; -o-animation: change 48s infinite; animation: change 48s infinite; } .name { font-family: 'Alegreya Sans', Helvetica, sans-serif; font-size: 13px; color: gold; text-align: center; letter-spacing: 3px; margin-top: 36px; opacity: 0.3; } @keyframes change { from { background-color: black; } to { background-color: white; } } 
 <body> <div id="movie" class="t" style="height: 100px;"></div> <p class="name">Pedro Costa</p> </body> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM