CSS Note 2.2 transition和transform组合动画

本文详细介绍了CSS中过渡(transition)和变换(transform)的使用方法,包括如何设定过渡属性、过渡时间、过渡速度和延迟时间,以及如何使用变换属性实现元素的位置、大小和旋转变化。通过具体实例展示了不同过渡效果的应用。

一、CSS

@charset "UTF-8";

div{
    margin: 10px;
    padding: 10px;
    width: 160px;
    height: 80px;
    background: chartreuse ;

}
#div1:hover{
    background: green;
    width: 60px;
    transition-property: width;/*设定对宽度进行过渡*/
    transition-duration: 1s;/*设定过渡时间*/
    transition-timing-function: ease;/*加速过渡;ease-in以慢速开始的过渡,ease-out以慢速结束的过渡,ease-in-out以慢速开始和结束的过渡*/
    transition-delay: 0.5s;/*设定延时过渡时间*/
}
#div2:hover{
    background: blue;
    width: 60px;
    transition-property: all;/*对所有属性过渡*/
    transition-duration: 1s;
    transition-timing-function: linear;/*匀速过渡*/
}
#div3:hover{
    background: red;
    width: 60px;
    transition: background 3s ease-in-out 3s;/*延时3秒,过渡时间为2s,对背景颜色过渡,过渡是先慢后快再慢*/
}
#div4{
    margin: 4px;
    padding: 4px;
    width: 320px;
    height: 200px;
    background-color: lightyellow;
    border-radius: 10px;
    border: 2px brown dashed;
    vertical-align: middle;
}
img{
    width: 60px;
    height: 200px;
}
img:hover{
    position: relative;
    transform-origin: 50% 50%;
    transform: translate3D(200px, -50px ,100px) rotate3D(1,1,1,3600deg) scale(2);
    transition: transform 2s ease 0s;
    bottom: 20px;
}

二、HTML

<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
    <div id="div4">
        <img src="..\sp\gh1.jpg" alt="test">
        <img src="..\sp\gh2.jpg" alt="test">
        <img src="..\sp\gh3.jpg" alt="test">
        <img src="..\sp\gh4.jpg" alt="test">
        <img src="..\sp\gh5.jpg" alt="test">
    </div>
</body>

三、效果展示

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>祝福便签动画特效</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { overflow: hidden; background-color: #f0f0f0; } .note { position: absolute; padding: 12px 16px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); font-family: "微软雅黑", sans-serif; color: #333; opacity: 0; transform: scale(0.5); transition: opacity 0.8s ease, transform 0.8s ease, top 0.8s ease, left 0.8s ease; } .note.show { opacity: 1; transform: scale(1); } .final-note { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(0); padding: 20px 30px; border-radius: 10px; background-color: #ff6b8b; color: white; font-size: 24px; font-weight: bold; box-shadow: 0 8px 16px rgba(255, 107, 139, 0.3); opacity: 0; transition: transform 1s ease, opacity 1s ease; } .final-note.show { transform: translate(-50%, -50%) scale(1); opacity: 1; } </style> </head> <body> <div class="final-note" id="finalNote">我喜欢你</div> <script> // 祝福文案列表 const notes = [ "记得吃饭", "天气冷了多穿点", "我很想你", "照顾好自己", "早点休息", "你笑起来真好看", "要开心哦", "有我在呢", "想你一起看日落", "你是我的小幸运" ]; // 便签颜色列表 const colors = [ "#ffcccc", "#ccffcc", "#ccccff", "#ffcc99", "#ccffff", "#ffccff", "#99ffcc", "#ff99cc" ]; // 创建并显示所有便签 function createNotes() { const container = document.body; notes.forEach((text, index) => { const note = document.createElement("div"); note.className = "note"; note.textContent = text; note.style.backgroundColor = colors[index % colors.length]; // 随机位置 note.style.top = `${Math.random() * 80 + 10}%`; note.style.left = `${Math.random() * 80 + 10}%`; container.appendChild(note); // 延迟显示,营造逐渐出现的效果 setTimeout(() => { note.classList.add("show"); }, index * 200); }); } // 便签收敛并显示最终表白便签 function convergeNotes() { const allNotes = document.querySelectorAll(".note"); allNotes.forEach((note, index) => { setTimeout(() => { note.style.top = "50%"; note.style.left = "50%"; note.style.opacity = "0"; note.style.transform = "translate(-50%, -50%) scale(0)"; }, index * 100); }); // 显示最终表白便签 setTimeout(() => { const finalNote = document.getElementById("finalNote"); finalNote.classList.add("show"); }, allNotes.length * 100 + 500); } // 执行动画流程 createNotes(); // 等所有便签完全显示后开始收敛(可根据需要调整时间) setTimeout(convergeNotes, notes.length * 200 + 2000); </script> </body> </html>
最新发布
11-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值