简体   繁体   English

有人可以解释这个脚本的作用吗?

[英]Could someone explain what this script does?

When I visit the homepage of the Rails website that I'm building, it takes a few seconds to load. 当我访问我正在构建的Rails网站的主页时,需要几秒钟才能加载。 Let's say after 2 seconds it has loaded the visuals after which for some reason it's busy for 2 more seconds (I think loading JavaScripts). 让我们说2秒后它已经加载了视觉效果,之后由于某种原因它再忙2秒钟(我想加载JavaScripts)。 If I scroll down between second 2 and 4, then after second 4 it automatically pops back to the top of the page. 如果我在第二个4和第四个之间向下滚动,那么在第二个4之后它会自动弹回到页面顶部。 So that's a bit annoying, especially since this happens everytime you visit the homepage (not just the first time you visit it). 所以这有点烦人,特别是因为每次访问主页时都会发生这种情况(不仅仅是第一次访问它)。

After trying to remove various parts of the page I found out the cause. 在尝试删除页面的各个部分后,我发现了原因。 I'm using a website template to develop my website. 我正在使用网站模板来开发我的网站。 This template at the top of the homepage uses the following script: 主页顶部的此模板使用以下脚本:

<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>

When I remove this script the described behaviour is gone. 当我删除此脚本时,描述的行为消失了。 But I don't know what this script does and why it's there. 但我不知道这个脚本是做什么的,为什么会这样。 Could someone perhaps advice if it's okay to remove it? 有人可能会建议,如果可以删除它吗?

Let's break it down. 让我们分解吧。

First, the code is adding a listener to the window. 首先,代码是向窗口添加一个监听器。 When the window is loaded it will trigger the function passed to addEventListener . 加载窗口时,它将触发传递给addEventListener的函数。

addEventListener("load", function() {
  // Code here will run when the page loads
});

Then the code defines a function called hideURLbar which will scroll to one pixel from the top when called (using window.scrollTo ). 然后代码定义了一个名为hideURLbar的函数,该函数在调用时将从顶部滚动到一个像素(使用window.scrollTo )。 This appears to be for mobile devices since they show the URL bar until the user scrolls down a little. 这似乎适用于移动设备,因为它们会显示URL栏,直到用户向下滚动一点。 1px must be enough. 1px必须足够。 This function is not called immediately, however. 但是,不会立即调用此函数。

function hideURLbar(){
  window.scrollTo(0,1);
}

After loading the page the function hideURLbar is passed to setTimeout with a timeout of zero milliseconds. 加载页面后,函数hideURLbar将传递给setTimeout ,超时为零毫秒。

setTimeout(hideURLbar, 0);

Using setTimeout with a zero timeout tells the browser to call hideURLbar as soon as it can, but that it should finish whatever else its doing first. 使用带有零超时的setTimeout告诉浏览器尽快调用hideURLbar ,但它应该完成其hideURLbar其他操作。 This is often used to sort of "kick" things to the background in the browser so they don't interrupt other things the browser may be doing. 这通常用于在浏览器中将某些东西“踢”到后台,这样它们就不会中断浏览器可能正在做的其他事情。

The combination of waiting for the load event and using setTimeout means this code isn't triggered until your browser is done loading everything else. 等待加载事件和使用setTimeout的组合意味着在浏览器完成加载其他所有操作之前,不会触发此代码。 By that time if you have scrolled down you will be popped back to the top of the page. 到那时,如果你向下滚动,你将会弹出回到页面顶部。

This is poor code and a better approach would be to only execute something like this if the scroll position is at the very top, like this: 这是糟糕的代码,如果滚动位置位于最顶端,更好的方法是只执行类似这样的操作,如下所示:

function hideURLbar(){
  if (window.scrollY == 0) window.scrollTo(0,1);
}

I believe that is a hack for mobile devices. 我认为这是移动设备的黑客攻击。 See, when you have a website load on the mobile, the url bar is on the top of the screen and eats up 15% of your space. 请注意,当您在移动设备上加载网站时,网址栏位于屏幕顶部,占用了15%的空间。 This (poorly designed) hack make the web scroll automatically just a tiny bit to hide this url bar. 这个(设计不佳)黑客攻击使网络自动滚动一点点来隐藏这个网址栏。 You should remove this code, it's plain dirty and will cause some problems, like the one you mention. 你应该删除这个代码,它很简单,会引起一些问题,比如你提到的问题。

This seems to be quite a common way to try and hide the address bar on mobile browsers such as iOS Safari or Chrome on Android. 这似乎是尝试隐藏移动浏览器(如iOS上的iOS Safari或Chrome)上的地址栏的常用方法。 It waits for the page to load and then scrolls down slightly in order to trigger the auto-hide feature most touch device browsers have. 它等待页面加载然后稍微向下滚动以触发大多数触摸设备浏览器具有的自动隐藏功能。

I'm 99.99% sure it doesn't do anything else so if it's causing issues for you, I'd recommend just removing it. 我99.99%肯定它没有做任何其他事情,所以如果它给你带来问题,我建议你删除它。

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

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