在移动设备上查看网站时,如何强制网站处于横向模式? [英] How do you force a website to be in landscape mode when viewed on a mobile device?
问题描述
我已经看到了很多与此有关的问题,但是在Razor/MVC网站(如何使用DefaultDisplayMode类确定移动网站)中,如何执行此操作没有明确的答案.另外,很多答案只是代码片段,没有代码去向的详细信息(在控制器,视图,CSS等中)?
I've seen a lot of questions about this, but no definitive answer on how to do this in a Razor/MVC site where the mobile site is determined using the DefaultDisplayMode class. Also, a lot of the answers are just code snippets without details on where the code goes (is it in the controller, view, CSS, etc.?)
因此,首先,有一个全局文件调用EvaluateDisplayMode():
So to start off, there is a Global file that calls EvaluateDisplayMode():
Global.asax.cs
Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
BundleConfig.RegisterBundles(BundleTable.Bundles);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
DeviceConfig.EvaluateDisplayMode();
}
}
App_Start文件夹中的EvaluateDisplayMode根据GetOverriddenUserAgent()类型设置移动和桌面类:
The EvaluateDisplayMode in the App_Start folder sets up a mobile and desktop class based on the GetOverriddenUserAgent() type:
DeviceConfig.cs
DeviceConfig.cs
public static class DeviceConfig
{
const string DeviceTypePhone = "Mobile";
public static void EvaluateDisplayMode()
{
DisplayModeProvider.Instance.Modes.Insert(0,
new DefaultDisplayMode(DeviceTypePhone)
{
ContextCondition = (ctx => (
(ctx.GetOverriddenUserAgent() != null) &&
(
(ctx.GetOverriddenUserAgent().IndexOf("android", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ctx.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ctx.GetOverriddenUserAgent().IndexOf("Window Phone", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ctx.GetOverriddenUserAgent().IndexOf("Blackberry", StringComparison.OrdinalIgnoreCase) >= 0)
)
))
});
}
}
每个页面都有一个名为_AdminMaster.Mobile.cshtml的布局/母版"页面,该页面使用名为PhoneCommon.css的CSS文件. CSS没什么特别的(移动vs.桌面;即没有媒体查询;我没有纠正CSS,我是从客户端使用的另一个开发人员那里继承过来的),只是高度并没有一直延伸到底部.页.这是CSS的一部分(很长):
Each page has a Layout/Master page called _AdminMaster.Mobile.cshtml which uses a CSS file called PhoneCommon.css. There's nothing special about the CSS (mobile vs. desktop; i.e. no media queries; I didn't right the CSS, I inherited it from another developer the client used) except the height doesn't extend all the way to the bottom of the page. Here's a portion (it's quite lengthy) of the CSS:
#main #content {
float:left;
display:block;
width:100%;
margin:0 auto;
position: relative;
top:7px;
left:0;
border:1px solid #000;
box-shadow:0 0 0 1px #464646;
-webkit-box-shadow:0 0 0 1px #464646;
-moz-box-shadow:0 0 0 1px #464646;
background:url(../images/login_bg.png) repeat top center;
border-radius:5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
position:relative;
min-height:300px; padding-bottom:30px;
}
最好的答案似乎来自@uʍopǝpısdn这里:
The best answer seems to come from @uʍopǝpısdn here:
,但是它不起作用.我将旋转代码放在PhoneCommon.css中.这样做是迫使页面进入左上角,并且不会旋转页面.
, but it doesn't work. I put the rotate code in PhoneCommon.css. What it does is force the page into the upper left hand corner and doesn't rotate it.
以下是我查看过的其他一些网站,但没有一个给出完整的答案.有人可以告诉我如何让我的网站强制在移动设备上进行美化吗?谢谢.
Below are some other sites I looked at, but none show a complete answer. Can someone show me how to get my site to force to landscape on mobile devices? Thanks.
是否有可能防止浏览器中的iPhone/iPad方向改变了吗?
http://www.w3. org/TR/2004/CR-css-print-20040225/#section-properties
推荐答案
简短的回答,您不能也不应通过网站控制用户的操作系统行为.
Short answer, you can't and you shouldn't control a user's OS behaviour via a website.
您可以使用以下问题的答案显示警告: 强制网站仅以横向模式显示
You can display a warning using the answer on the following question: forcing web-site to show in landscape mode only
Or you can force your layout to look like landscape even in portrait mode using the following code (taken from http://www.quora.com/Can-I-use-Javascript-to-force-a-mobile-browser-to-stay-in-portrait-or-landscape-mode):
@media screen and (orientation:landscape) {
#container {
-ms-transform: rotate(-90deg); /* IE 9 */
-webkit-transform: rotate(-90deg); /* Chrome, Safari, Opera */
transform: rotate(-90deg);
width: /* screen width */ ;
height: /* screen height */ ;
overflow: scroll;
}
}
不过,按照第二个站点的建议,您应该尝试使站点看起来不错,但是用户希望查看它.
As the second site recommends though, you should try and make your site look good however the user wishes to view it.
这篇关于在移动设备上查看网站时,如何强制网站处于横向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!