如何为HTML表格添加动画效果?CSS怎么实现?

星降
发布: 2025-07-04 17:41:02
原创
700人浏览过
如何为HTML表格添加动画效果?CSS怎么实现?如何为HTML表格添加动画效果?CSS怎么实现?如何为HTML表格添加动画效果?CSS怎么实现?如何为HTML表格添加动画效果?CSS怎么实现?
<style>
    /* 基础表格样式,让效果更明显 */
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 20px;
    }
    th, td {
        border: 1px solid #ddd;
        padding: 12px 15px;
        text-align: left;
    }
    th {
        background-color: #f2f2f2;
    }

    /* 方案一:简单的行悬停动画 */
    tr {
        transition: background-color 0.3s ease, transform 0.3s ease;
    }
    tr:hover {
        background-color: #e0f7fa; /* 浅蓝色背景 */
        transform: scale(1.01); /* 轻微放大 */
        box-shadow: 0 4px 8px rgba(0,0,0,0.1); /* 增加阴影 */
        z-index: 1; /* 确保放大时不会被遮挡 */
        position: relative; /* 配合z-index */
    }

    /* 方案二:单元格点击反馈动画 */
    td {
        transition: background-color 0.2s ease, color 0.2s ease;
        cursor: pointer; /* 提示可点击 */
    }
    td:active { /* 模拟点击按下状态 */
        background-color: #c8e6c9; /* 浅绿色 */
        color: #333;
        transform: translateY(1px); /* 轻微下沉 */
    }

    /* 方案三:数据加载或新增行的入场动画 (需要JS配合添加类) */
    @keyframes fadeInSlideIn {
        from {
            opacity: 0;
            transform: translateY(20px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    .new-row-animation {
        animation: fadeInSlideIn 0.5s ease-out forwards;
    }

    /* 方案四:特定单元格闪烁提示 (例如,数据更新) */
    @keyframes highlightPulse {
        0% { background-color: transparent; }
        50% { background-color: #fffacd; } /* 柠檬纱 */
        100% { background-color: transparent; }
    }
    .highlight-cell {
        animation: highlightPulse 1.5s ease-in-out 2; /* 闪烁两次 */
    }
</style>

<table>
    <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>城市</th>
        </tr>
    </thead>
    <tbody>
        <tr class="new-row-animation">
            <td>张三</td>
            <td>30</td>
            <td>北京</td>
        </tr>
        <tr>
            <td>李四</td>
            <td>25</td>
            <td class="highlight-cell">上海</td>
        </tr>
        <tr>
            <td>王五</td>
            <td>35</td>
            <td>广州</td>
        </tr>
    </tbody>
</table>

<script>
    // 模拟动态添加行,并触发动画
    document.addEventListener('DOMContentLoaded', () => {
        const tbody = document.querySelector('table tbody');
        setTimeout(() => {
            const newRow = document.createElement('tr');
            newRow.innerHTML = `<td>赵六</td><td>28</td><td>深圳</td>`;
            newRow.classList.add('new-row-animation'); // 添加动画类
            tbody.appendChild(newRow);
        }, 1000); // 1秒后添加新行
    });

    // 模拟单元格数据更新,并触发闪烁
    document.addEventListener('DOMContentLoaded', () => {
        const shanghaiCell = document.querySelector('.highlight-cell');
        setTimeout(() => {
            shanghaiCell.textContent = '杭州'; // 假设数据更新
            shanghaiCell.classList.remove('highlight-cell'); // 移除旧的,确保能再次触发
            void shanghaiCell.offsetWidth; // 强制reflow,重置动画
            shanghaiCell.classList.add('highlight-cell'); // 再次添加以触发动画
        }, 3000); // 3秒后更新并闪烁
    });
</script>
登录后复制
/* CSS */
@keyframes fadeOutSlideUp {
    from {
        opacity: 1;
        transform: translateY(0);
        max-height: 50px; /* 假设行高最大值 */
        padding-top: 12px;
        padding-bottom: 12px;
    }
    to {
        opacity: 0;
        transform: translateY(-20px);
        max-height: 0; /* 收缩高度 */
        padding-top: 0;
        padding-bottom: 0;
        overflow: hidden; /* 隐藏内容溢出 */
    }
}
.fade-out-row {
    animation: fadeOutSlideUp 0.6s ease-out forwards; /* forwards让动画停留在最后一帧 */
}
登录后复制
// JavaScript (简化示例)
function deleteRowWithAnimation(rowElement) {
    rowElement.classList.add('fade-out-row'); // 添加动画类

    // 监听动画结束事件,动画结束后移除元素
    rowElement.addEventListener('animationend', () => {
        rowElement.remove();
    }, { once: true }); // { once: true } 确保事件只触发一次
}

// 假设你有一个删除按钮,点击时调用这个函数
// const deleteButton = document.getElementById('some-delete-button');
// deleteButton.addEventListener('click', () => {
//     const targetRow = document.getElementById('row-to-delete');
//     if (targetRow) {
//         deleteRowWithAnimation(targetRow);
//     }
// });
登录后复制
@media (prefers-reduced-motion: reduce) {
    /* 在用户偏好减少动态效果时,禁用或简化动画 */
    tr, td, .new-row-animation, .highlight-cell {
        transition: none !important; /* 禁用所有过渡 */
        animation: none !important; /* 禁用所有动画 */
    }
    /* 或者只保留最基本的视觉提示 */
    tr:hover {
        background-color: #e0f7fa; /* 仅改变背景色,不缩放 */
        transform: none;
        box-shadow: none;
    }
}
登录后复制

以上就是如何为HTML表格添加动画效果?CSS怎么实现?的详细内容,更多请关注php中文网其它相关文章!

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号