[CSS] SCSS를 활용한 툴팁(tooltip) 구현해보자! (feat. transform-origin)

2024. 1. 11. 11:15CSS

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

반응형

 

 

툴팁 레이아웃(html) 및 스타일(scss)

 

<div class="wrap">
  <button>
    New
    <span class="tooltip">새로운 소식!</span>
  </button>
</div>

 

.wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  outline: 1px solid red;
  button {
    position: relative;
  }
  .tooltip{
    position: absolute;
    bottom: calc(-100% - 20px - 6px - 2px); //패딩, 삼각형 높이, 사이 간격 값들 빼기
    left: 50%;
    z-index: 999;
    transform: translateX(-50%);
    width: 150px;
    padding: 10px;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.25);
    box-sizing: border-box;
    
    &::before {
      position: absolute;
      top: -6px;
      left: 50%;
      transform: translateX(-50%);
      z-index: 998;
      display: inline-block;
      content: '';
      border: solid transparent;
      border-width: 0 6px 6px;
      border-bottom-color: #fff;
    }
  }
}

 

스타일(CSS) 반영된 툴팁(tooltip) UI

 

 

툴팁 모션(motion) 추가

삼각형(화살표) 기준으로 툴팁 크기(scale)가 0에서 100으로 나타났다(show), 사라졌다(hide)하는 모션 스타일을 추가해 봅시다.

.wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  outline: 1px solid red;
  button {
    position: relative;
  }
  .tooltip{
    position: absolute;
    bottom: calc(-100% - 20px - 6px - 2px);
    left: 50%;
    z-index: 999;
    transform: translateX(-50%);
    width: 150px;
    padding: 10px;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.25);
    box-sizing: border-box;
    // 툴팁 모션 추가하기
    transform: scale(0) translateX(-50%);
    transform-origin: 0 -6px;
    transition: transform .3s cubic-bezier(.9, .28, .45, .75);
    
    &::before {
      position: absolute;
      top: -6px;
      left: 50%;
      transform: translateX(-50%);
      z-index: 998;
      display: inline-block;
      content: '';
      border: solid transparent;
      border-width: 0 6px 6px;
      border-bottom-color: #fff;
    }
    
    &.show {
      transform: scale(1) translateX(-50%);
      transition: transform .3s cubic-bezier(.9, .28, .45, .75);
    }
    
    &.hide {
      transform: scale(0) translateX(-50%);
      transition: transform .3s cubic-bezier(.9, .28, .45, .75);
    }
    
  }
}

 

 

 

이제 스크립트를 정의해 봅시다.

페이지가 로딩이 완료되면 툴팁 요소에 show 클래스를 추가합니다. 이로써 툴팁이 화면에 나타나게 됩니다.
setTimeout을 활용하여 툴팁이 5초 동안 화면에 나타나고, 그 후에 사라지게 처리했습니다.

const tooltip = document.querySelector('.tooltip');

window.addEventListener('DOMContentLoaded', () => {
  tooltip.classList.add('show');
  setTimeout(function() {
    tooltip.classList.remove('show');
    tooltip.classList.add('hide');
  }, 5000);
})

 

transform-origin 속성 이해하기

transform-origintransform 속성과 함께 사용되어 요소의 변형 중심을 조절하는 데 유용합니다.

이 속성을 사용하면 해당 요소의 위치를 기준으로 회전(rotate), 크기 조절(scale), 기울임(skew) 등의 변형이 발생할 때 중심점을 지정할 수 있습니다. transform-origin의 값은 요소의 크기를 기준으로 상대적인 길이키워드로 지정할 수 있습니다.

 

길이

- px, 백분율(%) 등으로 지정할 수 있습니다.

- 0, 0%는 해당 방향의 시작 지점을 나타냅니다.

- 100%는 방향의 끝 지점을 나타냅니다.

 

키워드

- center : 요소의 중심을 나타냅니다.

- top, bottom, left, right : 각 상단, 하단, 좌측, 우측을 나타냅니다.

 

 

 

 

 

 

 

[Javascript] 브라우저 창 크기를 측정하는 주요 속성들에 대해서 알아보자

window.screen.width, window.screen.height, window.outerWidth, window.outerHeight, window.innerWidth, window.innerHeight, document.documentElement.clientWidth, document.documentElement.clientHeight 이러한 속성들은 모두 창의 크기를 나타내지

dev-chim.tistory.com

 

 

Vite에서 모듈들 간단하게 설치해서 사용하기 (feat. swiper) - 3편

https://swiperjs.com/ Swiper - The Most Modern Mobile Touch Slider Swiper is the most modern free mobile touch slider with hardware accelerated transitions and amazing native behavior. swiperjs.com 홈페이지 구축할 때, 자주 사용하는 UI 중 하

dev-chim.tistory.com

 

[Javascript] JavaScript로 스크롤 조작하기, scrollTo(), scrollBy(), scrollIntoView()

scrollTo(), scrollBy(), scrollIntoView() 메서드를 활용해 스크롤 조작하는 방법에 대해서 알아보겠습니다. scrollTo() 이동할 좌표값을 받아 절대적인 위치로 스크롤을 이동시킵니다. window.scrollTo(x좌표, y좌

dev-chim.tistory.com

 

 

 

[git] Github Desktop으로 아주 쉽게 저장소(repository)를 로컬에 클론(clone)하기

Github Desktop으로 아주 쉽게 저장소(repository)를 로컬에 클론(clone)하는 방법에 대해서 알아보겠습니다. Github Desktop 사이트 바로가기 Github에서 제공하는 애플리케이션으로 GUI(Graphical User Interface)로

dev-chim.tistory.com

 

 

[Utility] Snipaste - 무료 캡쳐 도구, 핀(Pin) 고정, 클립보드(Clipboard), 색상 추출(Color Picker)

스니페이스트(Snipaste)는 화면 캡쳐부터 다양한 편집 기능을 제공하여 작업 효율성을 높여주는 도구입니다. 가장 매력적인 기능으로 핀 고정이 있고, 스크린샷을 저장할 수 있음은 물론이고, 클

dev-chim.tistory.com

 

반응형