Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- nodejs교과서
- 클래스컴포넌트
- npm명령어
- React
- node
- NPM
- nodeJS
- component
- 제로초예제
- 리액트컴포넌트
- NoSQL
- mongoose
- MongoDB
- 리액트스타디
- mongo
- 시퀄라이즈공부
- 리액트
- sementicversion
- 시퀄라이즈
- express-generator
- sequelize
- 리액트기초
- React Component
Archives
- Today
- Total
개발노트
06 로또번호뽑기 본문
import React, { useState, useRef, useEffect } from 'react';
/*
바뀌는 부분 - 숫자 나오는 부분 / 버튼
- getNumbers 60이하의 숫자 7개 랜덤으로 뽑아서 리턴
*/
// 랜덤 숫자 리턴
function getRandomNums(count, max) {
const nums = new Set();
while (nums.size < count) {
nums.add(Math.floor((Math.random() * max) + 1));
}
// const bonus = nums[count];
// nums.remove(nums[count])
const result = (Array.from(nums)).sort((a, b) => a - b);
return result;
}
// 숫자 번호대별로 다른 클래스 추가
function getColorClassName(num) {
if (num < 10) {
return 'zeros';
} else if (num >= 10 && num < 20) {
return 'tens';
} else if (num >= 20 && num < 30) {
return 'twenties';
} else if (num >= 30 && num < 40) {
return 'thirties';
} else if (num >= 40 && num < 50) {
return 'fourties';
} else {
return 'fifties';
}
}
const Lotto = () => {
const [nums, setNums] = useState(getRandomNums(7, 60));
const [animating, setAnimating] = useState(false);
const listItems = useRef();
const fadeIn = () => {
listItems.current.forEach((v, i) => {
setAnimating(true);
setTimeout(() => {
v.classList.add('show');
if (listItems.current.length === i + 1) {
setAnimating(false);
}
}, 200 * (i + 1));
});
}
const fadeOut = () => {
listItems.current.forEach((v) => {
v.classList.remove('show');
})
}
useEffect(() => {
listItems.current = document.querySelectorAll('.nums li');
fadeIn();
return () => { };
}, []);
const onClickBtn = () => {
if (!animating) {
fadeOut();
setNums(getRandomNums(7, 60));
fadeIn();
}
}
return (
<>
<div className="lotto-wrap">
<h5>로또 추첨기</h5>
<ul className="nums">
{nums.map((v, i) => {
return (nums.length === i + 1) ?
<li className="bonus">{v}</li>
: <li className={`${getColorClassName(v)}`}> {v}</li>
})}
</ul>
<button className={animating ? `grey` : null}
onClick={onClickBtn}>한번더</button>
</div>
</>
)
}
export default Lotto;
.lotto-wrap {
margin: 10px;
width: 300px;
/* outline: 1px solid gold; */
}
.lotto-wrap ul.nums {
width: 100%;
height: 100px;
}
.lotto-wrap div {}
.lotto-wrap ul.nums li {
float: left;
width: 30px;
height: 30px;
text-align: center;
line-height: 33px;
border-radius: 50%;
margin: 8px;
border: 1px solid grey;
}
.zeros {
background-color: rgb(255, 255, 170);
}
.tens {
background-color: rgb(255, 170, 184)
}
.twenties {
background-color: #85c9ee;
}
.thirties {
background-color: rgb(189, 118, 255)
}
.fourties {
background-color: rgb(132, 208, 132);
}
.fifties {
background-color: rgb(139, 139, 255)
}
.bonus {
margin-left: 10px;
background-color: #d06a6a;
}
/* for fadeIn animation */
.nums li {
opacity: 0;
/* transform: translateY(20px); */
transition: opacity 0.5s ease, transform 0.5s ease;
}
.nums li.show {
opacity: 1;
transform: translateY(0);
}
button.grey {
background-color: #737373;
/*cursor: not-allowed*/
cursor: wait;
}
/* .nums li:nth-child(1) {
transition-delay: 0.1s;
}
.nums li:nth-child(2) {
transition-delay: 0.2s;
}
.nums li:nth-child(3) {
transition-delay: 0.3s;
}
.nums li:nth-child(4) {
transition-delay: 0.4s;
}
.nums li:nth-child(5) {
transition-delay: 0.5s;
}
.nums li:nth-child(6) {
transition-delay: 0.6s;
}
.nums li:nth-child(7) {
transition-delay: 0.9s;
} */
'React > webgame' 카테고리의 다른 글
08 지뢰찾기 (0) | 2024.04.20 |
---|---|
07 틱택토 (0) | 2024.04.16 |
05 가위바위보 (0) | 2024.04.11 |
04 반응속도체크 코드 (0) | 2024.04.09 |
03 숫자야구 코드 (0) | 2024.04.05 |