[英]onload event in home page
I have some text in a div in my home page, I want this should appear with light background (in less opacity) and the width of the div should be 50%, and will load after 2seconds after the page load. 我的主页中的div中有一些文本,我希望该文本应显示为浅色背景(不透明度较低),并且div的宽度应为50%,并在页面加载后2秒后加载。 I tried with javascript but its not working.
我尝试使用javascript,但无法正常工作。 in my css file "transition: width 2s;"
在我的css文件中“ transition:width 2s;”
var slider = document.getElementById("splasher");
function splasher(){
slider.style.width="50%";
}
I put onload in the div which is 我把加载放在div
onload ="splasher();"
any help for this 任何帮助
You need something like the following. 您需要类似以下内容的东西。 Initially we wait the DOM to be loaded.
最初,我们等待DOM加载。 Then we set a timeout for the splasher.
然后我们为启动器设置一个超时时间。
document.addEventListener("DOMContentLoaded", function(){ var slider = document.getElementById("splasher"); function splasher(){ slider.style.width="50%"; slider.style.opacity = 0.5; slider.style.display = 'block'; } setTimeout(splasher, 2000); })
<h4>Slider Demo</h4> <div id="splasher" style="display:none;">Slider</div>
Your slider might not have been rendered yet. 您的滑块可能尚未渲染。 Put in inside
onload
as well. 也放入内部
onload
。
<body onload="loaded()">
function loaded() {
var slider = document.getElementById("splasher");
function splasher(){
slider.style.width="50%";
}
}
您需要确保在加载元素之前不运行var slider=...
行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.