• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 JavaScript, TS 게시판 스크롤 응용 진척도 문제

스크롤 응용 진척도 문제

  • 이 주제에는 3개 답변, 2명 참여가 있으며 codingapple2 년 전에 전에 마지막으로 업데이트했습니다.
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 글쓴이
  • #85184

    Leehoya
    참가자
    .progress {
      width: 100%;
      height: 5px;
      background-color: #f0f0f0;
      position: fixed;
      top: 0;
      left: 0;
    }
    .progress-bar {
      height: 100%;
      background-color: black;
      width: 0%;
    }
    
    // js코드
    $(window).on("scroll",function(){
      var totalHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
      var progress =Math.floor((window.scrollY/totalHeight))*100;
      if(progress ==100){
       document.getElementsByClassName("progress-bar")[0].style.width="100%";
      }else if(progress ==1){
        document.getElementsByClassName("progress-bar")[0].style.width="1%";
      }else if(progress ==50){
        document.getElementsByClassName("progress-bar")[0].style.width="50%";
      }
    })
    
    이런식으로 했는데 진척도가 보이지 않고 console 찍어서 확인해보니까 progress가 0과 100으로만 나오는데 답을 모르겠어서 질문드립니다.
    
    
    #85205

    codingapple
    키 마스터
    window.scrollY/totalHeight 를 변수에 저장해두고 써봅시다
    #85303

    Leehoya
    참가자
    js 코드를 이런 식으로 수정해서 됐습니다 그런데
    $(window).on("scroll",function(){
      let totalHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
      let currHeight =document.documentElement.scrollTop;
      let percentage =((currHeight/totalHeight))*100;
      document.querySelector(".progress-bar").style.width=percentage+"%";
    })
    Math.floor() 를 없애니까 0에서 100으로 확 안 넘어 가지게 되던데 소수점 이하를 반올림하는 것이 0과 100으로 확
    넘어가는 이유에 대해서 궁금합니다.
    #85343

    codingapple
    키 마스터
    Math.floor는 가까운 정수로 내림해주는 함수라 0.8 도 0으로 만들어줘서 그런듯요
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