简体   繁体   English

在滚动条上缩小标题并将内容同步移至下方

[英]Shrink header on scroll and move content below synchronously

I am currently creating a personal website and I want a full height header when the website is loaded. 我当前正在创建一个个人网站,并且在加载网站时需要一个全高的标题。 This header should animate its height when the user scrolls down and the content below the header should move synchronously to the animation of the header. 当用户向下滚动时,此标头应设置其高度的动画,并且标头下方的内容应与标头的动画同步移动。 And the header should be fixed positioned at the end. 接头应固定在末端。 I got everything working, beside the fixed position. 除了固定位置之外,我还可以进行所有工作。 It is flickering when the animation is finished. 动画结束时,它正在闪烁。

I created a fiddle, so you will understand what I mean more easy. 我创建了一个小提琴,因此您将更容易理解我的意思。

http://jsfiddle.net/30j5njrw/10/ ` http://jsfiddle.net/30j5njrw/10/ `

Thanks in advance for any help 预先感谢您的任何帮助

Regards 问候

Edit: I tried several things so far, like adding position fixed from the beginning to the header, or trying to position the header content via jQuery on scroll...but I never got the result I wish. 编辑:到目前为止,我尝试了几件事,例如,从头开始将固定位置添加到标题中,或者尝试通过jQuery在滚动条上放置标题内容...但是我从未得到想要的结果。

Edit2: If someone knows a site with a similar effect, please share it. Edit2:如果有人知道某个网站具有类似的效果,请分享。

Edit3: Since no one got my problem so far, i will try to describe it more detailed. Edit3:到目前为止,没有人遇到我的问题,因此我将尝试更详细地描述它。

I have a header and a content area. 我有一个标题和一个内容区域。 The header is set to full height of the viewport when the page loads. 页面加载时,页眉设置为视口的全高。 The header uses static css positioning at this point, so the content area is correctly positioned below the header outside of the viewport. 标头此时使用静态css定位,因此内容区域正确地位于视口外部标头下方。

If I scroll down, the header shrinks and some css of the header content is animated. 如果我向下滚动,标题会缩小,并且标题内容的某些CSS会变为动画。 The content area now must move up with the arrow under the header and the header should be fixed when the animation is finished. 现在,内容区域必须向上移动,且标题下方为箭头,并且动画结束时,标题应固定。

All your examples so far just don't care about the flow of the content below the header. 到目前为止,您所有的示例都不关心标题下的内容流。

Edit4: 编辑4:

haxxxton answered the question and the solution looks even better than I actually wanted it. haxxxton回答了问题,解决方案看起来比我实际想要的还要好。 So I will reward him with the bounty when the 24 hours are over. 因此,当24小时结束后,我将奖励他。

Edit5 编辑5

Actually haxxxtons answer didn't fix my problem. 其实haxxxtons的答案不能解决我的问题。 I was so amazed that I have overseen some problems. 我感到非常惊讶,以至于我监督了一些问题。 The arrow must be part of the content section, since there will be more content sections below on the final site. 箭头必须是内容部分的一部分,因为最终站点上下面将有更多内容部分。 I came up with another solution, but I got stuck either. 我想出了另一种解决方案,但是我还是被卡住了。 Now I don't think that this is even possible to do. 现在,我什至认为这是不可能的。 Here is an updated version of my fiddle: 这是我的小提琴的更新版本:

http://jsfiddle.net/30j5njrw/14/ http://jsfiddle.net/30j5njrw/14/

The arrow is still not moving properly since I can't do preventDefault() on the scroll event. 箭头仍然无法正确移动,因为我无法在scroll事件上执行preventDefault()。 I need to do preventDefault() during the scroll event when the height of the header is not 165px. 当标题的高度不是165px时,在滚动事件期间需要执行preventDefault()。 According to different sources found on google, it is not possible to do preventDefault() on the scroll event. 根据在google上找到的不同来源,不可能在滚动事件上执行preventDefault()。 I might try now to play with padding of content area during the scroll event to make it look like it's not scrolling. 我现在可以尝试在滚动事件期间播放内容区域的填充,以使其看起来好像没有滚动。 But I don't think that the effect i want is actually possible. 但是我认为我想要的效果实际上是不可能的。

Edit6: 编辑6:

It works to set the padding of the content area to the pixels of the scroll offset. 它可以将内容区域的填充设置为滚动偏移量的像素。 See new fiddle: 看到新的小提琴:

Fiddle update 小提琴更新

But there is still a problem. 但是仍然存在问题。 When you scroll really fast it is flickering somehow and positioning of the footer is strange. 当您快速滚动时,它会以某种方式闪烁,并且页脚的位置很奇怪。 But I don't know if you can scroll that fast with any other input device than the magic touchpads of MacBooks so I might ignore this problem, if I don't finde a solution in a few hours. 但是我不知道您是否可以使用除MacBooks神奇的触摸板以外的任何其他输入设备快速滚动,因此,如果我在几个小时内找不到解决方案,我可能会忽略此问题。

This is because when the user scrolls down at the first instance (to trigger your animation) you are letting them continue scrolling, and then applying the position:fixed; 这是因为当用户在第一个实例处向下滚动(触发动画)时,是在让他们继续滚动,然后应用position:fixed; to your header.. which is making it 'jump' down the page. 到您的标题..这使其在页面上“跳转”。

The solution, is actually something you have tried already; 解决方案实际上是您已经尝试过的方法; to start with the header already position:fixed , and then animate from there. 从头已经position:fixed ,然后从那里开始动画。

