当页面滚动时,如何在div上快速移动1 div

Rah*_*hah 0 html javascript css jquery scroll

我看到这个效果 在这里.当页面滚动时,页面的主要内容部分在div上方移动.

我尝试使用视差效果重新创建这个效果,但是徒劳无功.问题在于使用视差,我只能在同一个div内改变2个物体的速度.除此之外,我还要把一些不必要的标签放在一边使脚本工作的页面.

是否有更简单(或工作)的方法来实现这种效果?非常感谢

nic*_*ass 6

你可以用CSS做到这一点.

#head, #subHead{
    position: fixed;           
    height: 80px;    
    width: 100%;
    display: block;
    left: 0;
    top: 0;
    z-index: 10;         /* on top of all */
}

#subHead{
    z-index: 4;          /* below all */
    top: 80px;           /* height of the top div */
}

#content{
    position: relative;
    z-index: 6;          /* below the top div, but above the one below it */
    margin-top: 160px;   /* the height of the two divs above combined */
}
Run Code Online (Sandbox Code Playgroud)

并使subHead滚动更慢:

window.onscroll = function(ev){
  var subHead = document.getElementById('subHead'),
      topHeight = document.getElementById('head').offsetHeight;

  subHead.style.top = (topHeight - document.body.scrollTop / 4) + 'px';
};    
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$(window).on('scroll', function(){  
  $('#subHead').css('top', ($('#head').height() - $('body').scrollTop() / 4));
}); 
Run Code Online (Sandbox Code Playgroud)