简体   繁体   English

如何在加载JavaScript幻灯片之前停止加载图片?

[英]How to stop my images loading before the javascript slideshow loads?

I am new to this and have a quick question. 我对此并不陌生,并且有一个简短的问题。 I have a very simple slideshow on my home page. 我的主页上有一个非常简单的幻灯片演示。 The problem I am having is the first second or so of loading the page, the slideshow images show up 1 on top of the other. 我遇到的问题是加载页面的第一秒左右,幻灯片图像在另一个页面上显示1。 Once the first second or so of loading the page is over, everything works fine. 加载页面的第一秒左右结束后,一切正常。 Here is my code for the javascript: 这是我的javascript代码:

<script type="text/javascript" language="javascript">
        window.onload = function () {
            var rotator = document.getElementById("slideshow_content");
            var images = rotator.getElementsByTagName("img");
            for (var i = 1; i < images.length; i++) {
                images[i].style.display = "none";
            }
            var counter = 1;
            setInterval(function () {
                for (var i = 0; i < images.length; i++) {
                    images[i].style.display = "none";
                }
                images[counter].style.display = "block";
                counter++;
                if (counter == images.length) {
                    counter = 0;
                }
            }, 3000);
        };
    </script>

Here is the body code: 这是正文代码:

<div id="slideshow">
<div id="slideshow_content" style="padding-top:10px; padding-bottom:10px;">
        <img alt="" src="/images/slide1.jpg" />
        <img alt="" src="/images/slide2.jpg" />
</div>
</div>

Here is the CSS for the 2 divs: 这是2个div的CSS:

#slideshow {position:relative; top:0; left:0; max-width:1680px; height:342px; margin:0     auto; padding:0; background-color:#070707;}
#slideshow_content {width:960px; max-height:322px; margin:0 auto; padding:0;}

How would you recommend fixing this? 您将如何建议解决此问题?

I would set the CSS to hide the slideshow on page load, and then when the slideshow starts it could be unhidden. 我将设置CSS以在页面加载时隐藏幻灯片,然后在幻灯片开始播放时将其隐藏。

This would also mean that if someone had javascript disabled they wouldn't get a weird looking pile of images. 这也意味着,如果有人禁用了javascript,他们将不会得到看起来很奇怪的图像。

Alternatively you could hide all but the first image using CSS, so that even if someone had javascript disabled they would see the first image. 或者,您可以使用CSS隐藏除第一个图像以外的所有图像,这样,即使有人禁用了javascript,他们也会看到第一个图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM