2015-02-04 4 views
0

저는 수백 개의 다른 웹 사이트처럼 움직이는 끈적한 헤더를 만들려고했습니다. 차이점은 특정 스크롤 포인트를 지나가고 새로운 크기로 스냅 한 후에 애니메이션을 스크롤하는 것이 좋습니다.스크롤과 함께 변경되는 애니메이션 끈적한 헤더

내가 할 노력하고있어의 예는이 사이트에 : 나는 구글 무엇을 모르고 나를이 아니 경우 희망 있도록 http://www.kriesi.at/themes/enfold/ 어떤 도움을 주시면 감사

, 나는 자바 스크립트에 아주 새로운 해요.

편집 : 여기에 지금까지 (내 질문의 고정 주요 부분)가 무엇

$(function() { 
    $(window).scroll(function() { 
     var scroll = getCurrentScroll(); 
     var progress = ((scroll * 145)/100); 
     var prog = document.getElementById("prog"); 
     prog.innerHTML = scroll + "px " + progress + "%"; 

     header = document.getElementById('header'); 

     if (progress < 5) { 
      header.style.height = 145 + "px"; 
     } 
     if (progress > 5 || progress < 100) { 
      header.style.height = (145 - progress + (scroll/2)) + "px"; 
     } 
     if (progress >= 100) { 
      header.style.height = 75 + "px"; 
      header.style.opacity = 0.8; 
     } 
    }); 

    function getCurrentScroll() { 
     return window.pageYOffset || document.documentElement.scrollTop; 
    } 
}); 

문에 해당하는 경우 중간, 사용자는 최소 (0) 및 최대 스크롤 포인트 사이가 될 것입니다 때 헤더 (150) 용 트랙 패드가 페이지 상단을 통과하면서 위로 스크롤 할 때 첫 번째 If 문이 작동하지 않는 이유는 무엇입니까?

+0

안녕, 이것은 당신이 무엇을 시도 ... 매우 광범위한 질문이다? 무슨 지점에 갇혔어요? 다시 일하게하기 위해 당신은 무엇을 연구 했습니까? 가짜 코드가있어? 당신이 kreisi에서 사람들에게 물어 보려 했습니까? 그들이 한 일 (또는 자신의 CSS/js를 파고들)은 무엇입니까? –

+0

이봐, 나는 꽤 많은 일을 해왔다. 지금은 머리글의 높이의 백분율을 기준으로 머리글의 높이를 조정하는 js 함수를 아래로 스크롤하여 만들려고합니다. 나는 꽤 늦게 수학으로 조금 어려움을 겪고있다. – ChrisPBacon

+0

초기 효과를 느리게 느리게 스크롤해야했다. 저는 개인적으로 머리말을 자르는 파동에 대해 거대한 팬이 아닙니다. 그게 도움이 되니?롤); – Timmerz

답변

1

두 개의 다른 클래스를 만듭니다. 1 일반 헤더에 대한 클래스 및 고정 헤더에 대한 클래스. jQuery for를 사용하면 윈도우의 스크롤 위치가 감지됩니다. 보통 사람들은 css3 전환을 사용하여 애니메이션을 만들지 만 빠른 변경을 위해 그냥 두십시오.

HTML

<header>Sticky Header</header> 

CSS

header { 
    position: fixed; 
    width: 100%; 
    text-align: center; 
    font-size: 72px; 
    line-height: 108px; 
    height: 108px; 
    background: #335C7D; 
    color: #fff; 
    font-family:'PT Sans', sans-serif; 
} 
header.sticky { 
    font-size: 24px; 
    line-height: 12px; 
    height: 48px; 
    background: #efc47D; 
    text-align: left; 
    padding-left: 20px; 
} 

당신은,393에서이 주제에 대한 튜토리얼을 통해 읽을 수 있습니다

$(window).scroll(function() { 
    if ($(this).scrollTop() > 1) { 
     $('header').addClass("sticky"); 
    } else { 
     $('header').removeClass("sticky"); 
    } 
}); 

http://jsfiddle.net/3Lj0oyL4/

JQUERY

+0

시간을내어 답변 해 주셔서 고마워요. 링크 에서처럼 머리글을 만들고 싶습니다. 스크롤을 중간에서 멈추는 경우 크기 변경은 작업을 다시 시작할 때 중지됩니다. – ChrisPBacon

+0

사용자가 스크롤을 멈추었을 때 머리글을 원래 크기로 다시 시작하라는 메시지가 표시되면 머리글이 다시 작아지면서 스크롤이 다시 시작됩니다. – AndrewH

+0

아니요, 원래 질문의 링크가 무슨 뜻인지 보여줍니다. 그래도보기에는 아주 천천히 스크롤해야합니다. 클래스를 가리키고 스냅하는 표준 스크롤보다 더 많습니다 – ChrisPBacon

0

먼저 머리글을 고정 및 100 %로 설정하고 가능하면 Z- 색인을 추가하십시오.

