인프런 커뮤니티 질문&답변

LEETY님의 프로필 이미지
LEETY

작성한 질문수

인터랙티브 웹 개발 제대로 시작하기

전진! 3D 스크롤 17

1분이가 계속 한자리에만 생기는데요. info.xPos가 일분이에게 안들어가는거 같아요. 놓친부분이 있는거겠죠?

해결된 질문

작성

·

169

0

(function(){//즉시 실행 함수
    const stageElem = document.querySelector('.stage');
    const houseElem = document.querySelector('.house');
    const barElem = document.querySelector('.progress-bar');
    const mousePos = {x:0,y:0};
    let maxScrollValue;


    function  resizeHandler(){
        maxScrollValue = document.body.offsetHeight - window.innerHeight;
    }

    window.addEventListener('scroll',function(){
        // pageYoffset 스크롤 한 값
        // console.log(this.pageYOffset);
        // 스크롤 범위 지정하기
        // console.log( pageYOffset/maxScrollValue);
        const scrollPer = pageYOffset/maxScrollValue
        const zMove =scrollPer*970 -490;
        houseElem.style.transform = 'translateZ('+zMove+'vw)';
        // progress bar
        barElem.style.width = scrollPer *100 + '%';
    })





    window.addEventListener('mousemove',function(e){
        // console.log(e.clientX,e.clientY) 마우스 위치 알아내기
        mousePos.x = -1+(e.clientX /window.innerWidth)*2;
        mousePos.y = 1-(e.clientY /window.innerHeight)*2;

        stageElem.style.transform='rotateX('+(mousePos.y*5)+'deg) rotateY('+(mousePos.y*5)+'deg)';
        // console.log(mousePos);
    })
    window.addEventListener('resize',resizeHandler)
    resizeHandler();

    stageElem.addEventListener('click',function(e){

        // console.log(e.clientX/window.innerWidth*100)
            new Character({//객체의 속성으로 넣어야 함. 다른 것들도 추가해야 하기때문에
                xPos :e.clientX/window.innerWidth*100
            });


    })


})()
function Character(info) {
    this.mainElem = document.createElement('div');
    this.mainElem.classList.add('charactor');
    this.mainElem.innerHTML = '' +
        ' <div class="character" >' +
            ' <div class="character-face-con character-head">' +
                '<div class="character-face character-head-face face-front"></div>' +
                '<div class="character-face character-head-face face-back"></div>' +
            '</div>' +
            ' <div class="character-face-con character-torso">' +
                '<div class="character-face character-torso-face face-front"></div>' +
                '<div class="character-face character-torso-face face-back"></div>' +
            ' </div>' +
            '<div class="character-face-con character-arm character-arm-right">' +
                ' <div class="character-face character-arm-face face-front"></div>' +
                ' <div class="character-face character-arm-face face-back"></div>' +
            '</div>' +
            '  <div class="character-face-con character-arm character-arm-left">' +
                '<div class="character-face character-arm-face face-front"></div>' +
                '<div class="character-face character-arm-face face-back"></div>' +
            '</div>' +
            '  <div class="character-face-con character-leg character-leg-right">' +
                '<div class="character-face character-leg-face face-front"></div>' +
                '<div class="character-face character-leg-face face-back"></div>' +
            '</div>' +
            '  <div class="character-face-con character-leg character-leg-left">' +
                '<div class="character-face character-leg-face face-front"></div>' +
                '<div class="character-face character-leg-face face-back"></div>' +
            '</div>' +
        '</div>';

        document.querySelector('.stage').appendChild(this.mainElem);


        this.mainElem.style.leftinfo.xPos+'%';
}

답변 2

0

LEETY님의 프로필 이미지
LEETY
질문자

감사합니다!

0

1분코딩님의 프로필 이미지
1분코딩
지식공유자

function Character() 에서 오타가 있습니다.

this.mainElem.classList.add('charactor');

charactor를 character로 바꾸어주세요~ 클래스 이름이 제대로 안들어가서 선택이 안되는 문제였습니다.

this.mainElem.classList.add('character');

LEETY님의 프로필 이미지
LEETY

작성한 질문수

질문하기