본문 바로가기
Js

circle, strokeDashoffset, scrollTop, 그래프

by 영감은어디에 2025. 3. 6.

<div class="content" style="height: 3000px; color: #fff;">
    <p style="position: fixed;">
       <span class="darray"></span>
    </p>
</div>

<svg id="circleIndicator" viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="25" stroke="yellow" stroke-width="3" fill="none"></circle>
    <text id="scrollPercentText" x="50" y="50" text-anchor="middle" dominant-baseline="middle" Fill="#fff" font-size="16">0%</text>
</svg>
let circleIndicator= document.getElementById('circleIndicator');
let circle = circleIndicator.querySelector('circle');
let textElement = document.getElementById('scrollPercentText');
let darrayElement = document.querySelector('.darray');

let radius = circle.getAttribute('r');
let circum = 2 * Math.PI * radius;
circle.style.strokeDasharray = circum;

function setProgress(percent){
    let offset = circum * (1 - percent / 100);
    circle.style.strokeDashoffset = offset;
    textElement.textContent = Math.round(percent) + '%';
    darrayElement.innerHTML = `strokeDasharray: ${circum.toFixed(2)}<br>strokeDashoffset: ${offset.toFixed(2)}`;
}

window.addEventListener('scroll', ()=> {
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
    const scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    const scrollPercent = (scrollTop / scrollHeight) * 100;
    setProgress(scrollPercent);
});

window.addEventListener('mousemove', (e) =>{
    circleIndicator.style.left = e.clientX + 'px';
    circleIndicator.style.top = e.clientY + 'px';
})
setProgress(0)

'Js' 카테고리의 다른 글

gsap, hue-rotate, mousemove  (0) 2025.03.15
smoothwheel, 부드러운 마우스 스크롤  (0) 2025.03.11
wow.js animate  (0) 2025.03.09
rotate, mouse move, delay  (0) 2025.03.07
gsap, mouse move  (0) 2025.03.05
원페이지 스크롤  (0) 2025.03.03
menu bar mouse move  (0) 2025.02.25
폴리곤. 컬러 로테이션 hue-rotate  (0) 2025.02.24