【问题标题】:Access to Variable Outside of Function Logic访问函数逻辑之外的变量
【发布时间】:2017-09-11 14:22:01
【问题描述】:
   $(window).on("load resize", function () {
      if ($(window).width() < 768) {
          menuHeight = '0px';
          contactHeight = '100%';
      } else {
          menuHeight = ($('#desktop-menu').height() + 'px').toString();
          contactHeight = ($('.get-in-touch-home').outerHeight() + 'px').toString();
      }
   });

我想在此函数之外访问这两个变量(menuHeight、contactHeight),以便在其他地方使用。我正在测量模态的顶部高度,但它不是对调整大小做出响应,而不仅仅是加载(在这种情况下,似乎一个简单的 If/Else 可以工作。)我可能需要对这里的范围进行一些教育。谢谢!!

【问题讨论】:

  • 如果你还没有在某处声明menuHeightcontactHeight,那么该代码将成为The Horror of Implicit Globals 的牺牲品(这是我贫血的小博客上的帖子)。一定要声明你的变量。 :-)

标签: jquery variables scope global


【解决方案1】:

您需要在之前定义它们:

var menuHeight = '';
var contactHeight = '';

$(window).on("load resize", function () {
    if ($(window).width() < 768) {
        menuHeight = '0px';
        contactHeight = '100%';
    } else {
        menuHeight = ($('#desktop-menu').height() + 'px').toString();
        contactHeight = ($('.get-in-touch-home').outerHeight() + 'px').toString();
    }
});

你可以使用一个函数:

function scroll() {
  var menuHeight;
  var contactHeight;

  if ($(window).width() < 768) {
    menuHeight = '0px';
    contactHeight = '100%';
  } else {
    menuHeight = ($('#desktop-menu').height() + 'px').toString();
    contactHeight = ($('.get-in-touch-home').outerHeight() + 'px').toString();
  }

  getHeight(menuHeight,contactHeight)

}

function getHeight(menuHeight,contactHeight) {
  console.log(menuHeight,contactHeight)
}

$(window).scroll(scroll);

用于全球访问(不推荐)

window.menuHeight = '';
window.contactHeight = '';

【讨论】:

  • 不确定我是否可以使用“窗口”,因为这是一个 Node 项目,至少我在这里读到了; )。
  • @ChrisJohnson:您问题中的代码显然是基于浏览器的,并且已经在使用window。 :-) 您可能正在使用 Node 编写它的服务器部分,但这部分是基于浏览器的。但是不要使用window.menuHeight之类的;这样做需要它们是全局变量,这是一个坏事™。
  • @T.J.Crowder 他们是邪恶的 :-) 万不得已才使用全局变量,但大多数时候你不需要它。
【解决方案2】:

您在函数之外定义它们。此外,由于load 在页面加载过程中发生得相当晚,因此您需要主动初始化它们,然后更新它们以响应loadresize。这表明您需要一个可重用的功能:

var menuHeight;                            // Defining...
var contactHeight;                         // ...them
grabHeights();                             // Proactive
$(window).on("load resize", grabHeights);  // And on events

function grabHeights() {
    if ($(window).width() < 768) {
        menuHeight = '0px';
        contactHeight = '100%';
    }
    else {
        menuHeight = ($('#desktop-menu').height() + 'px').toString();
        contactHeight = ($('.get-in-touch-home').outerHeight() + 'px').toString();
    }
}

如果您的代码还没有在作用域函数中,您可能希望将它放在一个中:

(function() {
    var menuHeight;
    var contactHeight;
    grabHeights();
    $(window).on("load resize", grabHeights);

    function grabHeights() {
        if ($(window).width() < 768) {
            menuHeight = '0px';
            contactHeight = '100%';
        }
        else {
            menuHeight = ($('#desktop-menu').height() + 'px').toString();
            contactHeight = ($('.get-in-touch-home').outerHeight() + 'px').toString();
        }
    }
})();

...因为全局变量是一件坏事™。 :-)

你会看到人们告诉你继续挂钩事件,然后手动触发它:

$("selector").on("event", function() {
    // ...
}).triger("event");

我不是粉丝。如果需要调用函数,直接调用即可。

【讨论】:

  • 将继续尝试这些。谢谢!!
  • 所以,我希望能够在下面的代码中使用这个“更新”变量,但它似乎只是获取初始值。 $("#contact-modal-1").animatedModal({ modalTarget:'contact-modal', animatedIn:'slideInUp', animatedOut:'slideOutDown', overflow: 'scroll', position: 'fixed', top: menuHeight, animationDuration:'.7s', color: '#EBEBEB' }); 为糟糕的格式道歉。
  • @ChrisJohnson 尝试我的代码中的 getHeight() 函数。
  • @ChrisJohnson:它不会只获得初始值,除非你在它具有初始值时这样做。如果您需要在值更改时更新该模式,则需要在更新值后从更改处理程序中调用某些内容。
  • 谢谢大家,试试这些!
猜你喜欢
  • 1970-01-01
  • 2014-04-09
  • 2014-07-11
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 2021-05-28
  • 2017-05-24
  • 1970-01-01
相关资源
最近更新 更多