Web应用程序屏幕方向锁定:方向锁定失败

时间:2019-04-07 22:14:03

标签: javascript screen-orientation

我从其他问题中使用了几种方法来尝试锁定Web应用程序的屏幕方向,但是锁定方向始终失败。这是我的代码:

// lock orientation to portrait
window.screen.lockOrientationUniversal = window.screen.lockOrientation || window.screen.mozLockOrientation || window.screen.msLockOrientation;

if (window.screen.lockOrientationUniversal("portrait")) {
    console.log("Orientation locked to portrait");
} else {
    console.log("Orientation lock failed.");
}

我也只用screen.而不是window.screen.尝试了一下,得到了同样的东西。请注意,这已经在最新的Firefox for Android上进行了测试,并且该网络应用不是全屏应用。

我还收到以下消息:

  

不推荐使用方向传感器。

Mozilla site提到已弃用这是有道理的。最新支持的方法是什么?

1 个答案:

答案 0 :(得分:2)

1。)Screen.lockOrientation已过时(如您提供的MDN链接所示),因此您的代码可能不适用于大多数现代浏览器。

2。)ScreenOrientation只是界面,这就是df2 = pd.DataFrame(np.array([[1,1,1,0,1,0,0], [2,1,1,0,1,1,0], [3,1,1,1,np.nan,np.nan,np.nan] ]),columns=['id', '1', '2','3','4','5','6']) id 1 2 3 4 5 6 1 1 1 0 1 0 0 2 1 1 0 1 1 0 3 1 1 1 不起作用的原因。基本上,ScreenOrientation是后台的东西,它告诉浏览器应如何构建屏幕方向对象(类似于JavaScript中的原型),但不是对象本身。

在现代浏览器中,您可以像这样访问全局屏幕方向:

C:\Users\kapro>pip install pylint Collecting pylint Using cached https://files.pythonhosted.org/packages/e9/59/43fc36c5ee316bb9aeb7cf5329cdbdca89e5749c34d5602753827c0aa2dc/pylint-2.4.4-py3-none-any.whl Collecting astroid<2.4,>=2.3.0 Using cached https://files.pythonhosted.org/packages/ad/ae/86734823047962e7b8c8529186a1ac4a7ca19aaf1aa0c7713c022ef593fd/astroid-2.3.3-py3-none-any.whl Collecting isort<5,>=4.2.5 Using cached https://files.pythonhosted.org/packages/e5/b0/c121fd1fa3419ea9bfd55c7f9c4fedfec5143208d8c7ad3ce3db6c623c21/isort-4.3.21-py2.py3-none-any.whl Collecting colorama; sys_platform == "win32" Using cached https://files.pythonhosted.org/packages/c9/dc/45cdef1b4d119eb96316b3117e6d5708a08029992b2fee2c143c7a0a5cc5/colorama-0.4.3-py2.py3-none-any.whl Collecting mccabe<0.7,>=0.6 Using cached https://files.pythonhosted.org/packages/87/89/479dc97e18549e21354893e4ee4ef36db1d237534982482c3681ee6e7b57/mccabe-0.6.1-py2.py3-none-any.whl Collecting six~=1.12 Using cached https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl Collecting wrapt==1.11.* Using cached https://files.pythonhosted.org/packages/23/84/323c2415280bc4fc880ac5050dddfb3c8062c2552b34c2e512eb4aa68f79/wrapt-1.11.2.tar.gz ERROR: Command errored out with exit status 1: command: 'c:\users\kapro\appdata\local\programs\python\python38\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\kapro\\AppData\\Local\\Temp\\pip-install-sko_km6h\\wrapt\\setup.py'"'"'; __file__='"'"'C:\\Users\\kapro\\AppData\\Local\\Temp\\pip-install-sko_km6h\\wrapt\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\kapro\AppData\Local\Temp\pip-install-sko_km6h\wrapt\pip-egg-info' cwd: C:\Users\kapro\AppData\Local\Temp\pip-install-sko_km6h\wrapt\ Complete output (11 lines): Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\users\kapro\appdata\local\programs\python\python38\lib\site-packages\setuptools\__init__.py", line 12, in <module> from setuptools.extension import Extension File "c:\users\kapro\appdata\local\programs\python\python38\lib\site-packages\setuptools\extension.py", line 7, in <module> from setuptools.dist import _get_unpatched File "c:\users\kapro\appdata\local\programs\python\python38\lib\site-packages\setuptools\dist.py", line 17, in <module> import pkg_resources File "c:\users\kapro\appdata\local\programs\python\python38\lib\site-packages\pkg_resources.py", line 1516, in <module> register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider) AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader' ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

(来源:https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation

稍后您可以使用以下方法锁定方向:

ScreenOrientation.lock("portrait")

并使用

解锁

var myScreenOrientation = window.screen.orientation;

.lock()方法会返回一个Promise,如果您想对此做任何事情,但这不在此问题的范围内,因此,我只提及它的存在。

3。)另一个可能的问题:在当前标准下,许多浏览器将要求页面处于全屏模式以锁定设备的方向。由于您的网络应用不是全屏显示,因此也有可能防止方向锁定。 (请参阅网络标准5.3:https://www.w3.org/TR/screen-orientation/