简体   繁体   English

滚动页面时如何更快地移动1格/在格上移动

[英]How to move 1 div faster/over a div when the page is scrolled

I saw this effect here . 我在这里看到了这种效果。 The main content section of the page moves over and above a div when the page is scrolled. 滚动页面时,页面的主要内容部分将在div上方和上方移动。

I tried recreating this effect using the parallax effect,but in vain.The issue is that using parallax,i can change the speed of 2 objects within the same div only.Also apart from that,i will have to put some unncessary tags all over the page to make the script work. 我尝试使用视差效果来重新创建此效果,但是徒劳无功。问题是使用视差,我只能更改同一div内2个对象的速度。除此之外,我还必须在所有位置放置一些不必要的标签使脚本工作的页面。

Is there a simpler(or working) way to achieve this effect?Thanks a lot 有没有更简单(或可行)的方法来实现此效果?

You can do this with CSS . 您可以使用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 */
}

And to make subHead scroll slower: 并使subHead滚动变慢:

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

  subHead.style.top = (topHeight - document.body.scrollTop / 4) + 'px';
};    

jQuery: jQuery的:

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

There definitely seems to be some parallax going on at least. 至少似乎肯定存在一些视差。 I haven't looked through the markup thoroughly enough, but one thing of note is that the area with the graphic and the content area (along with the area for the ads) are sibling <div> s. 我对标记的了解还不够充分,但是要注意的一件事是,带有图形的区域和内容区域(以及广告区域)都是同级<div>

Just top of head, with some CSS position ing and z-index ing, it'd be a bit straightforward to render the content <div> above the graphic <div> . 只是顶部,带有一些CSS positionz-index ,将内容<div>呈现在图形<div>上方会有点简单。 It seems that only the graphic <div> is being controlled via parallax scrolling. 似乎只有图形<div>是通过视差滚动控制的。 If that makes sense. 如果这样的话。

You could attach onscroll event for your first div and set scroll position of second div in your JS. 您可以为第一个div附加onscroll事件,并在JS中设置第二个div的滚动位置。

<div id="div1" onscroll="OnScrollDiv()">
    <div id="div2"></div>
</div>

javascript: javascript:

function OnScrollDiv () {
    var div1 = document.getElementById("div1");
    var div2 = document.getElementById("div2");
    div2.scrollTop = div1.scrollTop * 2;
}

More info in here: http://help.dottoro.com/ljurkcpe.php http://help.dottoro.com/ljnvjiow.php 此处提供更多信息: http : //help.dottoro.com/ljurkcpe.php http://help.dottoro.com/ljnvjiow.php

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

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