简体   繁体   English

jQuery .css background-image preload

[英]jQuery .css background-image preload

I'm sorry the title is so vague, I wasn't sure the best way to describe this in a short title. 对不起标题是如此模糊,我不确定用简短标题来描述这个标题的最佳方式。 My friend has been helping me develop this simple site for myself. 我的朋友一直在帮助我为自己开发这个简单的网站。 It's to play relaxing sounds of nature. 这是为了播放轻松的自然声音。

Here is the code my friend did, now I would ask him but he is away on a bike ride across Britain and will be gone for quite some time.. 这是我朋友的代码,现在我会问他,但是他骑自行车穿越英国,将会离开很长一段时间。

$(function() {
    var forest = document.getElementById("forest");
    var dusk = document.getElementById("dusk");
    var water = document.getElementById("water");
    var storm = document.getElementById("storm");
    var playing = "";
    var muted = false;


    $('#mute').on('click', function(e) {
      $('.icon').toggleClass("off"); 
      e.preventDefault();
    });

    $("#mute").click(function() {
        if(!muted) {
            forest.volume = 0;
            dusk.volume = 0;
            water.volume = 0;
            storm.volume = 0;
            muted = true;
        } else {
            forest.volume = 1;
            dusk.volume = 1;
            water.volume = 1;
            storm.volume = 1;
            muted = false;
        }
    });

    $('.sound').click(function() {
        switch($(this).find("audio").prop("id")) {
            case "forest":
                if(playing == "forest") {
                    fade(forest);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', 'url(images/forest.jpg)');
                }).fadeTo('slow', 1.4);
                playing = "forest";
                fade(forest);
                break;
            case "dusk":
                if(playing == "dusk") {
                    fade(dusk);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', 'url(images/dusk.jpg)');
                }).fadeTo('slow', 1.4);
                playing = "dusk";
                fade(dusk);
                break;
            case "water":
                if(playing == "water") {
                    fade(water);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', 'url(images/water.jpg)');
                }).fadeTo('slow', 1.4);
                playing = "water";
                fade(water);
                break;
            case "storm":
                if(playing == "storm") {
                    fade(storm);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', 'url(images/rain.jpg)');
                }).fadeTo('slow', 1.4);
                playing = "storm";
                fade(storm);
                break;
        }
        console.log(playing);
    });

I 'think' and I say that mainly as I'm a designer, but this is the bit of concern. 我想'而且我说这主要是因为我是一名设计师,但这有点令人担忧。

$(this).css('background-image', 'url(images/rain.jpg)');

Or any of the types of sounds, as you can see the code is just multiples. 或者任何类型的声音,你可以看到代码只是倍数。

Now basically when you switch from one background to another, it doesn't load the background image smoothly for the first time, is there anyway it can load smoothly? 现在基本上当你从一个背景切换到另一个背景时,它不会第一次平滑地加载背景图像,无论如何它可以顺利加载吗? So preload that .css change? 那么预加载.css会改变吗? I don't know, sorry I'm not well informed on how this works. 我不知道,抱歉,我不知道这是如何工作的。

Any advice is really appreciated. 任何建议真的很感激。 Recommendations or whatever. 建议或其他。

You can try pre-loading the images in the html so when the time comes for them to become a background image they will be in the cache. 您可以尝试在html中预加载图像,这样当它们成为背景图像的时候,它们将在缓存中。 Here is what i mean: 这就是我的意思:

Put this div at the opening of the tag 把这个div放在标签的开头

<div class="preload">
    <img src="css/images/path-to-your-img.jpg" alt="">

    <img src="css/images/path-to-your-img.jpg" alt="">

    <img src="css/images/path-to-your-img.jpg" alt="">

    <img src="css/images/path-to-your-img.jpg" alt="">
</div><!-- /.preload -->

then add this to your css: 然后将此添加到您的CSS:

.preload { opacity: 0; position: absolute; top: -9999px; left: -9999px; }

This is one of the many ways to preload img's so its your choice. 这是预加载img的众多方法之一,因此它是您的选择。 Personaly i preffer Html and CSS over js Personaly我在js上提供Html和CSS

I have made some modifications to your code to preload the images. 我已对您的代码进行了一些修改以预加载图像。 Let me know if this works for you :) 让我知道这是否适合你:)

$(function() {
    var forest = document.getElementById("forest");
    var dusk = document.getElementById("dusk");
    var water = document.getElementById("water");
    var storm = document.getElementById("storm");
    var playing = "";
    var muted = false;
    // Preload Images
    var forestImg = new Image();
    var duskImg = new Image();
    var waterImg = new Image();
    var rainImg = new Image();
    forestImg.src="images/forest.jpg";
    duskImg.src="images/dusk.jpg";
    waterImg.src="images/water.jpg";
    rainImg.src="images/rain.jpg"; 



    $('#mute').on('click', function(e) {
      $('.icon').toggleClass("off"); 
      e.preventDefault();
    });

    $("#mute").click(function() {
        if(!muted) {
            forest.volume = 0;
            dusk.volume = 0;
            water.volume = 0;
            storm.volume = 0;
            muted = true;
        } else {
            forest.volume = 1;
            dusk.volume = 1;
            water.volume = 1;
            storm.volume = 1;
            muted = false;
        }
    });

    $('.sound').click(function() {
        switch($(this).find("audio").prop("id")) {
            case "forest":
                if(playing == "forest") {
                    fade(forest);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', forestImg.src);
                }).fadeTo('slow', 1.4);
                playing = "forest";
                fade(forest);
                break;
            case "dusk":
                if(playing == "dusk") {
                    fade(dusk);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', duskImg.src);
                }).fadeTo('slow', 1.4);
                playing = "dusk";
                fade(dusk);
                break;
            case "water":
                if(playing == "water") {
                    fade(water);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', waterImg.src);
                }).fadeTo('slow', 1.4);
                playing = "water";
                fade(water);
                break;
            case "storm":
                if(playing == "storm") {
                    fade(storm);
                    return;
                }
                $('#panda').fadeTo('slow', 0.1, function() {
                    $(this).css('background-image', rainImg.src);
                }).fadeTo('slow', 1.4);
                playing = "storm";
                fade(storm);
                break;
        }
        console.log(playing);
    });

});

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

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