You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为body元素启用正确的淡入淡出过渡效果?

问题原因

  • 原生CSS的transition属性不支持background-image的过渡动画,直接修改渐变背景无法触发过渡效果,这是渐变没有淡入淡出的核心原因
  • 代码存在多处语法错误:
    • CSS中transition: "all 2.5s";属性值不需要加引号,属于无效写法
    • body标签上的transition="all 2.5s";是无效自定义属性,不会生效
    • JS中数组下标取值逻辑存在越界隐患

修正方案

通过两层伪元素叠加的方式实现渐变过渡:给body添加两个伪元素,分别承载前后两个渐变,通过切换伪元素的透明度实现淡入淡出效果,opacity是transition原生支持的属性,可以正常触发过渡。

修正后HTML代码

<!DOCTYPE html>
<html lang="en" id="htmlMain">
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
    <link href="styles.css" rel="stylesheet">
    <script>
        document.addEventListener('DOMContentLoaded', main);
        function main(){
            var colors = ['#B86B77', '#C0838C', '#CF989F', '#D8ABB1', '#E0BCBF'];
            const head = document.getElementById('header');
            head.style.transition = "all 2.5s";
            const body = document.body;
            let i = 0;
            let isBeforeActive = true;
            // 初始化第一个渐变
            updateGradient(body, 'before', colors[i], colors[colors.length - 1 - i]);
            changeColor();
            function changeColor() {
                setTimeout(change, 3000); // 切换间隔设为3s,留够2.5s的过渡时间
            }
            function change() {
                i = (i + 1) % colors.length;
                const fromColor = colors[i];
                const toColor = colors[colors.length - 1 - i];
                // 切换非激活状态的伪元素的渐变,然后修改透明度实现过渡
                if (isBeforeActive) {
                    updateGradient(body, 'after', fromColor, toColor);
                    body.style.setProperty('--after-opacity', '1');
                } else {
                    updateGradient(body, 'before', fromColor, toColor);
                    body.style.setProperty('--after-opacity', '0');
                }
                isBeforeActive = !isBeforeActive;
                // 标题颜色切换
                head.style.color = toColor;
                if (head.style.color == fromColor) {
                    head.style.color = '#9C626A';
                }
                changeColor();
            }
            function updateGradient(el, pseudo, from, to) {
                const orientation = 'to top';
                el.style.setProperty(`--${pseudo}-gradient`, `linear-gradient(${orientation}, ${from}, ${to})`);
            }
        }
    </script>
    <title>Debjyoti Sikdar</title>
</head>
<body>
    <p style="text-align:center;"> <span id="header">Debjyoti Sikdar</span> </p>
</body>
</html>

修正后CSS代码

@import url('https://fonts.googleapis.com/css2?family=Ubuntu:wght@700&display=swap');
:root {
    --after-opacity: 0;
}
html {
    height: 100%;
}
body {
    transition: all 2.5s;
    height: 100%;
    margin: 0;
    position: relative;
}
body::before, body::after {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    transition: opacity 2.5s ease;
}
body::before {
    background-image: var(--before-gradient);
}
body::after {
    background-image: var(--after-gradient);
    opacity: var(--after-opacity);
}
#header {
    font-family: 'Ubuntu', sans-serif;
    font-size: 50px;
    color: #9C626A;
}

内容的提问来源于stack exchange,提问作者millionleafclover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:45:03