延迟Javascript或div直到页面加载

时间:2013-02-18 07:33:06

标签: javascript html settimeout delay pageload

我一直在寻找一个解决方案,尝试一些建议,但似乎没有一个适用于我已经或不理解解决方案的脚本,所以希望有人可以提供帮助。< / p>

在head标签中我有下面的javascript脚本。它将页面上的第8个div从底部滑入页面。但是,当您访问该页面时,您可以在它滑入页面之前快速浏览一下该div(文本块)的最终位置内容。如何在卸载主图像时摆脱此快速预览并使文本块div滑入?

链接到网站:http://www.estilosalon.com.au/estilo-salon-philosophy2.html

<script type="text/javascript">
i=-700;
var c;
function f(){
    c=setInterval(function(){inv()},5)
}
function inv()
{
    if(i!=0)
    {
        i+=5;
        document.getElementsByTagName("div")[8].style.bottom=i+"px";
    }
    else
    {
        clearInterval(c);
        document.getElementsByTagName("button")[0].parentNode.removeChild(document.getElementsByTagName("button")[0]);
    }
}
</script>

提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以将功能附加到正文的加载事件<body onload="f()">,并在您的css中将div [8]设置为display: none,然后将其设置为document.getElementsByTagName("div")[8].style.display = "block" f() 1}}。

但是,我强烈考虑使用jQuery和$().animate函数来设置div的动画,为div分配一个id(如果你在它之前添加更多的div元素,你的代码就会中断),以及使用jQuery的$(document).ready()函数来触发初始代码而不是使用<body onload="">。采取这些步骤将有助于您的代码在多个浏览器中统一执行。

答案 1 :(得分:0)

您可以尝试以下方法:

如果没有js:

,请在<html>标记中添加一个类作为后备广告
<html xmlns="http://www.w3.org/1999/xhtml" class="no-js">

将此代码放在no-js

之前,将js类替换为i=-700;
var html = document.documentElement;
html.className=html.className.replace(/\bno-js\b/,'') + 'js';

将其添加到样式表

html.js #text_box_philosophy{
    bottom: -700px;
}
相关问题