You need to enable JavaScript to run this app. 如何为复选框触发的隐藏元素可见时添加淡入、缩放类动画效果
优惠活动
大模型
产品
解决方案
定价
更多

如何为复选框触发的隐藏元素可见时添加淡入、缩放类动画效果

家具选型展示项目动画新增实现方案

当前项目实现现状

做家具选型展示页面时,原有交互逻辑为:用户点击复选框选中对应家具(第一项为椅子、第二项为桌子),页面右侧区域同步显示对应家具元素。目前显隐控制是通过点击事件直接修改元素visibility属性完成的,现需要在现有交互基础上,为元素从隐藏到可见的过程添加淡入、缩放类过渡动画,梳理可落地的实现方案与优化方向。

项目现有代码

原有CSS代码

ul {
    list-style-type: none;
}

li {
    display: inline-block;
}

input[type="checkbox"][id^="cb"] {
    display: none;
}

label {
    border: 1px solid #fff;
    padding: 10px;
    display: block;
    position: relative;
    margin: 10px;
    cursor: pointer;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.container {
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    color: #707070;
    z-index: 12;
}

.container h1 {
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    color: #707070;
    font-size: 25px;
}

.container p {
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    color: #707070;
    font-size: 18px;
}

input:focus,
select:focus,
textarea:focus,
button:focus {
    outline: none;
}

.button {
    background-color: #4b4b4b;
    border: none;
    color: white;
    padding: 12px 62px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 14px;
    margin: 4px 2px;
    transition-duration: 0.4s;
    cursor: pointer;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}

.button1 {
    background-color: white;
    color: black;
    border: 1px solid #4b4b4b;
    border-radius: 21px;
}

.button1:hover {
    background-color: #757575;
    color: white;
}

label::before {
    background-color: white;
    color: white;
    content: " ";
    display: block;
    border-radius: 50%;
    border: 1px solid grey;
    position: absolute;
    top: -5px;
    left: -5px;
    width: 25px;
    height: 25px;
    text-align: center;
    line-height: 28px;
    transition-duration: 0.4s;
    transform: scale(0);
}

label img {
    height: 150px;
    width: 150px;
    transition-duration: 0.2s;
    transform-origin: 50% 50%;
}

:checked+label {
    border-color: #ddd;
}

:checked+label::before {
    content: "✓";
    background-color: grey;
    transform: scale(1);
}

:checked+label img {
    transform: scale(0.8);
    z-index: -1;
}

.render {
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    object-fit: cover;
    z-index: -2;
    left: 0;
    bottom: 0;
}

.render2 {
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    object-fit: cover;
    z-index: -2;
    left: 0;
    bottom: 0;
}

.render3 {
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    object-fit: cover;
    z-index: -2;
    left: 0;
    bottom: 0;
}

#render,
#render2,
#render3,
#render4 {
    visibility: hidden;
}

原有HTML代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="./style.css" />
    <link rel="stylesheet" href="./style.js" />
    <title>Dc Works Demo</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(function () {
        var url = "https://vigilant-allen-a37d54.netlify.app/";
        var choice = [];
        $("input:button[value='EVİNİZİ 3D-AR İLE GÖRÜNTÜLEYİN']").click(function () {
            $("input:checkbox[name^='choice']:checked").each(function (i, el) {
                choice += "choice=" + $(el).attr('name') + "&";
            });
            choice = choice.slice(0, -1);
            $('form').attr("action", url + choice).submit();
        });
    });
    </script>
    <style>
    body, html {
        height: 100%;
    }

    body {
        background-image: url("bosevkucuk.png");
        height: 100%;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
    }
    </style>
