我想使用 jQuery 自定义元素的 height
。 height
正在更改,但每次在页面加载时都会显示效果(如闪烁效果)。如何解决这个问题?
$(document).ready(function() {
$('.jQuery-Container').height('100px');
});
.jQuery-Container {
background-color: Red;
height: 700px;
width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="jQuery-Container">
This is text..!!
</div>
div 的页面加载高度正在更改,但在页面完全加载之后。我想在页面完全加载之前更改高度。
你可以看到我的jsfiddle here .
最佳答案
你可以这样做,在元素之后立即运行一个脚本,只要它是普通的 javascript,它就可以工作。
.JS-Container {
background-color: Red;
height: 700px;
width: 200px;
}
<head>
<script>
function changeThis(sel) {
document.querySelector(sel).style.cssText = 'height: 100px;';
}
</script>
</head>
<body>
<div class="JS-Container">
This is a sample text
</div>
<script>changeThis('.JS-Container');</script> <!-- this will run before page is fully
loaded, so no "blink" will occur -->
</body>
关于javascript - 如何在页面完全加载之前将 jQuery 效果应用于元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654492/