原生JS图片打散效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="main">
        <button>打散</button>
        <button>合起</button>
        <div>
            <img src="../img/2.jpg" alt="">
            <img src="../img/2.jpg" alt="">
            <img src="../img/2.jpg" alt="">
        </div>
    </div>
</body>
<script>
    var buttons=document.querySelectorAll('button')
    var imgs=document.querySelectorAll("img")
    buttons[0].onclick=function(){
    
    
        for(var i=0;i<imgs.length;i++){
    
    
            var x=Math.random()*400-200;
            var d=Math.random()*360;
            imgs[i].style.transform=`translateX(${
      
      x}px) rotate(${
      
      d}deg)`
        }
    }
    buttons[1].onclick=function(){
    
    
        for(var i=0;i<imgs.length;i++){
    
    
            imgs[i].style.transform=""
        }
    }
</script>
</html>

效果图如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42526440/article/details/113483613