Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 10th, 2012  |  syntax: None  |  size: 2.68 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jQuery - piece of code causing conflict problems on pages the selector doesn't exist
  2. jQuery('#commentWrapper').stickyfloat({ duration: 300, easing : 'easeInQuad' });
  3.        
  4. $.fn.stickyfloat = function(options, lockBottom) {
  5.             var $obj                = this;
  6.             var parentPaddingTop    = parseInt($obj.parent().css('padding-top'));
  7.             var startOffset         = $obj.parent().offset().top;
  8.             var opts                = $.extend({ startOffset: startOffset, offsetY: parentPaddingTop, duration: 200, lockBottom:true }, options);
  9.  
  10.             $obj.css({ position: 'absolute' });
  11.  
  12.             if(opts.lockBottom){
  13.                 var bottomPos = $obj.parent().height() - $obj.height() + parentPaddingTop; //get the maximum scrollTop value
  14.                 if( bottomPos < 0 )
  15.                     bottomPos = 0;
  16.             }
  17.  
  18.             $(window).scroll(function () {
  19.                 $obj.stop(); // stop all calculations on scroll event
  20.  
  21.                 var pastStartOffset         = $(document).scrollTop() > opts.startOffset;   // check if the window was scrolled down more than the start offset declared.
  22.                 var objFartherThanTopPos    = $obj.offset().top > startOffset;  // check if the object is at it's top position (starting point)
  23.                 var objBiggerThanWindow     = $obj.outerHeight() < $(window).height();  // if the window size is smaller than the Obj size, then do not animate.
  24.  
  25.                 // if window scrolled down more than startOffset OR obj position is greater than
  26.                 // the top position possible (+ offsetY) AND window size must be bigger than Obj size
  27.                 if( (pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow ){
  28.                     var newpos = ($(document).scrollTop() -startOffset + opts.offsetY );
  29.                     if ( newpos > bottomPos )
  30.                         newpos = bottomPos;
  31.                     if ( $(document).scrollTop() < opts.startOffset ) // if window scrolled < starting offset, then reset Obj position (opts.offsetY);
  32.                         newpos = parentPaddingTop;
  33.                     $obj.animate({ top: newpos }, opts.duration );
  34.                 }
  35.             });
  36.         };
  37.        
  38. var $obj = this;
  39. // ...
  40. var startOffset = $obj.parent().offset().top;
  41.        
  42. $.fn.stickyfloat = function(options, lockBottom) {
  43.     return this.each(function() {
  44.         var $obj = $(this);
  45.         // The rest of your code.
  46.     }
  47. };
  48.        
  49. var $obj = $(this);
  50. var $parent = $obj.parent();
  51. if (!$parent.length) {
  52.     return;  // No parent, continue with next element, if any.
  53. }
  54. // Parent element is safe to use.
  55. var parentPaddingTop = parseInt($parent.css('padding-top'));
  56. var startOffset = $parent.offset().top;