这两个片段之间实际上有区别吗?有什么好处吗?它们都通过脚本标签嵌入到我的 html 文档中
<script>
window.onload = function () {
var masthead = document.getElementById("js-masthead"),
pageWidth = masthead.offsetWidth,
viewportHeight = window.innerHeight,
fluidPadding = (viewportHeight / pageWidth) * 100;
masthead.style.paddingTop = fluidPadding + "%";
};
</script>
和
<script>
var masthead = document.getElementById("js-masthead");
var pageWidth = masthead.offsetWidth;
var viewportHeight = window.innerHeight;
var fluidPadding = (viewportHeight/pageWidth) * 100;
masthead.style.paddingTop = fluidPadding + "%";
</script>
您能否建议任何更好的替代方案或更好的方法来编写我的 javascript?