본문 바로가기
Front/Vue

VUE3.0 흘러가는 텍스트

by saramnim 2024. 6. 14.
728x90
💛 내가 재활용하기 위해 작성
글 쓴지 참 오래됐다!

 

흘러가는 텍스트를 만들기 위한 코드다.

// template
<div id="scrolling-text-container">
    <span id="scrolling-text">
    	{{ title }}
    </span>
 </div>
// style
<style scoped>
#scrolling-text-container {
  width: 17.8rem;
  white-space: nowrap;
  overflow: hidden;
  position: relative;
}

#scrolling-text {
  display: inline-block;
  animation: scrollText 20s linear infinite;
}

@keyframes scrollText {
  0% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(-100%);
  }
}
</style>

 

만약 span의 길이가 해당 div의 길이를 넘어갈 때에만  흘러가게 하고싶다면?

// ts
const container = document.getElementById('scrolling-text-container')
const text = document.getElementById('scrolling-text')
if (container && text)
    checkOverflow(container, text)

function checkOverflow(container: HTMLElement, text: HTMLElement) {
    if (text.scrollWidth > container.clientWidth)
        text.classList.add('scroll')
    else
        text.classList.remove('scroll')
}
// style
<style scoped>
#scrolling-text-container {
  width: 17.8rem;
  white-space: nowrap;
  overflow: hidden;
  position: relative;
}

#scrolling-text {
  display: inline-block;
}

.scroll {
  animation: scrollText 20s linear infinite;
}

@keyframes scrollText {
  0% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(-100%);
  }
}
</style>

 

대충 요런 식으로 scroll 클래스를 넣었다 빼줬다 할 수 있다!

728x90
반응형

'Front > Vue' 카테고리의 다른 글

비/눈 오는 효과  (0) 2024.09.19
마우스 따라오는 원  (0) 2024.07.28
devExtreme 사용 중 만난 에러  (0) 2024.01.16
Vue3.0 + Nuxt 프로젝트 진행 후 정리  (0) 2023.11.22
Nuxt 모달 만들기 및 적용하기  (0) 2023.10.04

댓글

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