简体   繁体   English

如果访问者拥有移动设备,则阻止从站点加载图像...有何看法?

[英]Prevent load images from the site if the visitor has a mobile device… Opinions?

I'm using media queries, but unfortunately they are inferior to a simple "monolithic" version of the site specifically for mobile devices in the determination of user agents (like Google, Facebook and other sites). 我正在使用媒体查询,但是不幸的是,它们不如专门用于确定用户代理(例如Google,Facebook和其他网站)的移动设备的网站的简单“整体式”版本。 I do not want to do a special version, because I have translated the entire site to media queries. 我不想做一个特殊的版本,因为我已经将整个站点翻译成媒体查询。 The bottom line is that this method does not have the flexibility in terms of weight loading pages that are critical for owners of mobile phones (in our country the mobile Internet is still very at a high price + images reduce performance). 最重要的是,该方法在重量加载页面方面没有灵活性,而这对于手机所有者至关重要(在我们国家,移动互联网价格仍然很高,而且图像会降低性能)。 I have matured the idea to use Javascript, which will prevent the browser to load the image at a certain rule (eg screen resolution). 我已经成熟了使用Javascript的想法,这将阻止浏览器按特定规则(例如屏幕分辨率)加载图像。 What do you think - is actually realized, and may already have implemented? 您如何看待-实际上已经实现,并且可能已经实现? I know that the site is visited by a lot of smart people and I would really like to hear from you invaluable experience in this area! 我知道许多聪明人都访问过该站点,我真的很想听听您在该领域的宝贵经验!

Sorry for bad English. 对不起,英语不好。

UPDATE: Prevent images from loading 更新: 防止图像加载

There are a number of possible solutions to this question. 有很多可能的解决方案。 I'll list the common three below. 我将在下面列出常见的三个。

A CSS only solution, a javascript solution, and a HTML5 solution if you don't care about older browsers: 如果您不关心较旧的浏览器,则为纯CSS解决方案,JavaScript解决方案和HTML5解决方案:

CSS CSS

I would suggest moving the images into style tags and use media queries to show the appropriate image: 我建议将图像移入样式标签,并使用媒体查询来显示适当的图像:

<style>
/* mobile first */
.img {
    display:none;
}

/* tablet */
@media (min-width:600px) {
    .img {
        background:url('/path/to/image-tablet.png') no-repeat scroll 0 0 transparent;
    }
}

/* desktop */
@media (min-width:1280px) {
    .img {
        background:url('/path/to/image-large.png') no-repeat scroll 0 0 transparent;
    }
}
</style>

<div class="img"></div>

Assuming this is a dynamic webpage, you would include the css in the page withing style tags instead of in a css file. 假设这是一个动态网页,您将在CSS中添加带有style标签的CSS,而不是在CSS文件中。

HTML5 HTML5

If you are not bothered about complete browser coverage ( canIuse ), you could also use the new HTML5 picture element: 如果您不担心完整的浏览器覆盖范围( canIuse ),则还可以使用新的HTML5 picture元素:

<picture>
  <source 
    media="(min-width: 1280px)"
    srcset="/path/to/image-large.png">
  <source 
    media="(min-width: 600px)"
    srcset="/path/to/image-tablet.png">
  <img src="/path/to/image-mobile.png" /> <!-- a 1x1 empty pixel -->
</picture>

In this example, mobile browsers and browsers that do not support it will load the img tag. 在此示例中,移动浏览器和不支持该浏览器的浏览器将加载img标签。 Other browsers will load the appropriate image for the media query. 其他浏览器将为媒体查询加载适当的图像。

If you like this solution but also want it to work on older browsers, there are JS scripts you can find that will implement the feature if the browser does not support it. 如果您喜欢此解决方案,但又希望它在较旧的浏览器上运行,可以找到一些JS脚本,如果浏览器不支持该脚本,则可以实现该功能。

Javascript Java脚本

This approach feels the most hacky to me, but that is just my opinion. 这种方法对我来说最难受,但这只是我的看法。 It has a very large browser coverage, with IE supporting this method since 10. 它具有非常大的浏览器覆盖范围,从10开始,IE就支持此方法。

Create an image tag without the src attribute, and the image size versions as data attributes: 创建一个不带src属性的图像标签,并将图像大小版本作为data属性:

<img data-tablet="/path/to/tablet.png" data-desktop="/path/to/large.png" />

<script>
var $el = jQuery('img');
if(window.matchMedia("(min-width: 600px)").matches) {
    $el.attr('src', $el.data('tablet'));
}
if(window.matchMedia("(min-width: 1280px)").matches) {
    $el.attr('src', $el.data('desktop'));
}
</script>

I've used jQuery to demonstrate, but the important part of this is the window.matchMedia . 我已经使用jQuery进行了演示,但是其中的重要部分是window.matchMedia This does a media-query like check and returns a boolean value. 这会执行诸如check之类的媒体查询,并返回一个布尔值。

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

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