简体   繁体   English

如何在页面加载之前运行特定的javascript代码(首先)?

[英]How to run a specific javascript code before page load (first of all)?

I have a js code that adds margin-top to a row with a specific class name (page with id=3) . 我有一个js代码,将margin-top添加到具有特定类名的行(id = 3的页面)。 I would like this code runs before page load because now it instantly displays the row without margin-top and then add it. 我希望这段代码在页面加载之前运行,因为现在它立即显示不带margin-top的行,然后添加它。 The row should be displayed with the margin-top already be added. 应显示该行,并已添加边距顶部。

My site is on wordpress and i added the js script on head. 我的网站在wordpress上,我在头上添加了js脚本。

I have tried window.onpaint = checkMargin(); 我已经尝试过window.onpaint = checkMargin(); but it did not work. 但它没有用。 Any idea? 任何想法?

This is my js code 这是我的js代码

<script type="text/javascript">   

//sets margin-top in serv-col --- IF not mobile version
 function addServMargin() {
 containers = document.getElementsByClassName('serv-cont');
  titles = document.getElementsByClassName('serv-col-title');
  texts = document.getElementsByClassName('serv-col-text');
  links = document.getElementsByClassName('serv-col-link');

  col_pad = '0px';
  if ( window.innerHeight > 800) { col_pad = '8.3vh'; }

  for (var i = 0; i < titles.length; i++) { 
    title_height = titles[i].offsetHeight;
    text_height = texts[i].offsetHeight;
style = window.getComputedStyle(containers[i], '');
    cont_height = style.getPropertyValue('height');
    cont_padd = style.getPropertyValue('padding-top');
    links[i].style.marginTop = 'calc(' + cont_height + ' - ' + 
cont_padd + ' - ' + col_pad + ' - ' + title_height + 'px - 1.48vh - ' + 
text_height + 'px - 127px - 5vh)';
  }
}
function checkMargin() {
 if (document.getElementsByClassName('page-id-13')[0] && window.innerWidth > 900) { addServMargin(); }
}
window.onresize = checkMargin;

</script>

I don't think making it run first will solve anything. 我认为让它先运行不会解决任何问题。 The first thing the code does is get the containers, titles, texts, and links... which it does by searching the DOM. 代码要做的第一件事是获取容器,标题,文本和链接……这是通过搜索DOM来完成的。 It then loops through the titles array and does the adjusting as needed. 然后,它遍历titles数组并根据需要进行调整。 If the script runs before any rendering is done, the DOM elements won't exist. 如果脚本在完成渲染之前运行,则DOM元素将不存在。 It 1) won't be able to find them, and 2) can't loop through them because the array will be empty. 1)无法找到它们,2)无法遍历它们,因为数组将为空。

Actually even before that, it checks for the existence of the elements it's looking for, and the screen size. 实际上,即使在此之前,它也会检查要查找的元素是否存在以及屏幕大小。 I think the only way to get it to work w/o making it look like an after thought adjustment, would be to use CSS and media sizing to set the styles in the first place. 我认为,使其不像经过深思熟虑的调整那样工作的唯一方法是,首先使用CSS和媒体大小调整来设置样式。

As I know JS is executed as the script tag is reached by Browser html interpreter. 据我所知,JS是通过浏览器html解释器到达脚本标签而执行的。 So putting it in the head tag on the first position may guarantee that it strats first, but can't guarantee that it ends execution before page loads, because the page loads asynchroniously. 因此,将其放在head标签的第一个位置上可以保证它首先分层,但是不能保证它在页面加载之前结束执行,因为页面是异步加载的。

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

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