我正在研究爲移動應用程序設計Rails應用程序樣式的方法。Rails 3中的移動樣式切換,幫助器方法與媒體查詢
這個想法很常見,使用移動瀏覽器的一套樣式和傳統的一套樣式。
從我可以告訴有Rails中這樣做的兩種基本方式:
使用一個輔助方法來檢測用戶代理,然後瓶坯的開關。
application_controller.rb
private
def mobile?
request.user_agent =~ /Mobile|webOS/
end
helper_method :mobile?
application.html.erb
<% unless mobile? %>
<%= stylesheet_link_tag "core" %>
<%else%>
<%= stylesheet_link_tag "mobile" %>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<% end%>
或者使用樣式表
body {
// basic styles
}
@media all and (max-width: 600px) {
body {
// extra styles for mobile
}
}
@media all and (min-width: 600px) {
body {
// extra styles for desktop
}
}
我的問題是什麼是權衡媒體查詢?一種方法通常更好,或者兩者都有很好的用例。
在此先感謝