简体   繁体   English

我可以强制CSS加载带有媒体查询的图像吗?

[英]Can I force CSS to load an image with Media Queries?

here my css: 在这里我的css:

html {
    background: url(../img/header_mobile.jpg) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

/* Small devices (tablets, 768px and up) */
@media (min-width: 768px) {
    html {
        background: url(../img/header_tablet.jpg) no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
}

If my screen is bigger than 768px, my browser load header_tablet.jpg only 如果我的屏幕大于768px,我的浏览器只加载header_tablet.jpg 在此输入图像描述 Can I force my browser to load header_mobile.jpg before with media queries ? 在使用媒体查询之前,我可以强制我的浏览器加载header_mobile.jpg吗?

edit: for know what I'm doing is this: 编辑:知道我在做什么是这样的:

<script type="text/javascript">
    $(window).bind("load", function () {
        if ($("html").width() > 767) {
            $('html').css('background-image', 'url({{ site.url }}/img/header_tablet.jpg)');
        }
    });
</script>

I try to find a media queries solution. 我试图找到一个媒体查询解决方案。 on mobile load only my small picture. 在移动负载只有我的小图片。 On tablet start to load my mobile image and load my big image. 在平板电脑上开始加载我的移动图像并加载我的大图像。

Yes. 是。 Pick a method. 选择一种方法。

  1. HTML HTML

     <img src="img/header_mobile.jpg" width=0 height=0 /> 
  2. JavaScript JavaScript的

     img = new Image(); img.src = "img/header_mobile.jpg"; 
  3. CSS CSS

     html:after { background: url(../img/header_mobile.jpg); opacity: 0; } 

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

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