根据屏幕分辨率显示内容
我有一个包含一些内容的网站。根据用户的屏幕分辨率,我想显示不同的内容,以便移动设备会有一些其他内容,但网站的其余部分将是相同的。我做了一些研究,似乎唯一可能的方法是使用 javascript。我大部分时间都在编写 PHP 代码,所以我真的很糟糕 javascript,所以如果有人能为我提供一个简单的脚本那就太好了。
我需要的是这样的javascript函数:
if (screen resolution < X x X) {
show some content...
} else {
show some other content ...
}
如果javascript关闭,它应该只显示一些其他内容..:)如果有帮助的话我可以安装jquery。谢谢,
如果有 html 代码的例子就更好了。
I have a website with some content. Based on the users screen resolution i want to show different content so that mobile devices will have some other content, but the rest of the site will be the same. I did some research, and it seems like the only possible way is with javascript. I code PHP most of the time, so i really suck at javascript, so it would be nice if someone could provide me with a simple script.
What i need is a javascript function like this:
if (screen resolution < X x X) {
show some content...
} else {
show some other content ...
}
If javascript is off, it should just show some other content.. :) I can install jquery if it helps. Thanks
It would be nice with examples for the html code too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不应该使用 javascript 检测用户是否在移动设备上。我向你推荐这个 PHP 版本。您可以使用 [$_SERVER'HTTP_USER_AGENT'] 然后简单地解析出来字符串以查看它是什么类型的用户代理。我现在实际上正在实施同样的概念。
您还可以使用此类移动检测
you should NOT detect if the user is on a mobile device with javascript. i recommend you this in PHP. you can use [$_SERVER'HTTP_USER_AGENT'] and then simply parse out the string to see what kind of user agent it is. I am actually implementing this same concept right now.
you can also use this class Mobile Detect
查看 CSS at 规则。它们允许您为 CSS 规则的“命名空间”指定最大和最小宽度,在其中您可以为较小的屏幕设置不同的规则。但使用它们时要小心,因为 IE 不喜欢支持好的东西。
在我正在从事的一个项目中,如果用户代理包含某些关键字(检查来自 JS 的 HTTP 标头),我们实际上会重定向到页面的移动版本,并完全使用不同的样式表。
Check out CSS at-rules. They allow you to specify maximum and mimimum widths for a "namespace" of CSS rules, inside which you can have different rules for smaller screens. But be careful when using those, since IE doesn't like to support good things.
On a project I'm working on, we actually redirect to a mobile version of the page if the user-agent contains certain keywords(check out the HTTP headers from JS), and use a different stylesheet completely.
您可以使用 CSS 媒体查询来定位不同的屏幕分辨率。例如:
更多信息:
You can use css media queries to target different screen resolutions. eg:
More info:
你应该尝试 CSS 媒体查询
you should try CSS media queries instead
在 PHP 中不知道,但在 .Net 中,您可以检测到他们是移动访问者,然后您可以将他们重定向到网站的移动部分。
那么您真正需要做的就是重新使用现有的 Web 控件等编写小型网站。同样,不确定您在 PHP 中是否有这个概念,但我想您会的。
In don't know from PHP but in .Net you can kinda detect that they are a mobile visitor and then you can redirect them to a mobile section of the site.
Then all you really need to do is write the small site re-using your existing web controls etc. Again, unsure if you have that concept in PHP but I imagine you would.