Tip: Click lines to highlight, hold ctrl/cmd to multi-select

http://codedumper.com/ifeva (17-Feb @ 17:31)

Syntax Highlighted Code

  1. // comment scroll
  2. var $form = $('#new_comment');
  3. if ( $form.length ) {
  4.     var offset = $form.offset(),
  5.         offsetTop = offset.top, offsetLeft = offset.left,
  6.         fixedPos = false, timeout;
  7.     $(window).bind('resize', function(event) {
  8.         if ( timeout ) clearTimeout(timeout);
  9.         timeout = setTimeout(resize, 50);
  10.         function resize() {
  11.             $form.css({
  12.                 position: 'relative',
  13.                 top: 0,
  14.                 left: 0,
  15.                 width: ''
  16.             });
  17.             offsetLeft = $form.offset().left;
  18.             fixedPos = false;
  19.             $(window).scroll();
  20.         }
  21.     });
  22.     $(window).bind('scroll', function(event) {
  23.         var scrollTop = $(this).scrollTop(),
  24.             diff = scrollTop - offsetTop;
  25.         if ( diff > -20 && !fixedPos ) {
  26.             $form.css({
  27.                 position: 'fixed',
  28.                 top: 20,
  29.                 width: $form.width(),
  30.                 left: offsetLeft
  31.             });
  32.             fixedPos = true;
  33.         } else if ( diff < -20 && fixedPos ) {
  34.             $form.css({
  35.                 position: 'relative',
  36.                 top: 0,
  37.                 left: 0,
  38.                 width: ''
  39.             });
  40.             fixedPos = false;
  41.         }
  42.     }).scroll();
  43. }
  44.  

Plain Code

// comment scroll
var $form = $('#new_comment');
if ( $form.length ) {
    var offset = $form.offset(),
        offsetTop = offset.top, offsetLeft = offset.left,
        fixedPos = false, timeout;
    $(window).bind('resize', function(event) {
        if ( timeout ) clearTimeout(timeout);
        timeout = setTimeout(resize, 50);
        function resize() {
            $form.css({
                position: 'relative',
                top: 0,
                left: 0,
                width: ''
            });
            offsetLeft = $form.offset().left;
            fixedPos = false;
            $(window).scroll();
        }
    });
    $(window).bind('scroll', function(event) {
        var scrollTop = $(this).scrollTop(),
            diff = scrollTop - offsetTop;
        if ( diff > -20 && !fixedPos ) {
            $form.css({
                position: 'fixed',
                top: 20,
                width: $form.width(),
                left: offsetLeft
            });
            fixedPos = true;
        } else if ( diff < -20 && fixedPos ) {
            $form.css({
                position: 'relative',
                top: 0,
                left: 0,
                width: ''
            });
            fixedPos = false;
        }
    }).scroll();
}

Codedump Run

Permalink: http://codedumper.com/ifeva