简体   繁体   English

使用css或javascript响应式网页设计?

[英]Responsive web design by using css or javascript?

I am going to make a website that is using responsive design. 我打算建立一个使用响应式设计的网站。 I read some information about css media query. 我读了一些关于css媒体查询的信息。 What I wanna do is that the layout of my webpage should looks difference by using different devices (like PC, tablets or smartphones). 我想做的是我的网页布局应该通过使用不同的设备(如PC,平板电脑或智能手机)看起来不同。

If I use media query to determine the device by using the width of the screen (in pixel), I always worry about if there will be a new device using a extremely high ppi screen. 如果我使用媒体查询通过使用屏幕的宽度(以像素为单位)来确定设备,我总是担心是否会有使用极高ppi屏幕的新设备。 That device may threat as tablet or something like PC? 该设备可能会像平板电脑或类似PC一样受到威胁?

Another solution that is using the user agent to determine the device category by using userAgent. 另一种使用用户代理通过使用userAgent确定设备类别的解决方案。 There's also a problem is that if the device not interpret the javascript fine then the page maybe broken. 还有一个问题是,如果设备不能解释javascript,那么页面可能会被破坏。

Any great solution that can solve my worries above? 任何可以解决我上述担忧的出色解决方案? Or Which solution is better? 或者哪种解决方案更好?

Or I misunderstand the method of using media query? 或者我误解了使用媒体查询的方法?

Thank you. 谢谢。

CSS is the way to go, and you can always provide fallback for browsers that don't support media queries using a js plugin like css3mediaqueries.js , but relying on JS solely to make your website responsive is a risk because you can't tell for sure if the user will have Javascript enabled, and when it's not enabled it's not going to be responsive anymore. CSS是可行的方法,你可以随时使用像css3mediaqueries.js这样的js插件为不支持媒体查询的浏览器提供回退,但仅依靠JS来使你的网站响应是一种风险,因为你无法分辨确定用户是否启用了Javascript,并且当它启用时,它将不再具有响应性。

Also, it's considered best practice now to use em values for media queries instead of pixels to make sure your website always scales right. 此外,现在最好的做法是使用em值进行媒体查询而不是像素,以确保您的网站始终正确缩放。 More on this topic in this article . 本文中有关此主题的更多信息。

Another tip is that you determine the media query values according to your content's best break points instead of device dimensions, that way you also make sure your content will always look right no matter how many new devices are made. 另一个提示是,您可以根据内容的最佳断点而不是设备尺寸来确定媒体查询值,这样您无论制作了多少新设备,都可以确保您的内容始终正确。

Hope that helped :) 希望有帮助:)

id' personally use CSS and set min-width and max-width. id'亲自使用CSS并设置min-width和max-width。 Most responsive designs now days use CSS. 大多数响应式设计现在使用CSS。 This way if there is a new device on the market it will just adjust according to it's screen size. 这样,如果市场上有新设备,它将根据它的屏幕尺寸进行调整。

@media screen and (max-width:480px) { }
@media screen and (min-width:481px) { ) etc... etccc....

我更喜欢使用Twtitter Bootstrap而不是使用CSS3媒体查询来处理各种设备。

I prefer use media queries in CSS. 我更喜欢在CSS中使用媒体查询 Just write the queries after the default CSS... 只需在默认CSS之后编写查询 ...

@media screen and (max-width: 600px) { write here only the elements that must change de code for responsive performance } @media屏幕和(max-width:600px){ 在这里只写下必须更改代码以响应响应性能的元素 }

.logo {
width: 200px;
height: 80px;
background: url... ;
background-size: 100%;
margin: 0;
}

@media screen and (max-width: 600px) {
.logo {
width: 150px;
height: 60px;
margin: 0 auto;
}
}

Don't forget to insert the viewport code into the head/HTML. 不要忘记将视口代码插入head / HTML。

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=false" />

Try jRWD, a JavaScript-only module I designed recently. 试试jRWD,这是我最近设计的JavaScript模块。 It uses 12 lines of pure JavaScript and 2 small helper functions. 它使用12行纯JavaScript和2个小帮助函数。 It's available on GitHub, at https://github.com/BlackMagic/jRWD . 它可以在GitHub上获得, 网址是https://github.com/BlackMagic/jRWD

If you want to see jRWD in action, visit http://ieee-qld.org . 如果您想查看jRWD的运行情况,请访问http://ieee-qld.org Make sure you inspect the source code. 确保检查源代码。 Minimal JavaScript, minimal CSS stylesheet. 最小的JavaScript,最小的CSS样式表。 No jQuery either. 也没有jQuery。

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

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