스크립트 관련 질문이 있습니다.
style 로 값을 준 후에
addEventListener('transitionend') 이벤트를 주려고 합니다.
end가 붙어있으면 제거해주고
없으면 end 추가해주는 식으로 했는데..
처음 클릭했을 때 end 클래스가 계속 보여졌다 사라졌다 합니다ㅠㅠㅠㅠ
if 문 안에 return; 을 적용했는데도 동일한 현상이 보여집니다ㅠㅠ
const ballElem = document.querySelector('.ball');
let defult = false;
window.addEventListener('click', function(e) {
ballElem.style.transform = 'translate(' + (e.clientX - 15) +'px, ' + (e.clientY - 15) + 'px)';
ballElem.addEventListener('transitionend', function(e) {
if ( !defult ) {
ballElem.classList.add('end');
defult = true;
return;
} else if ( defult) {
ballElem.classList.remove('end');
defult = false;
return;
}
});
});
답변 2
2
end 클래스가 추가되면 빨강 -> 파랑으로 트랜지션이 일어나고,
end 클래스가 제거되면 파랑 -> 빨강으로 트랜지션이 일어나죠?
즉 end 클래스가 추가되든 제거되든, 무조건 계속 transitionend 이벤트가 일어나서 무한 반복되는 현상입니다.
transitionend 대신 setTimeout으로 아래처럼 해보세요^^
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Transition/Animation Event</title>
<style>
.ball {
position: absolute;
left: 0;
top: 0;
width: 30px;
height: 30px;
border-radius: 50%;
background: red;
transition: 2s;
}
.ball.end {
background: dodgerblue;
}
</style>
</head>
<body>
<div class="ball"></div>
<script>
const ballElem = document.querySelector('.ball');
let colorState = false;
let clickable = true;
window.addEventListener('click', function (e) {
ballElem.style.transform = 'translate(' + (e.clientX - 15) + 'px, ' + (e.clientY - 15) + 'px)';
if (!clickable) return;
clickable = false;
setTimeout(function () {
if (!colorState) {
ballElem.classList.add('end');
colorState = true;
} else {
ballElem.classList.remove('end');
colorState = false;
}
clickable = true;
}, 2000);
});
</script>
</body>
</html>
왼쪽/오른쪽 동작시 딜레이 문제
0
93
1
변수 범위 관련 질문
0
106
1
perspective 문의
0
100
1
생성자 함수를 클래스 함수로 변경 하고 this 오류 관련
0
150
1
스크롤이 중간 위치에 있을 때 창의 크기를 변환하면 생기는 문제
0
127
1
animation이벤트 질문이요!
0
71
1
resize handler에서 질문이 있습니다.
0
110
1
카드 뒤집힐 때 F가 보인 이유
0
149
1
3d 뒤집기 추가효과
0
217
1
전진! 3D 스크롤 21 강의 질문
1
171
1
eventlistener 질문
0
148
1
zMove 를 1000으로 설정하는 이유에 대하여.
0
168
1
[정보-23강] ES6 class 문법으로 공부하시는 분들!! 화살표 함수로도 시도해보셔요!
1
191
1
동적으로 html 생성 후 이벤트 위임 질문 있습니다.
0
265
1
rotateY()에서 deg에 따른 차이
0
197
1
코드 작성 순서
0
275
1
이미지가 없는데 첨부파일을 다운 받는 방법이 있나요??
1
394
1
'이벤트 위임 보강 영상'에 있는 예제 html이 안 보입니다
0
265
2
섹션5 자바스크립트 이벤트 다루기 질문
1
243
1
[#전진! 3D 스크롤 11] mousePos 공식 질문 있습니다!
0
423
2
css 는 직접 작성을 해야하는걸까용?
0
325
1
translateZ 에 px 이 아닌 vw 로 값을 주신 이유가 있을가요?
0
359
2
house 부분에도 width , height 부분을 꽉 차게 주신 부분이 제가 이해한게 맞는지 궁금합니다.
0
304
2
left:-400vw 가 아닌 translateZ(100vw); 을 입력하신 이유가 궁금합니다.
0
305
2





