解决思路:延时一点时间再去获取内容的高度
const oBox=document.getElementById("headbox"); setTimeout(function(){ console.log(oBox.scrollHeight) },100)
实战代码:
<script> const oBox=document.getElementById("headbox"); setTimeout(function(){ console.log(oBox.scrollHeight); let marginTop = oBox.scrollHeight; $(".main-box").css('margin-top',marginTop); },100) </script>
标签:function,盒子,log,适应,获取,const,oBox,document,scrollHeight From: https://www.cnblogs.com/zhainverer/p/17736910.html