js可变高度div

时间:2019-09-19 14:18:19

标签: javascript css

我尝试了这个js代码,但没有结果。 说明:我的页面加载了一个名为row的div,其最小高度不是px而是vh。 我需要将该值设置为另一个div的高度,名为row2。

window.onload = function() {
var number = document.getElementById('row').style.minHeight;
document.getElementById('row2').style.height = number+'vh';
}

我在哪里做错了? 谢谢

3 个答案:

答案 0 :(得分:0)

函数内的第一行:

android....rcesImpl ResourcesImpl.java line 228 in android.content.res.ResourcesImpl.getResourceName() android....rcesImpl ResourcesImpl.java line 687 in android.content.res.ResourcesImpl.loadDrawableForCookie() android....rcesImpl ResourcesImpl.java line 571 in android.content.res.ResourcesImpl.loadDrawable() android....esources Resources.java line 858 in android.content.res.Resources.loadDrawable() android....pedArray TypedArray.java line 928 in android.content.res.TypedArray.getDrawable() android....pedArray XResources.java line 1363 in android.content.res.XResources$XTypedArray.getDrawable() android....Drawable AnimatedStateListDrawable.java line 504 in android.graphics.drawable.AnimatedStateListDrawable.parseItem() android....Drawable AnimatedStateListDrawable.java line 453 in android.graphics.drawable.AnimatedStateListDrawable.inflateChildElements() android....Drawable AnimatedStateListDrawable.java line 385 in android.graphics.drawable.AnimatedStateListDrawable.inflate() android....Inflater DrawableInflater.java line 130 in android.graphics.drawable.DrawableInflater.inflateFromXml() android....Drawable Drawable.java line 1227 in android.graphics.drawable.Drawable.createFromXmlInner() android....Drawable Drawable.java line 1200 in android.graphics.drawable.Drawable.createFromXml() android....rcesImpl ResourcesImpl.java line 715 in android.content.res.ResourcesImpl.loadDrawableForCookie() android....rcesImpl ResourcesImpl.java line 571 in android.content.res.ResourcesImpl.loadDrawable() android....esources Resources.java line 771 in android.content.res.Resources.getDrawable() android....esources XResources.java line 790 in android.content.res.XResources.getDrawable() android.....Context Context.java line 525 in android.content.Context.getDrawable() androidx...ontent.a line 2 in androidx.core.content.a.c() androidx...idget.l0 line 15 in androidx.appcompat.widget.l0.a() androidx...idget.l0 line 11 in androidx.appcompat.widget.l0.a() a.a.k.a.a line 1 in a.a.k.a.a.c() com.goog...Delegate line 2 in com.google.android.material.textfield.PasswordToggleEndIconDelegate.initialize() com.goog...utLayout line 5 in com.google.android.material.textfield.TextInputLayout.setEndIconMode() com.goog...utLayout line 184 in com.google.android.material.textfield.TextInputLayout.<init>()

将为您返回值(含单位)

因此,您不要将单位传递给第二个分配。

简而言之,将您的代码更改为不可知的,就像这样:

var number = document.getElementById('row').style.minHeight;

答案 1 :(得分:0)

使用getComputedStyle,它将返回单位,因此您需要将其切掉。

window.onload = function() {
  var row = document.getElementById('row')
  var number = parseInt(window.getComputedStyle(row).minHeight)
  document.getElementById('row2').style.height = number + 'vh';
}
div {
  border: 1px solid black;
}

#row {
  min-height: 100px;
  background-color: yellow;
}
<div id="row"> boom

</div>

<div id="row2"> room

</div>

答案 2 :(得分:0)

Find
window.onload = function() {
    var number = document.getElementById('row').offsetHeight;
    var numberVh = number * 0.10718113612004287;
    document.getElementById('row2').style.height = numberVh+'vh';
}