购物车-多元素组合动画css

学习渡一课程 多元素组合动画练习。

在我们开发购物车功能时,经常会有点击添加按钮,就会有一个小圆点掉进购物车的动画,如下图所示,今天我们通过css来实现。

首先实现多元素组合动画

直接上代码,可以复制到本地使用:

javascript 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>多元素动画</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .view{
            width: 400px;
            height: 600px;
            position: relative;
          
            border: 1px solid #dedede;
        }
        .ShoppingCart {
            position: absolute;
            right: 0;
            bottom: 0;
            text-align: center;
            width: 80px;
            height: 40px;
            background-color: blueviolet;
            color: #ffffff;
        }
        
        .add {
            position: relative;
            width: 20px;
            height: 20px;
            top:50px;
            left: 10px;
            background-color: blue;
            color: #ffffff;
            text-align: center;
            line-height: 20px;
            cursor: pointer;
            border-radius: 50%;
        }
        .box{
            position: absolute;
            top: 0;
            left: 0;
            display: none;
        }
        .active{
            display: block;
            animation: move1 0.6s cubic-bezier(0, 0.36, 1, 0.59);
        }
        .ball{
            width: 20px;
            height: 20px;
            top: 0;
            left: 0;
            border-radius: 50%;
            background-color: blue;
            position: absolute;
            z-index: -1;
            
        }
        .active .ball{
            animation: move2 0.6s cubic-bezier(0.35,-0.35, 1, 0.38);
        }

        @keyframes move1 {
            to {
                transform: translateX(350px);
            }
        }
        @keyframes move2 {
            to {
                transform: translateY(510px);
            }
        }

    </style>
</head>

<body>
    <div class="view">
        <div class="add" id="add">
            +
            <div class="box">
                <div class="ball"></div>
            </div>
        </div>
       
        <div class="ShoppingCart">
            购物车
        </div>
    </div>
   
    
</body>
<script>    

    let add = document.querySelector(".add")
    let box = document.querySelector(".box")
    add.addEventListener("click",()=>{
        box.className = "box active"
        setTimeout(()=>{
            box.className = "box"
        },2000)
    })
</script>
</html>

实现多个动画

上代码

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>多种元素动画</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .view{
            width: 400px;
            height: 600px;
            position: relative;
          
            border: 1px solid #dedede;
        }
        .ShoppingCart {
            position: absolute;
            right: 0;
            bottom: 0;
            text-align: center;
            width: 80px;
            height: 40px;
            background-color: blueviolet;
            color: #ffffff;
        }
        
        .add {
            position: relative;
            width: 20px;
            height: 20px;
            top:50px;
            left: 10px;
            background-color: blue;
            color: #ffffff;
            text-align: center;
            line-height: 20px;
            cursor: pointer;
            border-radius: 50%;
        }
        .box{
            position: absolute;
            top: 0;
            left: 0;
            animation: move1 0.6s cubic-bezier(0, 0.36, 1, 0.59);
        }
    
        .ball{
            width: 20px;
            height: 20px;
            top: 0;
            left: 0;
            border-radius: 50%;
            background-color: blue;
            position: absolute;
            z-index: -1;
            animation: move2 0.6s cubic-bezier(0.35,-0.35, 1, 0.38);
        }
    

        @keyframes move1 {
            to {
                transform: translateX(350px);
            }
        }
        @keyframes move2 {
            to {
                transform: translateY(510px);
            }
        }

    </style>
</head>

<body>
    <div class="view">
        <div class="add" id="add">
            +
            
        </div>
       
        <div class="ShoppingCart">
            购物车
        </div>
    </div>
   
    
</body>
<script>    

    let add = document.querySelector(".add")
    let box = document.querySelector(".box")
    add.addEventListener("click",()=>{
        let ele = document.createElement("div")
        ele.className = "box"
        ele.innerHTML = "<div class='ball'></div>"
        add.appendChild(ele)
        setTimeout(()=>{
            ele.parentNode.removeChild(ele);
        },1000)
    })
</script>
</html>
相关推荐
恋猫de小郭25 分钟前
JetBrains Terminal 又发布新架构,Android Studio 将再次迎来新终端
android·前端·flutter
gaog2zh41 分钟前
0401react中使用css-react-css-仿低代码平台项目
css·react.js·低代码
夕秋一梦1 小时前
vue项目本地调试使用https
前端·vue.js·https
小破孩呦1 小时前
动态列表的数据渲染、新增、编辑等功能开发及数据处理
前端·javascript·elementui
成长ing121381 小时前
点击音效系统
前端·cocos creator
熟悉不过1 小时前
cesium项目之cesiumlab地形数据加载
前端·javascript·vue.js·cesium·webgis·cesiumlab
神经毒素2 小时前
WEB安全--XSS--DOM破坏
前端·web安全·xss
不简说2 小时前
sv-print可视化打印组件不完全指南③
前端·javascript·vue.js
前端摸鱼杭小哥2 小时前
Vue 开发者狂喜!我在 React 中完美复刻了 v-if/v-for 指令
前端·vue.js·react.js