簡體   English   中英

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

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

我有一個div #HangerLeft,它會根據主體寬度通過jQuery自動生成css.right,使其位於頁面的左側。 它是絕對定位的。

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

在#HangerLeft div內,我有一個#scrollWrapper div,沒有位置,在#scrollWrapper內,我有#scrollBox。 #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;}

#scrollBox一直坐着直到您滾動。 一旦滾動到#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>

在Firefox和IE中,這可以正常工作。

在Safari和Chrome中,一旦#scrollBox javascript命中,#scrollBox div就會從#HangerLeft div跳出到頁面中間,而忽略#HangerLeft div的位置。

我已經為此奮斗了2周,茫然不知所措。

任何幫助,將不勝感激。

好的,所以我重新編寫了您的代碼。 我按照您的喜好保留它。.我將以其他方式進行設置,但這適用於您的方法。 您可以在此處查看實時版本的 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM