根据设备(手机/平板电脑等)控制元素的加载方式



我需要使用Javascript/jQuery来控制元素加载的方式,这意味着如果我在iPhone或Android手机上以及iPad和平板电脑上。我不想使用CSS媒体查询,我需要Javascript或jQuery。任何提示或链接阅读将是很好的,谢谢

您可以检测设备宽度并从那里更改设置/元素?

var viewSize = $(window).width();
/* if is below 481px  then it might be a phone :) */
if (viewSize < 481) {
  //something special
}
...

我使用屏幕。宽度,它工作得很好。只要确保您的脚本在页面结束之前正确,以确保所有元素都被加载。下面是代码:

<script type="text/javascript">
$(document).ready(function() {
if ((screen.width>720)) {
document.getElementById("photos").style.marginLeft="110px";
document.getElementById("videos").style.marginRight="100px";
}
});
</script>

最新更新