views:

193

answers:

1

I have a div #HangerLeft that the css.right is automatically generated via jQuery to sit on the left side of the page based on the body width. It is absolute positioned.

function hangerLeft() {
  var hangerPosition = (jQuery("body").innerWidth() / 2) + (990 / 2);
  jQuery("#HangerLeft").css("position","absolute").css("right", hangerPosition +"px").css("top","20px");
}

Inside the #HangerLeft div I have a #scrollWrapper div with no positioning and inside the #scrollWrapper i have a #scrollBox. The #scrollBox is absolute positioned.

#scrollWrapper { width:130px; height:400px; border:1px solid #fff;}

#scrollBox { position: absolute; top: 100; margin-top: 25px; padding-top: 0px;}

#scrollBox.fixed { position: fixed; top: 0;}

The #scrollBox sits until you scroll. Once you scroll past the top of the #scrollBox div javascript adds a class to make the #scrollBox position:fixed instead of absolute.

<script>
$(function () {

var msie6 = $.browser == 'msie' && $.browser.version < 7;

if (!msie6) {
var top = $('#scrollBox').offset().top - parseFloat($('#scrollBox').css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
  // what the y position of the scroll is
  var y = $(this).scrollTop();

  // whether that's below the form
  if (y >= top) {
    // if so, ad the fixed class
    $('#scrollBox').addClass('fixed');
  } else {
    // otherwise remove it
    $('#scrollBox').removeClass('fixed');
  }
 });
}  
});
</script>

In Firefox and IE this works fine.

In Safari and Chrome once the #scrollBox javascript hits, the #scrollBox div jumps out of the #HangerLeft div into the middle of the page and ignores the positioning of the #HangerLeft div.

I have been battling this for 2 weeks and am at a loss.

Any help would be appreciated.

A: 

Ok, so I reworked your code. I kept it to your liking.. I would set this up in a different way but this works for your approach. You can see a live version here JavaScript:

<script type="text/javascript">

function setupScrollBox(){
  // cache box element and use wrapper as your position element
  var hanger = $("#HangerLeft"),
      position = $("#wrap").offset();

  hanger.css({
    position: 'absolute',
    left: position.left - $("#scrollWrapper").outerWidth(),
    marginTop: '25px'
  });

}

$(document).ready(function(){
  // check if IE6
  var msie6 = $.browser.msie && $.browser.version < 7;

  setupScrollBox();

  // attach resize event to window 
  $(window).resize(function(){
    setupScrollBox();
  });

  // check browser
  if(!msie6){
    // attach scroll event     
    $(window).scroll(function (event) {
      // get scroll position and cache element so we only access it once
      var y = $(this).scrollTop(),
          wrap = $('#HangerLeft');

      // if scroll position is greater than 100 adjust height else do nothing
      if(y > 100)
        // you can animate the position or not, your call
        wrap.stop().animate({top: y}, 250);
        //wrap.css('top', y+'px');
    });
  }  
});
</script>

CSS:

#HangerLeft {
    top: 100px;
}

#scrollWrapper {
  width: 130px;
}

#scrollBox {
  position: relative;
  margin-top: 25px;
  padding-top: 0px;
  z-index: 10;
}

HTML:

<div id="HangerLeft">
  <div id="scrollWrapper">
    <div id="scrollBox">
      <div id="mainContainer">
        <div id="shareContainer">
          <div class="moduleShareHeader">SCROLL BOX</div>
        </div>
      </div>
    </div>
  </div>
</div>
Kieran
Makes sense. Unit-less pixel values aren't technically valid CSS and WebKit does lack a lot of Firefox's hard-earned "Support crappy, broken IE CSS" hacks.
ssokolow
I added the px to both top values in #scrollBox and #scrollBox.fixed,For some reason I am still getting a jump. Do ou have your example? My example is located here: http://jobingarena.com/figurecrapout2.html
Michael Sharer
I tried to simplify your existing code. Hopefully it helps.
Kieran