Contact Section & scrollTo 구현_20210605_Day11

2021. 6. 6. 01:49·Projects/JPortfolio_2021

진행 사항

- 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_2021' 카테고리의 다른 글

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
'Projects/JPortfolio_2021' 카테고리의 다른 글
  • About_Section2 & Works 구현_20210602-03_Day9,10
  • About_Section2 구현중_20210601_Day8
  • About_Section1 구현 완료_20210531_Day7
  • Nav, Home, About 구현_20210530_Day6
JTB
JTB
웹/앱 개발 정보를 공유하고 있습니다.
  • JTB
    JTechBlog
    JTB
  • 전체
    오늘
    어제
    • All About Programming;) N
      • Computer Science
        • Terminology and Concepts
        • Network
        • Operating System
        • Database
        • Data Structure
      • Frontend
        • Javascript Essentials
        • Perfomance Optimization
        • JS Patterns
        • Next.js
        • Flutter
      • Backend
        • Node.js
      • DevOps
        • Docker & Kubernetes
      • Coding Test N
        • LeetCode N
        • Programmers
      • Tech Books & Lectures
        • Javascript_Modern JS Deep d..
        • Network_IT 엔지니어를 위한 네트워크 입문
      • Projects
        • PolyLingo_2025
        • Build Your Body_2024
        • JStargram_2021
        • Covid19 Tracker_2021
        • JPortfolio_2021
      • BootCamp_Codestates
        • TIL
        • TILookCloser
        • Pre Tech Blog
        • IM Tech Blog
        • Daily Issues and DeBugging
        • First Project
        • Final Project
        • Sprint Review
        • Good to Know
        • Socrative Review
        • HTML &amp; CSS
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 글쓰기
    • 관리
  • 공지사항

  • 인기 글

  • 태그

    DOM
    커리어
    need a database
    프론트엔드 성능 최적화 가이드
    Data Structure
    Time complexity and Space complexity
    Shared resources
    자바스크립트
    Operating System
    Network
    스코프
    Binary Tree BFS
    모던 자바스크립트 Deep Dive
    How memory manage data
    TCP/IP
    structure of os
    database
    js pattern
    testing
    indie hacker
    Threads and Multithreading
    VoiceJournal
    mobile app
    자바스크립트 딥다이브
    딥다이브
    CPU scheduling algorithm
    leetcode
    이벤트
    polylingo
    JavaScript
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
JTB
Contact Section & scrollTo 구현_20210605_Day11
상단으로

티스토리툴바