简体   繁体   English

在Safari和Chrome中跳转div。 jQuery / JavaScript

[英]Jumping div in Safari and Chrome. jQuery/Javascript

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. 我有一个div #HangerLeft,它会根据主体宽度通过jQuery自动生成css.right,使其位于页面的左侧。 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. 在#HangerLeft div内,我有一个#scrollWrapper div,没有位置,在#scrollWrapper内,我有#scrollBox。 The #scrollBox is absolute positioned. #scrollBox是绝对定位的。

#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. #scrollBox一直坐着直到您滚动。 Once you scroll past the top of the #scrollBox div javascript adds a class to make the #scrollBox position:fixed instead of absolute. 一旦滚动到#scrollBox div的顶部,JavaScript就会添加一个类以使#scrollBox position:fixed而不是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. 在Firefox和IE中,这可以正常工作。

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. 在Safari和Chrome中,一旦#scrollBox javascript命中,#scrollBox div就会从#HangerLeft div跳出到页面中间,而忽略#HangerLeft div的位置。

I have been battling this for 2 weeks and am at a loss. 我已经为此奋斗了2周,茫然不知所措。

Any help would be appreciated. 任何帮助,将不胜感激。

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: 您可以在此处查看实时版本的 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: CSS:

#HangerLeft {
    top: 100px;
}

#scrollWrapper {
  width: 130px;
}

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

HTML: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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