gpt4 book ai didi

javascript - 如何在页面完全加载之前将 jQuery 效果应用于元素?

转载 作者:太空宇宙 更新时间:2023-11-04 08:47:48 25 4
gpt4 key购买 nike

我想使用 jQuery 自定义元素的 heightheight 正在更改,但每次在页面加载时都会显示效果(如闪烁效果)。如何解决这个问题?

$(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com