簡體   English   中英

我可以強制CSS加載帶有媒體查詢的圖像嗎?

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

在這里我的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;
    }
}

如果我的屏幕大於768px,我的瀏覽器只加載header_tablet.jpg 在此輸入圖像描述 在使用媒體查詢之前,我可以強制我的瀏覽器加載header_mobile.jpg嗎?

編輯:知道我在做什么是這樣的:

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

我試圖找到一個媒體查詢解決方案。 在移動負載只有我的小圖片。 在平板電腦上開始加載我的移動圖像並加載我的大圖像。

是。 選擇一種方法。

  1. HTML

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

     img = new Image(); img.src = "img/header_mobile.jpg"; 
  3. 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