Javascript to only display animated gif when loaded

后端 未结 3 662
后悔当初
后悔当初 2020-12-11 11:36

I have an animated gif banner on my website that is around 2MB. For people with slow connections, I want to include some Javascript that will only display (and start playing

3条回答
  •  悲哀的现实
    2020-12-11 11:46

    How about a jquery script like http://jqueryfordesigners.com/image-loading/

    So you would do

    
    
    
    $(function () {
        var img = new Image();
        $(img).load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $(this).hide();
            $('#loader').removeClass('loading').append(this);
            $(this).fadeIn();
        }).error(function () {
            // notify the user that the image could not be loaded
        }).attr('src', 'myimage.jpg');
    });
    

    Note, you dont need to create a new image element if you already have one set. If you create one already then you can just use a selector. something like $('#myimage').load(... which is an image tag with an id called myimage.

提交回复
热议问题