【发布时间】:2010-11-23 23:59:22
【问题描述】:
是否可以使用 JQuery 根据用户的屏幕分辨率重定向到不同的 index.html?例如,宽度为 1024px 的屏幕会转到 1024.html,而其他所有屏幕都会转到正常的 index.html?
我最好喜欢为此使用 jQuery。任何帮助将不胜感激!
谢谢!
【问题讨论】:
是否可以使用 JQuery 根据用户的屏幕分辨率重定向到不同的 index.html?例如,宽度为 1024px 的屏幕会转到 1024.html,而其他所有屏幕都会转到正常的 index.html?
我最好喜欢为此使用 jQuery。任何帮助将不胜感激!
谢谢!
【问题讨论】:
你不需要 jQuery。
你可以使用screen.width,which works in all browsers:
if (screen.width <= 1024) window.location.replace("http://www.example.com/1024.html")
else window.location.replace("http://www.example.com/index.html")
【讨论】:
if ($(window).width() <= 1024) location.href = "1024.html";
【讨论】:
$(document).ready(function() {
if (screen.width >= 1024) {
window.location.replace("http://example.com/1024.html");
}
else {
window.location.replace("http://example.com/index.html");
}
});
answer to this question 中关于window.location.replace 和window.location.href 之间区别的参考说明。
【讨论】:
使用上述答案中的else 语句如果用于 index.html(即用户默认登陆的页面)会导致无限循环
对于简单的桌面页面到移动页面重定向
在您的 index.html 的 <head> 中:
<script>
if (screen.width <= 1024) {
window.location.replace("http://www.yourMobilePage.html");
}
</script>
【讨论】:
Ben,它似乎有效,但有 2 个问题: 如何保持用户所在的实际页面?
如何自动加载脚本?调整大小时,必须刷新页面才能重定向。
【讨论】: