简体   繁体   English

加载网页后是否可以禁用JS插件?

[英]Is it possible to disable a JS plugin after a webpage has been loaded?

A web page normally loads some JS scripts like (animate.je, wow.js, scrollpage.js, and so on) when loading.网页在加载时通​​常会加载一些 JS 脚本,例如(animate.je、wow.js、scrollpage.js 等)。 I have realized one of these scripts are affecting the layout on mobile devices like smartphones when rotated in landscape position.我意识到其中一个脚本在横向旋转时会影响智能手机等移动设备上的布局。 Is there any way to say: If you are in landscape mode, disable the script 'animation.js'?有什么办法可以说:如果您处于横向模式,请禁用脚本'animation.js'?

Edit: The problem pops up if the page is loaded in landscape mode and after that the device is rotated in landscape mode.编辑:如果页面以横向模式加载,然后设备以横向模式旋转,则会出现问题。

You can append script to DOM with a condition您可以使用条件将脚本附加到 DOM

Example:例子:

<html>
    <body>
    <script>
        if (!landscape) {
        const script = document.createElement('script');
        ...
        const body = document.getElementsByTagName('body')[0];
        body.appendChild(script);
        }
    </script>
    </body>
</html>

First of all you shouldn't really disable the script animation.js you should extend it to not trigger when screen is landscape.首先,您不应该真正禁用脚本animation.js您应该将其扩展为在屏幕为横向时不触发。

To do so you need to detect screen size change.为此,您需要检测屏幕尺寸的变化。 Just follow this question Detect viewport orientation, if orientation is Portrait display alert message advising user of instructions (this could be omitted if you detect landscape with each iteration/trigger/event or whenever your animation.js content is executing troubling functions)只需按照这个问题检测视口方向,如果方向是纵向显示警告消息,建议用户注意说明(如果您在每次迭代/触发器/事件时检测风景,或者每当您的 animation.js 内容正在执行令人不安的功能时,这可以省略)

Now you need to make reusable function that detects if you're in landscape (that's answered in other question as well).现在,您需要创建可重用的函数来检测您是否处于横向状态(这也在其他问题中得到了回答)。

Final step is to apply it inside animation.js - first on load, meaning you need to prevent animation.js to function from executing in above function says it's landscape.最后一步是在 animation.js 中应用它 - 首先在加载时,这意味着你需要阻止 animation.js 在上面的函数中执行,说它是横向的。 Then You need to react on switch to landscape.然后你需要对切换到横向做出反应。

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

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