通过css / js / html强制设备定向到景观? [英] force device orientation to landscape via css/js/html?

查看:120
本文介绍了通过css / js / html强制设备定向到景观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

移动网站 - 仅限强制景观/不自动旋转

我看过几个帖子

是否可以通过网页强制设备定向?

Is it possible to force the device orientation via a webpage? I.e: can I 'jam' my site as landscape?

非常感谢

推荐答案

您可以使用JavaScript。首先检查是否支持:

You can with JavaScript. First check if it is supported:

if (window.DeviceOrientationEvent) {
    console.log("DeviceOrientation is supported");
}

然后添加eventListener如果你想做旋转的事情:

Then add eventListener if you want to do something on rotation:

window.addEventListener('deviceorientation', function(eventData) {});

我想你应该触发你的身边回应时。您不能触发方向,因为设备会这样做。因此,当您的网站以横向模式加载时,您应该检测它并以横向模式呈现网站(加载自定义CSS),然后当访问者打开他的手机或平板电脑时,监听器可以使用新的CSS重新呈现您的网站文件通过JavaScript(jQuery建议这些情况)。

I guess you should trigger your side to respond when it is turned. You can't trigger the orientation, because the device does that. So when your website loads in landscape mode, you should detect it and render the site in Landscape-mode (load a custom css?) and then when the visitor turns his phone or tablet the listener can re-render your site with a new CSS file through JavaScript (jQuery advised for these situations).

还可以做到这一点,网站没有太多的动态,你可以只重新加载页面用GET方法调用改变方向,但是当然这可以是一个较慢的选项。在移动设备上使用JavaScript重新呈现整个网站通常甚至可以加热我的iPhone手机上的测试。 JavaScript引擎是快速的,并且拉出了很多CPU来平滑地渲染一切,但也在短时间内吃了很多资源,使得移动设备难以不断地保持渲染。最好的例子是当使用JavaScript调用制作HTML5游戏时。一个iPhone最终可能像在玩游戏一样,在App:P。

What you can also do it the site hasn't much dynamics in it, you can just re-load the page with GET method call to change the orientation, but off course this can be a slower option. Re-rendering a whole website with JavaScript on a mobile device very often can even heat up the phone for what I tested before on my iPhone. The JavaScript engine is fast and pulls out much CPU to render everything smoothly, but also eats many resources in a short time, making it hard for a mobile device to constantly keep the renderings up. Best example for this is when making a HTML5 game with JavaScript calls. An iPhone can eventually heat up like playing a game in an actually App :P.

这篇关于通过css / js / html强制设备定向到景观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