javaScript/jQuery
이미지 랜덤하게 출력하기
mooyou
2023. 5. 10. 10:25
728x90
300x250
SMALL
jQuery 코드
var $imgs = null;
$(document).ready(function(){
init();
initPos();
})
function init(){
$imgs = $(".container img");
}
function initPos(){
$imgs.each(function(){
let left = Math.floor(Math.random()*400);
let top = Math.floor(Math.random()*400);
$(this).css({
left:left,
top:top
})
})
}
$imgs 변수를 만들어서 .container img들을 가져온다.
each 메서드를 사용해서 각각의 이미지들에 랜덤한 값을 left와 top 변수에 저장
랜덤한 수치를 css 적용한다.
728x90
반응형
LIST