I'm

Developer, Designer, Photographer

GET IN TOUCH

  • Buk-gu, Busan, Republic of Korea
  • ungdoli0916@naver.com
  • Kakao ID : Ungdoli
Your message has been sent. Thank you!
(즐거웅코드) 제이쿼리 랜덤 위치에 객체 생성하기

· 즐거'웅' 코드 (Source)/HTML & CSS & JAVASCRIPT
2021. 8. 21. 15:39

반응형

[즐거'웅'코드] 목차

  1. 예제 소스
  2. 구현 예시
예제 소스
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
29
30
31
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
 
<style>
    body {
        position: relative;
    }
 
    .randomBox {
        width: 50px;
        height: 50px;
        background: red;
        border-radius: 25px;
        position: absolute;
    }
</style>
 
<button onclick="making_randomBox()">랜덤 위치 값 박스 생성</button>
 
<script>
    function making_randomBox() {
        $("body .randomBox").remove();
        $("body").append("<div class='randomBox'></div>");
 
        $(".randomBox").each(function () {
            $(this).css({
                left: Math.random() * ($("body").width() - $(this).width()),
                top: Math.random() * ($("body").height() - $(this).height())
            });
        });
    }
</script>
구현 예시
반응형