javascript – 如何使用树枝检测屏幕大小或移动/桌面

前端之家收集整理的这篇文章主要介绍了javascript – 如何使用树枝检测屏幕大小或移动/桌面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在使用twig作为我的模板引擎,我想在使用移动和桌面加载网站时加载不同的图像URL.是否有捷径可寻?

所以我想做这样的事情:@H_403_3@

  1. {% if (mobile) %}
  2. <img src="{{ picture.getLowresimageurl() }}"/>
  3. {% else %}
  4. <img src="{{ picture.getMedresimageurl() }}"/>
  5. {% endif %}

有没有办法做到这一点?@H_403_3@

解决方法

您可以使用 MobileDetectBundle检测移动设备,管理移动视图并重定向到移动设备和平板电脑版本

Twig Helper@H_403_3@

  1. {% if is_mobile() %}
  2. {% if is_tablet() %}
  3. {% if is_device('iphone') %} # magic methods is[...]

Twig的例子@H_403_3@

  1. {% if is_mobile_view() %}
  2. {% extends "MyBundle:Layout:mobile.html.twig" %}
  3. {% else if is_tablet_view() %}
  4. {% extends "MyBundle:Layout:tablet.html.twig" %}
  5. {% else if is_full_view() or is_not_mobile_view() %}
  6. {% extends "MyBundle:Layout:full.html.twig" %}
  7. {% endif %}

猜你在找的JavaScript相关文章