패럴랙스 효과
패럴랙스 이펙트 - 텍스트 효과
스크롤값에 따라서 텍스트가 하나 하나 씩 딜레이를 줘서 나타나게 만들어보겠습니다.
결과
HTML 작성
각각의 섹션을 만들고 보기 편하게 하기 위해서 스크롤 탑값을 보여주는 태그를 만들어줍니다.
소스 보기
<main id="parallax__cont">
<div id="contents">
<section id="section1" class="content__item">
<span class="content__item__num">01</span>
<h2 class="content__item__title">section1</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">산다는것 그것은 치열한 전투이다.</p>
</section>
<!-- //section1 -->
<section id="section2" class="content__item">
<span class="content__item__num">02</span>
<h2 class="content__item__title">section2</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">하루에 3시간을 걸으면 7년 후에 지구를 한바퀴 돌 수 있다.</p>
</section>
<!-- //section2 -->
<section id="section3" class="content__item">
<span class="content__item__num">03</span>
<h2 class="content__item__title">section3</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">내일은 내일의 태양이 뜬다.</p>
</section>
<!-- //section3 -->
<section id="section4" class="content__item">
<span class="content__item__num">04</span>
<h2 class="content__item__title">section4</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">배고픈 자 밥솥을 열어라.</p>
</section>
<!-- //section4 -->
<section id="section5" class="content__item">
<span class="content__item__num">05</span>
<h2 class="content__item__title">section5</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">그대 자신의 영혼을 탐구하라.</p>
</section>
<!-- //section5 -->
<section id="section6" class="content__item">
<span class="content__item__num">06</span>
<h2 class="content__item__title">section6</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">돈이 없는 것은 죄다.</p>
</section>
<!-- //section6 -->
<section id="section7" class="content__item">
<span class="content__item__num">07</span>
<h2 class="content__item__title">section7</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">무전 유죄, 유전 무죄</p>
</section>
<!-- //section7 -->
<section id="section8" class="content__item">
<span class="content__item__num">08</span>
<h2 class="content__item__title">section8</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">내 주머니의 푼돈이 남 주머니에 있는 거금보다 낫다.</p>
</section>
<!-- //section8 -->
<section id="section9" class="content__item">
<span class="content__item__num">09</span>
<h2 class="content__item__title">section9</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc split">최고에 도달하려면 최저에서 시작하라.</p>
</section>
<!-- //section9 -->
</div>
</main>
CSS 작성
CSS를 수작업으로 작성할시 매우 많아지기때문에 스크립트를 사용했습니다.
결과
/* parallax__nav */
#parallax__nav {
position: fixed;
right: 20px;
top: 20px;
z-index: 2000;
background-color: rgba(0, 0, 0, 0.4);
padding: 20px 30px;
border-radius: 50px;
transition: top 0.4s ease;
}
#parallax__nav li {
display: inline;
margin: 0 5px;
}
#parallax__nav li a {
display: inline-block;
height: 30px;
padding: 5px 20px;
text-align: center;
line-height: 30px;
}
#parallax__nav li.active a {
background: #fff;
color: #000;
border-radius: 20px;
box-sizing: content-box;
}
#parallax__cont {
max-width: 1600px;
width: 98%;
margin: 0 auto;
/* background-color: rgba(255, 255, 255, 0.1); */
}
.content__item {
width: 1000px;
max-width: 70vw;
margin: 30vw auto;
/* background-color: rgba(255, 255, 255, 0.3); */
text-align: left;
margin-right: 0;
position: relative;
padding-top: 8vw;
}
.content__item:nth-child(even) {
margin-left: 0;
text-align: right;
}
.content__item__num {
font-size: 35vw;
font-weight: 100;
font-family: 'Lato';
position: absolute;
left: -5vw;
top: -13vw;
opacity: 0.07;
z-index: -2;
}
.content__item:nth-child(even) .content__item__num {
left: auto;
right: -5vw;
}
.content__item__title {
font-weight: 400;
text-transform: capitalize;
}
.content__item__imgWrap {
width: 100%;
padding-bottom: 56.25%;
background: #000;
position: relative;
overflow: hidden;
z-index: -1;
}
.content__item__img {
position: absolute;
background: url(../assets/img/effect_bg19@2x.jpg);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
width: 110%;
height: 110%;
left: -5%;
top: -5%;
filter: saturate(0%);
/* transition: all 1s; */
}
.content__item:nth-child(1) .content__item__img {
background-image: url(../assets/img/effect_bg18@2x.jpg);
}
.content__item:nth-child(2) .content__item__img {
background-image: url(../assets/img/effect_bg19@2x.jpg);
}
.content__item:nth-child(3) .content__item__img {
background-image: url(../assets/img/effect_bg20@2x.jpg);
}
.content__item:nth-child(4) .content__item__img {
background-image: url(../assets/img/effect_bg17@2x.jpg);
}
.content__item:nth-child(5) .content__item__img {
background-image: url(../assets/img/effect_bg16@2x.jpg);
}
.content__item:nth-child(6) .content__item__img {
background-image: url(../assets/img/effect_bg15@2x.jpg);
}
.content__item:nth-child(7) .content__item__img {
background-image: url(../assets/img/effect_bg14@2x.jpg);
}
.content__item:nth-child(8) .content__item__img {
background-image: url(../assets/img/effect_bg13@2x.jpg);
}
.content__item:nth-child(9) .content__item__img {
background-image: url(../assets/img/effect_bg12@2x.jpg);
}
.content__item__desc {
font-size: 4vw;
line-height: 1.4;
margin-top: -4vw;
margin-left: -4vw;
word-break: keep-all;
}
.content__item:nth-child(even) .content__item__desc {
margin-left: auto;
margin-right: -4vw;
}
#parallax__info {
position: fixed;
left: 20px;
bottom: 20px;
z-index: 2000;
background-color: rgba(0,0,0,0.4);
color: #fff;
padding: 20px;
border-radius: 10px;
}
#parallax__info li,
.scroll {
line-height: 1.4;
}
@media (max-width: 800px) {
#parallax__cont {
margin-top: 70vw;
}
#parallax__nav {
padding: 10px;
right: auto;
left: 10px;
top: 50%;
transform: translateY(-50%);
border-radius: 5px;
background-color: rgba(0,0,0,0.8);
}
#parallax__nav li {
display: block;
margin: 5px;
}
#parallax__nav li a {
font-size: 14px;
padding: 5px;
border-radius: 5px;
height: auto;
line-height: 1;
}
#parallax__nav li.active a {
border-radius: 5px;
}
}
.split span {
opacity: 0;
transform: translateY(50px);
transition: all 0.6s cubic-bezier(0.75,-0.32, 0.07, 1.29);
display: inline-block;
min-width: 1vw;
}
.split span.show {
opacity: 1;
transform: translateY(0);
}
/* option */
/* .split span {
opacity: 0;
transform: translateY(50px);
transition: all 0.6s cubic-bezier(0.75,-0.32, 0.07, 1.29);
display: inline-block;
min-width: 1vw;
}
.split.show span {
opacity: 1;
transform: translateY(0);
}
.split span:nth-child(1) {transition-delay: 100ms;}
.split span:nth-child(2) {transition-delay: 150ms;}
.split span:nth-child(3) {transition-delay: 200ms;}
.split span:nth-child(4) {transition-delay: 150ms;}
.split span:nth-child(5) {transition-delay: 200ms;}
.split span:nth-child(6) {transition-delay: 250ms;}
.split span:nth-child(7) {transition-delay: 300ms;}
.split span:nth-child(8) {transition-delay: 350ms;}
.split span:nth-child(9) {transition-delay: 400ms;}
.split span:nth-child(10) {transition-delay: 450ms;}
.split span:nth-child(11) {transition-delay: 500ms;}
.split span:nth-child(12) {transition-delay: 550ms;}
.split span:nth-child(13) {transition-delay: 600ms;}
.split span:nth-child(14) {transition-delay: 650ms;}
.split span:nth-child(15) {transition-delay: 700ms;}
.split span:nth-child(16) {transition-delay: 750ms;}
.split span:nth-child(17) {transition-delay: 800ms;}
.split span:nth-child(18) {transition-delay: 850ms;}
.split span:nth-child(19) {transition-delay: 900ms;}
.split span:nth-child(20) {transition-delay: 950ms;}
.split span:nth-child(21) {transition-delay: 100ms;}
.split span:nth-child(22) {transition-delay: 1050ms;}
.split span:nth-child(23) {transition-delay: 1100ms;}
.split span:nth-child(24) {transition-delay: 1150ms;}
.split span:nth-child(25) {transition-delay: 1200ms;}
.split span:nth-child(26) {transition-delay: 1250ms;}
.split span:nth-child(27) {transition-delay: 1300ms;}
.split span:nth-child(28) {transition-delay: 1350ms;}
.split span:nth-child(29) {transition-delay: 1400ms;}
.split span:nth-child(30) {transition-delay: 1450ms;}
.split span:nth-child(31) {transition-delay: 1500ms;}
.split span:nth-child(32) {transition-delay: 1550ms;}
.split span:nth-child(33) {transition-delay: 1600ms;} */
JS 작성
일단 글씨를 변수로 선언해주고 split과 join을 이용해 글씨를 하나하나 쪼개서 span태그로 묶어줍니다. 단 이렇게 만들시 웹 접근성이 좋지 않아지므로 aria-hidden을 줍니다. 평소에 글씨들을 숨겨놓고, 위치값에 따라 스타일을 넣어서 순차적으로 딜레이를 걸어 글씨들이 나타나도록 만들어줍니다. 스크롤은 재귀함수를 이용해줍니다. 세부 설명들은 주석을 확인하시면 됩니다.^^
//글씨 하나씩 쪼개기
let text = document.querySelectorAll(".content__item__desc");
// let splitText = text.innerText;
// let splitWrap = splitText.split('').join('</span><span>')
// text.innerHTML = splitWrap = "<span>" + splitWrap + "</span>"
// 글씨 쪼개기(다중이)
document.querySelectorAll(".split").forEach(text => {
let splitText = text.innerText;
let splitWrap = splitText.split('').join('</span><span aria-hidden = "true">'); //접근성 때문에 aria-hidden을 준다
splitWrap = "<span aria-hidden = 'true'>" + splitWrap + "</span>";
text.innerHTML = splitWrap;
text.setAttribute("aria-label", splitText); // splitText에 aria-label로 값을 넣는다.
});
function scroll(){
let ScrollTop = window.scrollY;
document.querySelector("#parallax__info .scroll").innerText = Math.round(ScrollTop);
// CSS에 클래스 추가(노가다)
// document.querySelectorAll(".content__item").forEach(item => {
// if(ScrollTop >= item.offsetTop) {
// item.querySelector(".split").classList.add("show");
// }
// })
document.querySelectorAll(".content__item").forEach(item => {
if(ScrollTop >= item.offsetTop){
item.querySelectorAll(".split span").forEach((span, index) => {
span.classList.add("show");
span.style.transitionDelay = index + "00ms";
});
}
})
requestAnimationFrame(scroll);
// 첫 번째 span --> 0.01s
// 두 번째 span --> 0.02s
// 세 번째 span --> 0.03s
}
scroll();
// text.forEach((el) => {
// let splitText = el.innerText;
// let splitWrap = splitText.split('').join('</span><span>');
// el.innerHTML = splitWrap = "<span>" + splitWrap + "</span>";
// });
'Javascript_Effect' 카테고리의 다른 글
패럴랙스 이펙트 - 리빌 효과 (4) | 2022.10.06 |
---|---|
마우스 이펙트 - 텍스트 효과 (2) | 2022.09.29 |
Search Effect05 - filter()를 이용하여 속성의 중요도 보여주기 (7) | 2022.09.29 |
마우스 이펙트 - 기울기 효과 글씨 반전 효과 (10) | 2022.09.28 |
Search Effect04 - find() 사용 (7) | 2022.09.28 |
댓글