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

home2 게시판 JavaScript, TS 게시판 캐러셀 스와이프 기능 질문 드립니다.

캐러셀 스와이프 기능 질문 드립니다.

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #34263

    조승현
    참가자

    안녕하세요 반복문 써서 확장성 있는 코드로 만들어 보려고 했는데 잘안되서 질문 드립니다.

    이동은 잘되는데, mousemove일 때 움직인 거리 만큼 드래그가 잘 안되서요.. 어떤게 잘못 되었을까요..

     

    <div style="overflow:hidden;position:relative;">
          <div class="img-container">
            <div class="img-cont">
              <img src="img.jpg">
            </div>
            <div class="img-cont">
                <img src="img.jpg">
            </div>
            <div class="img-cont">
                <img src="img.jpg">
            </div>
          </div>
          <button class="next-btn">next</button>
          <button class="prev-btn">prev</button>
        </div>
        <div class="btn-box">
            <button id="slide1">1</button>
            <button id="slide2">2</button>
            <button id="slide3">3</button>
        </div>

          <script>
              let startLocation = 0;
              let click = 0;
              for(let i=0;i<$('.img-cont').length - 1;i++){
                $('.img-container .img-cont').eq(i).on('mousedown',function(e){
                    startLocation = e.clientX;
                    console.log(startLocation);
                    click = 1;
                });
                $('.img-container .img-cont').eq(i).on('mousemove',function(e){
                    let 이동거리 = e.clientX - startLocation;
                    if(click == 1){
                        $('.img-container').css(transform, translateX(${이동거리}px - ${i}00vw));
                    }
                });
                $('.img-container .img-cont').eq(i).on('mouseup',(e)=>{
                    console.log(e.clientX - startLocation);
                    if(e.clientX - startLocation < -150){
                        $('.img-container').css('transform','translateX('+ (-1 - i) + '00vw)').css('transition', 'transform 0.5s');
                    }else{
                        $('.img-container').css('transform','translateX(' + i*-1 + 'vw)').css('transition', 'transform 0.5s');
                    }
                    setTimeout(function(){
                        $('.img-container').css('transition', 'none');
                    }, 500)
                    click = 0;
                })
                }
          </script>

    #34293

    codingapple
    키 마스터

    transform, translateX(${이동거리}px - ${i}00vw) 

    이거 전체에 따옴표가 있는데 각각 따옴표를 칩시다 

    css 값넣는 부분에서 뺄셈하고 싶으면 calc()로 감싸야합니다 

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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