저는 jQuery를 배우고 그 기능을 테스트하고 있으며 제가 만든 예제에 문제가 있습니다.
https://codepen.io/MaxVelichkin/pen/pBJeZy
/*remove animate2 class and assign animate1 class to target*/
$(function() {
$('#trigger').on('click', function() {
$('#target').removeClass('animate2');
$('#target').addClass('animate1');
});
});
/*remove animate1 class and assign animate2 class to target*/
$(function() {
$('#trigger2').on('click', function() {
$('#target').removeClass('animate1');
$('#target').addClass('animate2');
});
});
/*just a container around*/
#container {
margin: 10px;
width: 350px;
height: 350px;
border: solid green 1px;
}
/*green button*/
#trigger {
width: 50px;
height: 50px;
border-radius: 100%;
background-color: green;
margin: 20px auto;
}
/*red button*/
#trigger2 {
width: 50px;
height: 50px;
border-radius: 100%;
background-color: red;
margin: 20px auto;
}
/*Target div which will be changing*/
#target {
width: 100px;
height: 100px;
border: solid blue 1px;
margin: 10px auto;
}
/*Keyframes for green button*/
@keyframes myfirst {
0% {
background: white;
width: 100px;
height: 100px;
border-radius: 0%;
}
100% {
background: blue;
width: 150px;
border-radius: 100%;
}
}
/*Keyframes for red button*/
@keyframes mysecond {
0% {
background: blue;
width: 150px;
border-radius: 100%;
}
100% {
background: white;
width: 100px;
height: 100px;
border-radius: 0%;
}
}
/*cusstom class to be assigned by green button*/
.animate1 {
-webkit-animation: myfirst 3s;
animation: myfirst 3s;
height: 100px;
background: blue;
width: 150px;
border-radius: 100%;
margin: 10px auto;
}
/*cusstom class to be assigned by red button*/
.animate2 {
-webkit-animation: mysecond 3s;
animation: mysecond 3s;
height: 100px;
width: 150px;
border: solid red 1px;
border-radius: 0%;
margin: 10px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<div id="trigger"></div>
<div id="trigger2"></div>
<div id="target"></div>
</div>
다음이 있습니다.
버튼을 클릭하면 대상 div에 클래스를 할당하고 다른 버튼에 의해 할당 된 클래스를 제거합니다.
질문 1 : 녹색 버튼 애니메이션이 끝난 후 대상 div의 너비가 100px로 다시 변경되는 이유는 무엇입니까 (150px로 유지되지 않는 이유)?
질문 2 : 모든 작업을 올바르게 수행합니까 아니면 더 나은 jQuery 접근 방식이 있습니까?
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다