简体   繁体   English

无法更改我的变量值(jQuery)

[英]can't change my variable value (jQuery)

I'm building a wordpress theme and I'm developing a sidebar which stays fixed during scrolling and goes up when you reach the comments. 我正在建立一个wordpress主题,并且正在开发一个侧边栏,该边栏在滚动过程中保持固定,并在您到达评论时向上移动。 Here's a part of my jQuery code: 这是我的jQuery代码的一部分:

jQuery(window).bind('scroll', function () {
  var flag = '';

  if (calcTop() === true && calcBottom() === false && flag !== 'false')
    {
      jQuery('#aside-sidebar').css({'position': 'fixed', "top": "59px"});
    } 
  else if (calcTop() === false && calcBottom() === false && flag !== 'false') 
    {
      jQuery('#aside-sidebar').css({'position': 'absolute', "top": ""});
    } 
  else 
    {
      flag = 'false';
      jQuery('#aside-sidebar').css({'position': 'absolute', "top": sidebarPosition()+"px"});
      console.log(flag);
    }
});

The issue I have is that the flag variable is not changing according to the last part of the if statement. 我的问题是,根据if语句的最后一部分, flag变量没有更改。 Any hint? 有什么提示吗?

in the else part if you are setting flag to 'false' it will definitely be set as u expected. 在else部分中,如果将标志设置为“ false”,则肯定会按照您的期望进行设置。

In your condition part i think you can add some refactoring as in if and else if part you are having two similar conditions . 在您的条件部分中,我认为您可以添加一些重构,就像如果您有两个相似的条件一样。

and why are you setting flag to a string variable instead you can easily use boolean flag here. 以及为什么将flag设置为字符串变量,而您可以在此处轻松使用boolean flag。

last but not the least you can watch the console for any error. 最后但并非最不重要的一点是,您可以观看控制台中的任何错误。 if any error occurs that means it is not coming to the setter part flag = 'false'; 如果发生任何错误,则意味着没有传到设置器标志='false';

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

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