Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- unique identifiers
- 진행기록
- middleware pattern
- 이벤트
- 딥다이브
- mixin pattern
- 자바스크립트 패턴
- 블로그 서비스 최적화
- js pattern
- pr review
- const
- 이미지 갤러리 최적화
- 프론트엔드 성능 최적화 가이드
- 새 코드 받아오기
- 모던 자바스크립트 Deep Dive
- 학습내용정리
- 커리어
- package management
- js pattern
- 제너레이터와 async/await
- 올림픽 통계 서비스 최적화
- 자바스크립트
- 프로그래머스
- Babel과 Webpack
- peerdependencies
- 자바스크립트 딥다이브
- version management
- 스코프
- DOM
- 브라우저의 렌더링 과정
Archives
- Today
- Total
Dev Blog
Contact Section & scrollTo 구현_20210605_Day11 본문
진행 사항
- Contact Section 구현
- Nav 버튼 클릭 scrollTo 로직 구현
- 전체적으로 CSS re-sizing
Contact Section
ScrollTo
import React from "react";
import styled from "styled-components";
import theme from "../common/style/themes/default";
import MainLogo from "../assets/images/logo.png";
import GitLogo from "../assets/images/github.png";
import BlogLogo from "../assets/images/tistory.svg";
function MainNav() {
//스크롤 발생시 위치값에 따라 네비 버튼 색 변경
document.addEventListener("scroll", () => {
const about = document.getElementsByClassName("about")[0].offsetTop;
const works = document.getElementsByClassName("works")[0].offsetTop;
const contact = document.getElementsByClassName("contact")[0].offsetTop;
const navBtns = document.querySelectorAll(".navBtn");
const btnAbout = document.getElementsByClassName("navAbout");
const btnWorks = document.getElementsByClassName("navWorks");
const btnContact = document.getElementsByClassName("navContact");
navBtns.forEach((btn) => {
btn.style.color = "black";
});
let currentScrollValue = document.documentElement.scrollTop;
if (about <= currentScrollValue && currentScrollValue < works) {
btnAbout[0].style.color = theme.palette.green;
} else if (works <= currentScrollValue && currentScrollValue < contact) {
btnWorks[0].style.color = theme.palette.green;
} else if (contact <= currentScrollValue) {
btnContact[0].style.color = theme.palette.green;
}
});
const handleScroll = (e) => {
//네비 버튼 클릭시 해당 위치로 scroll 위치 이동
const target = e.target.innerText;
const home = document.getElementsByClassName("home")[0].offsetTop;
const about = document.getElementsByClassName("about")[0].offsetTop;
const works = document.getElementsByClassName("works")[0].offsetTop;
const contact = document.getElementsByClassName("contact")[0].offsetTop;
if (target === "About") {
window.scrollTo({ top: about, behavior: "smooth" });
} else if (target === "Works") {
window.scrollTo({ top: works, behavior: "smooth" });
} else if (target === "Contact") {
window.scrollTo({ top: contact, behavior: "smooth" });
} else {
window.scrollTo({ top: home, behavior: "smooth" });
}
};
return (
<Main>
<NavList>
<List>
<Scroll className="navBtn navAbout" onClick={handleScroll}>
About
</Scroll>
</List>
<List>
<Scroll className="navBtn navWorks" onClick={handleScroll}>
Works
</Scroll>
</List>
<List>
<Scroll className="navBtn navContact" onClick={handleScroll}>
Contact
</Scroll>
</List>
<List>
<Scroll>
<LogoImg
className="navBtn navHome"
src={MainLogo}
onClick={handleScroll}
/>
</Scroll>
</List>
<List>
<Scroll>
<SocialLink href="https://github.com/JayKim88" target="_blank">
<SocialImg src={GitLogo} />
</SocialLink>
</Scroll>
</List>
<List>
<Scroll>
<SocialLink
href="https://nomadkim880901.tistory.com/"
target="_blank"
>
<SocialImg src={BlogLogo} />
</SocialLink>
</Scroll>
</List>
</NavList>
</Main>
);
}
...생략...
export default MainNav;
더 간단한 방법이 있을 것이다.
도움받은 사이트
- 스크롤 이벤트: https://www.everdevel.com/JavaScript/scroll-event/
'Projects > JPortfolio' 카테고리의 다른 글
About_Section2 & Works 구현_20210602-03_Day9,10 (0) | 2021.06.04 |
---|---|
About_Section2 구현중_20210601_Day8 (0) | 2021.06.02 |
About_Section1 구현 완료_20210531_Day7 (0) | 2021.05.31 |
Nav, Home, About 구현_20210530_Day6 (0) | 2021.05.31 |
프로젝트 기획_컴포넌트 세분화 작업_20210528_Day5 (0) | 2021.05.29 |
Comments