简体   繁体   English

浏览器下载页面时是否可以显示Twitter Bootstrap进度栏?

[英]Is it possible to display a Twitter Bootstrap Progress bar while the browser downloads the page?

I have a site with lots of large images and I'd like to display a twitter bootstrap progress bar while everything is loading. 我的网站上有很多大图片,我想在加载所有内容时显示一个twitter引导进度栏。

Is it possible to do this? 是否有可能做到这一点? Does jquery have a way of tracking the progress of a page loading? jQuery是否可以跟踪页面加载进度?

I'd like to display just a progress bar until all the css, js, and images, etc. have be downloaded. 我想只显示进度条,直到所有的css,js和图像等都已下载。

You can try something like this: 您可以尝试如下操作:

HTML HTML

<div class="progress progress-striped active">
    <div class="bar"></div>
</div>

<div class="imagelist">
    <img src="http://studio7designs.com/wp-content/uploads/green-grass.jpeg"/>
    <img src="http://studio7designs.com/wp-content/uploads/rowboat-600x398.jpg"/>
    <img src="http://studio7designs.com/wp-content/uploads/blue-wheat-grass.jpeg"/>
    <img src="http://studio7designs.com/wp-content/uploads/rocks.jpg"/>
    <img src="http://studio7designs.com/wp-content/uploads/sun-grass-golden.jpg"/>
    <img src="http://studio7designs.com/wp-content/uploads/blue-water-photo.jpg"/>
    <img src="http://studio7designs.com/wp-content/uploads/flight.jpg"/>
</div>

Javascript 使用Javascript

$.fn.imagesLoaded = function (callback, onEach) {
    var elems = this.filter('img'),
        len = elems.length,
        blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";

    elems.bind('load.imgloaded', function () {
        if(typeof onEach === 'function'){
            onEach(len, elems.length); // runs on every image load
        }

        if (--len <= 0 && this.src !== blank) {
            elems.unbind('load.imgloaded');
            callback.call(elems, this);
        }
    }).each(function () {
        // cached images don't fire load sometimes, so we reset src.
        if (this.complete || this.complete === undefined) {
            var src = this.src;
            // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
            // data uri bypasses webkit log warning (thx doug jones)
            this.src = blank;
            this.src = src;
        }
    });

    return this;
};

$(document).ready(function () {
    var progress = $('.progress');
    var progressbar = progress.children('.bar');

    $('.imagelist img').imagesLoaded(function () {
        progressbar.width('100%');
        progress.fadeOut();
    }, function (left, total) {
        progressbar.width((total - left) / total * 100 + '%');
    });
});

I took the .imagesLoaded() code written by Paul Irish, and added an additional callback that's triggered every time an image is loaded. 我使用了Paul Irish编写的.imagesLoaded()代码,并添加了一个额外的回调,该回调在每次加载图像时触发。 So for every image that's loaded, the progress bar progresses. 因此,对于每个加载的图像,进度条都会进行进度。

It's not entirely accurate because it doesn't track how much of any particular file is loaded, and I'm not entirely sure that's possible without complicating this code, so the progress bar only changes when an image is loaded rather than while it's loading. 它并不完全准确,因为它不跟踪任何特定文件的加载量,而且我也不完全确定在不使该代码复杂化的情况下是可能的,因此进度条仅在加载图像时(而不是在加载时)改变。

I hope this helps. 我希望这有帮助。

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

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