简体   繁体   中英

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. Here's a part of my jQuery code:

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. Any hint?

in the else part if you are setting flag to 'false' it will definitely be set as u expected.

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.

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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