</head>
<body>
    <br>
    <div class="container">
        <ul>
            <P>DC WORKS DEMO <BR> 3D GÖRÜNTÜLEME <BR>ARTIRILMIŞ GERÇEKLİK</P>
            <br>
            <h1>Evinde İstediğin Eşyaları Seç</h1>
            <br><br>
            <form name="search_something" action="" method="get">
                <li>
                    <input onclick="myFunction()" type="checkbox" name="choice1" id="cb1" />
                    <label for="cb1"><img src="sandalye3-01.png" /></label>
                    <center><p>Gri Tekli Sandalye</p></center>
                </li>
                <li>
                    <input onclick="myFunction2()" type="checkbox" name="choice2" id="cb2" />
                    <label for="cb2"><img src="masa3-01.png" /> </label>
                    <center><p>Köşe masa</p></center>
                </li>
                <li>
                    <input onclick="myFunction3()" type="checkbox" name="choice3" id="cb3" />
                    <label for="cb3"><img src="hali3-01.png" /></label>
                    <center><p>El İşlemesi Halı</p></center>
                </li>
                <br><br><br><br>
                <input class="button button1" type="button" value="EVİNİZİ 3D-AR İLE GÖRÜNTÜLEYİN">
            </form>
        </ul>
    </div>
    <br><br>
    <center>
        <div>
            <img class="render" id="render" src="sandalyeson.png" />
            <img class="render2" id="render2" src="masason.png" />
            <img class="render3" id="render3" src="hali.png" />
        </div>
    </center>
    <script>
        function myFunction() {
            var x = document.getElementById('render');
            if (x.style.visibility === 'visible') {
                x.style.visibility = 'hidden';
            } else {
                x.style.visibility = 'visible';
            }
        }
    </script>
    <script>
        function myFunction2() {
            var x = document.getElementById('render2');
            if (x.style.visibility === 'visible') {
                x.style.visibility = 'hidden';
            } else {
                x.style.visibility = 'visible';
            }
        }
    </script>
    <script>
        function myFunction3() {
            var x = document.getElementById('render3');
            if (x.style.visibility === 'visible') {
                x.style.visibility = 'hidden';
            } else {
                x.style.visibility = 'visible';
            }
        }
    </script>
</body>
</html>

动画实现改造方案

完全不需要改动原有复选框的事件绑定逻辑,只需要基于现有visibility的切换规则做属性扩展即可实现平滑动画。

  • 第一步:修改家具元素的基础CSS,添加过渡规则与动画状态
    把原本控制隐藏状态的CSS替换为以下内容,同时覆盖隐藏、可见两种状态的动画属性:
    #render,
    #render2,
    #render3,
    #render4 {
        visibility: hidden;
        /* 对透明度、缩放比例、可见性设置统一过渡规则 */
        transition: opacity 0.5s ease, transform 0.5s ease, visibility 0.5s;
        opacity: 0; /* 隐藏状态下元素完全透明 */
        transform: scale(0.85); /* 隐藏状态下缩小15% */
        transform-origin: bottom center; /* 以底部中心为缩放原点,匹配家具落地的视觉逻辑 */
    }
    
    /* 元素可见时的状态样式 */
    #render.visible,
    #render2.visible,
    #render3.visible,
    #render4.visible {
        visibility: visible;
        opacity: 1; /* 可见状态下完全不透明 */
        transform: scale(1); /* 恢复元素原始尺寸 */
    }
    
  • 第二步:最小化修改原有JS交互逻辑
    不需要重写所有点击事件,只需要把原本直接修改行内visibility属性的代码,替换为给元素切换visible类即可:
    function myFunction() {
        document.getElementById('render').classList.toggle('visible');
    }
    
    function myFunction2() {
        document.getElementById('render2').classList.toggle('visible');
    }
    
    function myFunction3() {
        document.getElementById('render3').classList.toggle('visible');
    }
    
  • 可选优化方向:
    • 保留visibility属性参与过渡是实现无突兀感动画的关键:visibility从可见切为隐藏时会按照设置的过渡时长延迟生效,能保证退出动画完整播放;从隐藏切为可见时是即时生效,能保证进入动画第一时间触发,不会出现延迟卡顿。
    • 可以根据视觉偏好调整动画参数:比如把过渡时长改成0.3秒、缩放初始值改成0.95,会得到更轻快的展示效果;也可以给transform加上轻微的上移效果,让动画更有层次感。
    • 现有代码中重复定义的.render/.render2/.render3类样式可以合并为一个通用类,减少CSS冗余代码。

内容的提问来源于stack exchange,提问作者Sercan Demirtaş

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.14 16:15:03