By applying fixed to the header to begin with, the other elements wont appear to 'snap' later. 通过将fixed应用于标头,其他元素以后似乎不会“捕捉”。

I found that adding the 'arrow' element into the header stops the noticeable snapping (and i assume you intended this to be there all the time). 我发现在标头中添加'arrow'元素会停止明显的捕捉(我假设您一直以来都希望这样做)。

JSFIDDLE DEMO JSFIDDLE演示


CSS 的CSS

body {
    text-align: center;
}
.clear {
    clear: both;
}
h1 {
    margin-top: 4px;
}
h1 p {
    font-size: 18px;
    margin: 5px 0 10px 0;
}
h2 {
    margin-bottom: 50px;
}
h1, h2, h3, h4 {
    text-align: center;
}
.white, .white * {
    background-color: #ffffff;
    border-color: #2d2d2d;
    color: #2d2d2d;
}
.dark, .dark * {
    background-color: #2d2d2d;
    border-color: #ffffff;
    color: #ffffff;
}
#top {
    top: 0;
    text-align: center;
    width: 100%;
    position:fixed;
    overflow: visible !important;
    z-index: 1000;
}
#top .arrow{
    border-left: 30px solid transparent;
    border-right: 30px solid transparent;
    border-top-width: 20px;
    border-top-style: solid;
    border-top-color:white;
    height: 20px;
    margin: 0 0 0 -30px;
    width: 60px;
    position:absolute;
    top:100%;
    left:50%;
    background-color:transparent;
}
#top p.slogan {
    font-family:'Poiret One', cursive;
    margin-top: 50px;
    font-size: 20px;
}
#top a {
    display: block;
    font-family:'Poiret One', cursive;
    font-size: 20px;
    text-align: center;
    width: 117px;
}
#top a:hover {
    color: #767676;
    text-decoration: none;
}
#top nav {
    float: right;
    left: -50%;
    margin-top: 35px;
    position: relative;
    text-align: left;
}
#top ul {
    left: 50%;
    margin: 0 auto;
    padding: 0;
    position: relative;
}
#top li {
    float: left;
    margin: 0 5px;
    position: relative;
    width: 100px;
}
#contact {
    padding-top:215px;
    height: 1300px;
}

JS JS

$( document ).ready(function() {
    changeHeaderHeight();
    var lastScrollTop = 0;
    var isAnimationFinished = true;
    var isScrolled = $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();
        if (isAnimationFinished == true) {
            isAnimationFinished = false;
            topHeight = $('#top').height() - scrollTop;
            $('#top h1').animate({
                fontSize: '14px'
            },500);
            $('#top h1 p').fadeOut();
            $('#top p.slogan').fadeOut();
            $('#top').animate({
                height: 165 + 'px'
            },1000);
            $('#main-navigation').animate({
                marginTop: 5 + 'px',
            },1000);
            $('#main-navigation a').animate({
                width: '97px',
                fontSize: '16px',
            },1000);
            $('#main-navigation li').animate({
                width: '97px'
            },1000);
        }
        lastScrollTop = scrollTop;
    });
    $.when(isScrolled).then(function() {
        if ($('#top').height() == 165) {
            isAnimationFinished = true;

        }
    });



});
$(window).resize(function() {
    changeHeaderHeight();
});
function changeHeaderHeight() {
    var viewportHeight = $(window).height();
    $('header#top').height(viewportHeight);
}

HTML 的HTML

<body data-spy="scroll" data-target="#main-navigation">
  <header id="top" class="white">
    <h1>
      Title
      <p>
        ...slogan goes here
      </p>
    </h1>
    <nav id="main-navigation">
      <ul data-spy="affix" data-offset-top="155" class="nav">
        <li>
          <a href="#news" title="News">
            News
          </a>
        </li>
        <li>
          <a href="#agency" title="Agentur">
            Agentur
          </a>
        </li>
        <li>
          <a href="#services" title="Leistungen">
            Leistungen
          </a>
        </li>
        <li>
          <a href="#portfolio" title="News">
            Portfolio
          </a>
        </li>
        <li>
          <a href="#contact" title="News">
            Kontakt
          </a>
        </li>
      </ul>
    </nav>
    <div class="clear"></div>
    <p class="slogan">
      Blabla
    </p>
    <p class="slogan">
      More Blabla
    </p>
    <div class="arrow">
    </div>
  </header>
  <section id="contact" class="dark">
    <header>
      <h2>
        Contact
      </h2>
    </header>
  </section>
  <footer class="white">
    <a class="scroll-top" href="#top">
      Logo
    </a>
  </footer>
</body>

Here is another stack overflow question that asks something similar: jQuery - Sticky header that shrinks when scrolling down 这是另一个堆栈溢出问题,要求类似的内容: jQuery-向下滚动时会缩小的粘性标头

The accepted answer has this code: 接受的答案具有以下代码:

$(function(){
  $('#header_nav').data('size','big');
});

$(window).scroll(function(){
  if($(document).scrollTop() > 0)
{
    if($('#header_nav').data('size') == 'big')
    {
        $('#header_nav').data('size','small');
        $('#header_nav').stop().animate({
            height:'40px'
        },600);
    }
}
else
  {
    if($('#header_nav').data('size') == 'small')
      {
        $('#header_nav').data('size','big');
        $('#header_nav').stop().animate({
            height:'100px'
        },600);
      }  
  }
});

With this JS Fiddle example. 通过这个JS Fiddle示例。 http://jsfiddle.net/jezzipin/JJ8Jc/ http://jsfiddle.net/jezzipin/JJ8Jc/

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

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