Skip to main content
added 45 characters in body
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37

Can't really tell what your code does in real life, thus the following optimization/organization might not work. However, following generic tips, here's what I got:

//we can reduce DOM fetching by fetching html first
//caching it as well as other frequently used elements
//assuming that they never change in the course of your page's life
var html = $('html')
  , footer = $('footer')
  , foo = $('.foo')
  , bar = $('.bar')
  , options = {
      queue: false,
      duration: 1500,
      complete: function () {
        if (!foo.hasClass('active')) {
          foo.toggleClass('active');
          bar.slideToggle();
        }
      }
    }
  ;

//then here, we fetch body only when it's not IE9+ thus reducing operations
html = html.hasClass('lt-ie9') ? html : $('body');

$('.var').on('click', function (e) {
  
  //i think the properties can't be cached since it may need current values
  //however, your options can be cached since they are static values
  html.animate({
    scrollTop: footer.offset().top
  }, options);
  
  //foran jQueryanswer handlers,as it'sto betterwhen to use return false thanvs stopPropagationpreventDefault
  //you can use either, depending on the situation
  //http://stackoverflow.com/a/1357151/575527
  return false;
});

Can't really tell what your code does in real life, thus the following optimization/organization might not work. However, following generic tips, here's what I got:

//we can reduce DOM fetching by fetching html first
//caching it as well as other frequently used elements
//assuming that they never change in the course of your page's life
var html = $('html')
  , footer = $('footer')
  , foo = $('.foo')
  , bar = $('.bar')
  , options = {
      queue: false,
      duration: 1500,
      complete: function () {
        if (!foo.hasClass('active')) {
          foo.toggleClass('active');
          bar.slideToggle();
        }
      }
    }
  ;

//then here, we fetch body only when it's not IE9+ thus reducing operations
html = html.hasClass('lt-ie9') ? html : $('body');

$('.var').on('click', function (e) {
  
  //i think the properties can't be cached since it may need current values
  //however, your options can be cached since they are static values
  html.animate({
    scrollTop: footer.offset().top
  }, options);
  
  //for jQuery handlers, it's better to return false than stopPropagation
  //http://stackoverflow.com/a/1357151/575527
  return false;
});

Can't really tell what your code does in real life, thus the following optimization/organization might not work. However, following generic tips, here's what I got:

//we can reduce DOM fetching by fetching html first
//caching it as well as other frequently used elements
//assuming that they never change in the course of your page's life
var html = $('html')
  , footer = $('footer')
  , foo = $('.foo')
  , bar = $('.bar')
  , options = {
      queue: false,
      duration: 1500,
      complete: function () {
        if (!foo.hasClass('active')) {
          foo.toggleClass('active');
          bar.slideToggle();
        }
      }
    }
  ;

//then here, we fetch body only when it's not IE9+ thus reducing operations
html = html.hasClass('lt-ie9') ? html : $('body');

$('.var').on('click', function (e) {
  
  //i think the properties can't be cached since it may need current values
  //however, your options can be cached since they are static values
  html.animate({
    scrollTop: footer.offset().top
  }, options);
  
  //an answer as to when to use return false vs preventDefault
  //you can use either, depending on the situation
  //http://stackoverflow.com/a/1357151/575527
  return false;
});
Source Link
Joseph
  • 25.4k
  • 2
  • 26
  • 37

Can't really tell what your code does in real life, thus the following optimization/organization might not work. However, following generic tips, here's what I got:

//we can reduce DOM fetching by fetching html first
//caching it as well as other frequently used elements
//assuming that they never change in the course of your page's life
var html = $('html')
  , footer = $('footer')
  , foo = $('.foo')
  , bar = $('.bar')
  , options = {
      queue: false,
      duration: 1500,
      complete: function () {
        if (!foo.hasClass('active')) {
          foo.toggleClass('active');
          bar.slideToggle();
        }
      }
    }
  ;

//then here, we fetch body only when it's not IE9+ thus reducing operations
html = html.hasClass('lt-ie9') ? html : $('body');

$('.var').on('click', function (e) {
  
  //i think the properties can't be cached since it may need current values
  //however, your options can be cached since they are static values
  html.animate({
    scrollTop: footer.offset().top
  }, options);
  
  //for jQuery handlers, it's better to return false than stopPropagation
  //http://stackoverflow.com/a/1357151/575527
  return false;
});