2017-01-27 15 views
-2

被檢測到我想創建在我的網頁元素(一個div),但不能使用position:absolute;如何創建這將使高度由JavaScript

股利將需要100元素%的頁面,但我需要能夠通過JavaScript獲取像素的實際高度。

如何使用CSS設置該風格以允許此JavaScript功能?請,不JQuery的

+2

可以使用JS得到任何元素的高度,不管它是如何與CSS樣式。 –

回答

3

看評論在線:

// Create a <div> dynamically 
 
var div = document.createElement("div"); 
 

 
// Attach CSS class to div 
 
div.setAttribute("class", "height100"); 
 

 
// Add to the document 
 
document.body.append(div); 
 

 
// Get the actual height of the element: 
 
console.log("The height of the div is: " + window.getComputedStyle(div).height);
.height100 { height: 100vh; }

+0

它確實@ScottMarcus。我知道getBoundingClientRect(我沒有聲明),並且對CSS更感興趣。 +1,現在進行測試 – MyDaftQuestions