그런 다음 당신은 페이지가 스크롤되면 그 다음 우리는 숏 다리 전화를 추가 할 수 있습니다 다음과 같은 CSS

.header{ 
    position:fixed; 
    width:100%; 
    height:100px; 
    z-index:9999; //only if you need the header on top of everything 
} 
.header.shorty{ 
    height:50px; 
} 

가 필요합니다.

$(window).scroll(function(){ 
if($(window).scrollTop() > 100){ 
    $(".header").addClass("shorty"); 
}else{ 
    $(".header").removeClass("shorty"); 
} 
}); 
+0

시간을내어 답변 해 주셔서 감사합니다. 링크에서 머리글을 만들고 싶습니다. 스크롤을 중간에 멈추는 경우 크기 변경이 필요합니다. 네가 할 때 다시 시작하기 전에 멈출거야. – ChrisPBacon

0

이것은 완벽하지는 않을 수도 있지만 그것이 내가하고 싶은대로하고 싶은 생각입니다. 이것이 틀린 경우는 실례합니다.

window.onload = function() { 
 
    $("#everything").scroll(function() { 
 
    var startAt = 40; //How many pixles scrolled to start effect, 0 would match link 
 
    if ($("#everything").scrollTop() >= startAt) { 
 
     var scroll = $("#everything").scrollTop(), 
 
     total = 0, // go to this value 
 
     distance = 40, //distance to shrink 
 
     value = (scroll < distance) ? total : total + (distance - (scroll - startAt)); 
 

 
     $("#head").css("height", value); //change #head to what ever you want to shrink 
 
    } else { 
 
     $("#head").css("background-color", value); 
 
    } 
 
    }); 
 
}
html, 
 
body { 
 
    overflow: hidden; 
 
    /* Disables regular scrolling */ 
 
    margin: 0; 
 
    padding: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
#everything { 
 
    overflow: scroll; 
 
    /* enables content to scroll */ 
 
    position: relative; 
 
    width: 100%; 
 
    height: 100%; 
 
    padding-top: 40px; 
 
} 
 
#head { 
 
    width: 100%; 
 
    height: 40px; 
 
    background-color: red; 
 
    position: fixed; 
 
    top: 0px; 
 
    overflow: hidden; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 

 
<body> 
 
    <div id="everything"> 
 
    <div id="head">header</div> 
 
    <span> 
 
Text Following text is so the page can scroll: <br/> 
 
     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer porta velit eu turpis imperdiet congue. Morbi nec mi ipsum. Nam eu nunc in lorem sagittis feugiat a quis nisl. Donec suscipit leo quis magna egestas, id convallis leo gravida. Curabitur finibus lectus ut metus feugiat, eu tincidunt eros tempor. Fusce facilisis nunc vulputate, posuere velit nec, ultrices diam. Vestibulum aliquam velit in lectus imperdiet, vitae condimentum lectus finibus. Aliquam ac arcu eget velit molestie rhoncus. Etiam rhoncus, tellus nec lacinia porta, diam lorem ultrices sem, et dignissim ipsum augue non augue. Suspendisse tincidunt felis sit amet orci accumsan, at ornare tellus viverra. Nam viverra nulla in urna elementum ornare.Sed interdum nisi libero, id porta turpis consectetur vitae. Curabitur nunc ex, interdum eget hendrerit maximus, faucibus non est. Etiam scelerisque condimentum eleifend. Integer ac ligula eget magna porta tristique at eu neque. Sed venenatis ipsum non metus sodales finibus. Suspendisse nec nunc lobortis ligula venenatis tristique. Suspendisse aliquam leo elit, et pretium ipsum tempor sed. Maecenas tincidunt dictum leo sit amet accumsan. Nullam eu viverra nulla. Aenean vehicula tellus a mauris malesuada interdum. Sed libero lacus, consectetur at condimentum vel, egestas vitae nisl.Mauris facilisis tincidunt magna, at gravida elit. Cras molestie eros sed tincidunt ultricies. Pellentesque eleifend egestas orci, sit amet condimentum nisl semper eleifend. Sed ipsum elit, aliquet nec lacinia a, maximus eu dolor. Quisque finibus efficitur odio gravida convallis. Vivamus nec velit in est ornare luctus at a risus. In hac habitasse platea dictumst. Proin condimentum eget est non posuere. Vivamus ante quam, bibendum in tincidunt ut, egestas sed mauris. Nunc non interdum nibh, nec ornare tellus. In interdum elit nisi, a interdum est tempor id. Cras a elit ut purus ornare mollis sit amet ut est. Cras ut ex sed neque lacinia accumsan quis aliquet turpis. Quisque nisl nunc, pretium sed lectus pretium, lacinia ornare magna. Curabitur sit amet felis turpis. Morbi nisi mi, mattis quis tempor ut, accumsan nec ex. 
 
     </span> 
 
    </div> 
 
</body> 
 

 
</html>

관련 문제