简体   繁体   English

如何使Javascript函数在Rails应用程序中工作?

[英]How to get Javascript function to work in Rails app?

I'm getting this error message: 我收到此错误消息:

Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'. 

and this second line of code is being flagged 第二行代码被标记

var nav                 = document.querySelector('.navv'),
nav_height          = getComputedStyle(nav).height.split('px')[0],

I'm in the middle of converting a static webpage into a Ruby on Rails app and this code works just fine on the original project, but I'm getting this error message on Rails. 我正在将静态网页转换为Ruby on Rails应用程序,并且此代码在原始项目上可以正常工作,但是我在Rails上收到此错误消息。 How do I get this code working again? 如何使此代码再次起作用?

Here is the rest of the function for refrence: 这是其余的Refreence函数:

var nav                 = document.querySelector('.navv'),
nav_height          = getComputedStyle(nav).height.split('px')[0],
nav_links           = document.querySelector('.nav-links'),
//nav_links_height    = getComputedStyle(nav_links).height.split('px')[0],
sticky_class        = 'is-fixed';
//unfixed             = 'unfixed'


function stickyScroll(e) {

if( window.pageYOffset > (nav_height) ) {
nav_links.classList.add(sticky_class);
}

if( window.pageYOffset < (nav_height) ) {
nav_links.classList.remove(sticky_class);

 }
}

You are not actually checking if the element exists before calling window.getComputedStyle . 在调用window.getComputedStyle之前,您实际上并没有检查该元素是否存在。

Document.querySelector()
Returns null if no matches are found; 如果找不到匹配项,则返回null否则返回null otherwise, it returns the first matching element. 否则,它返回第一个匹配的元素。
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector https://developer.mozilla.org/zh-CN/docs/Web/API/Document/querySelector

When dealing with the DOM you should always code defensively and ensure that an element actually exists before trying to use it. 处理DOM时,应始终进行防御性编码,并确保在尝试使用元素之前确实存在该元素。

var nav = document.querySelector('.navv'),
  nav_height,
  nav_links,
  sticky_class = 'is-fixed';

if (nav) {
  // Calling global functions explicitly is good style
  nav_height = window.getComputedStyle(nav).height.split('px')[0];
  nav_links = document.querySelector('.nav-links');
}

function stickyScroll(e) {
  if( window.pageYOffset > (nav_height) ) {
    nav_links.classList.add(sticky_class);
  }

  if( window.pageYOffset < (nav_height) ) {
    nav_links.classList.remove(sticky_class);
  }
}

Better yet would be to refactor: 更好的是重构:

function stickyScroll(e, nav_links) {
  var nav_height = e.offsetHeight,
      sticky_class = 'is-fixed';
  if( window.pageYOffset > (nav_height) ) {
    nav_links.classList.add(sticky_class);
  }
  if( window.pageYOffset < (nav_height) ) {
    nav_links.classList.remove(sticky_class);
  }
}

Because if you are using this as an event handler when the window is resized that you will need reevaluate the height of .navv . 因为如果在调整窗口大小时将其用作事件处理程序,则需要重新评估.navv的高度。

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

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