풀페이지 스크롤 예시
페이지 정보
본문
안녕하십니까?
유토하우스 유토맨입니다
CodePen Home Sticky Slider Nav v2 (Responsive)
https://codepen.io/ettrics/details/Wpbxgw
1. html
<header class="et-header">
<div class="et-header__left">
<a href="" class="et-header__logo">Ettrics</a>
</div>
</header>
<section class="et-hero-tabs">
<h1>STICKY SLIDER NAV V2</h1>
<h3>Sliding content with sticky tab nav</h3>
<div class="et-hero-tabs-container">
<a class="et-hero-tab" href="#tab-es6">ES6</a>
<a class="et-hero-tab" href="#tab-flexbox">Flexbox</a>
<a class="et-hero-tab" href="#tab-react">React</a>
<a class="et-hero-tab" href="#tab-angular">Angular</a>
<a class="et-hero-tab" href="#tab-other">Other</a>
<span class="et-hero-tab-slider"></span>
</div>
</section>
<main class="et-main">
<section class="et-slide" id="tab-es6">
<h1>ES6</h1>
<h3>something about es6</h3>
</section>
<section class="et-slide" id="tab-flexbox">
<h1>Flexbox</h1>
<h3>something about flexbox</h3>
</section>
<section class="et-slide" id="tab-react">
<h1>React</h1>
<h3>something about react</h3>
</section>
<section class="et-slide" id="tab-angular">
<h1>Angular</h1>
<h3>something about angular</h3>
</section>
<section class="et-slide" id="tab-other">
<h1>Other</h1>
<h3>something about other</h3>
</section>
</main>
2. css
//
// Body
//
body {
font-family: "Century Gothic", "Lato", sans-serif;
}
a {
text-decoration: none;
}
//
// Header
//
.et-header {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
position: fixed;
top: 0;
left: 0;
right: 0;
padding: 0 2em;
height: 75px;
z-index: 2;
transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
&--scrolled {
background: #fff;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
}
&--move-up {
transform: translateY(-75px);
transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
}
&__logo {
color: #000;
}
&__link {
margin-left: 1em;
color: #000;
}
}
//
// Hero
//
.et-hero-tabs,
.et-slide {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
position: relative;
background: #eee;
text-align: center;
padding: 0 2em;
h1 {
font-size: 2rem;
margin: 0;
letter-spacing: 1rem;
}
h3 {
font-size: 1rem;
letter-spacing: 0.3rem;
opacity: 0.6;
}
}
.et-hero-tabs-container {
display: flex;
flex-direction: row;
position: absolute;
bottom: 0;
width: 100%;
height: 75px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
background: #fff;
z-index: 1;
transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
&--top-first {
position: fixed;
top: 75px;
transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
}
&--top-second {
position: fixed;
top: 0;
}
}
.et-hero-tab {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
color: #000;
letter-spacing: 0.1rem;
transition: all 0.5s ease;
font-size: 0.8rem;
&:hover {
color:white;
background: rgba(102,177,241,0.8);
transition: all 0.5s ease;
}
}
.et-hero-tab-slider {
position: absolute;
bottom: 0;
width: 0;
height: 6px;
background: #66B1F1;
transition: left 0.3s ease;
}
@media (min-width: 800px) {
.et-hero-tabs,
.et-slide {
h1 {
font-size: 3rem;
}
h3 {
font-size: 1rem;
}
}
.et-hero-tab {
font-size: 1rem;
}
}
3. js
HTML SCSS JSResult Skip Results Iframe
class StickyNavigation {
constructor() {
this.currentId = null;
this.currentTab = null;
this.tabContainerHeight = 70;
this.lastScroll = 0;
let self = this;
$('.et-hero-tab').click(function() {
self.onTabClick(event, $(this));
});
$(window).scroll(() => { this.onScroll(); });
$(window).resize(() => { this.onResize(); });
}
onTabClick(event, element) {
event.preventDefault();
let scrollTop = $(element.attr('href')).offset().top - this.tabContainerHeight + 1;
$('html, body').animate({ scrollTop: scrollTop }, 600);
}
onScroll() {
this.checkHeaderPosition();
this.findCurrentTabSelector();
this.lastScroll = $(window).scrollTop();
}
onResize() {
if(this.currentId) {
this.setSliderCss();
}
}
checkHeaderPosition() {
const headerHeight = 75;
if($(window).scrollTop() > headerHeight) {
$('.et-header').addClass('et-header--scrolled');
} else {
$('.et-header').removeClass('et-header--scrolled');
}
let offset = ($('.et-hero-tabs').offset().top + $('.et-hero-tabs').height() - this.tabContainerHeight) - headerHeight;
if($(window).scrollTop() > this.lastScroll && $(window).scrollTop() > offset) {
$('.et-header').addClass('et-header--move-up');
$('.et-hero-tabs-container').removeClass('et-hero-tabs-container--top-first');
$('.et-hero-tabs-container').addClass('et-hero-tabs-container--top-second');
}
else if($(window).scrollTop() < this.lastScroll && $(window).scrollTop() > offset) {
$('.et-header').removeClass('et-header--move-up');
$('.et-hero-tabs-container').removeClass('et-hero-tabs-container--top-second');
$('.et-hero-tabs-container').addClass('et-hero-tabs-container--top-first');
}
else {
$('.et-header').removeClass('et-header--move-up');
$('.et-hero-tabs-container').removeClass('et-hero-tabs-container--top-first');
$('.et-hero-tabs-container').removeClass('et-hero-tabs-container--top-second');
}
}
findCurrentTabSelector(element) {
let newCurrentId;
let newCurrentTab;
let self = this;
$('.et-hero-tab').each(function() {
let id = $(this).attr('href');
let offsetTop = $(id).offset().top - self.tabContainerHeight;
let offsetBottom = $(id).offset().top + $(id).height() - self.tabContainerHeight;
if($(window).scrollTop() > offsetTop && $(window).scrollTop() < offsetBottom) {
newCurrentId = id;
newCurrentTab = $(this);
}
});
if(this.currentId != newCurrentId || this.currentId === null) {
this.currentId = newCurrentId;
this.currentTab = newCurrentTab;
this.setSliderCss();
}
}
setSliderCss() {
let width = 0;
let left = 0;
if(this.currentTab) {
width = this.currentTab.css('width');
left = this.currentTab.offset().left;
}
$('.et-hero-tab-slider').css('width', width);
$('.et-hero-tab-slider').css('left', left);
}
}
new StickyNavigation();
- 이전글개행문자 표현 줄바꿈코드 23.05.02
- 다음글마우스 Drog & Drop을 이용하여 박스 23.04.28
댓글목록
등록된 댓글이 없습니다.