是否可以在不使用 JavaScript 的情况下在选择更改时为单选按钮设置动画?

问题描述 投票:0回答:1

我正在通过使用样式丰富的单选按钮为应用程序制作一个选择栏,这样我可以确保一次只能选择一个元素,并且还使用方便的样式技巧,因此我必须减少编码。然而,现在我正在尝试为它们设置动画,每当选定的按钮发生变化时,都会稍微放松一下。每当选择一个元素时,我都会更改背景颜色,并且希望它沿线线性移动它,直到到达应具有背景颜色的元素的新位置。仅使用 CSS 和 HTML 可以实现这一点吗?或者我需要添加一些 JavaScript 才能使其工作吗?如果是,具体如何?

我尝试了很多事情,比如在不同的地方添加背景颜色,我尝试了不同的动画方式,但没有任何效果是我喜欢的。我设法获得了一个动画,当我切换按钮时,

form
会折叠,因此在输入更改时触发动画并非不可能。

这是我的代码(我试图让它稍微更通用,这样其他人也可以从中受益:)

<script lang="ts">
    import './example.css';

    export let options: "OPTION1" | "OPTION2" | "OPTION3" = "OPTION1";

    const handleClick = () => {
        const option1 = document.getElementById('option1') as HTMLInputElement;
        const shortBreak = document.getElementById('option2') as HTMLInputElement;
        const longBreak = document.getElementById('option3') as HTMLInputElement;

        if (option1.checked) {
            options = "OPTION1";
        } else if (option2.checked) {
            options = "OPTION2";
        } else if (option3.checked) {
            timerType = "OPTION3";
        }

        console.log(`debug> option selected: ${options}`);
    }
</script>

<div class="options">
    <form>
        <label>
            <input type="radio" id="option1" name="options" checked on:click={handleClick}>
            <span>Option 1</span>
        </label>
        <label>
            <input type="radio" id="option2" name="options" on:click={handleClick}>
            <span>Option 2</span>
        </label>
        <label>
            <input type="radio" id="option3" name="options" on:click={handleClick}>
            <span>Option 3</span>
        </label>
    </form>
</div>
input[type="radio"] {
    display: none;
}

form {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: nowrap;
    overflow: hidden;
    background-color: #242424;
    color: white; /* needed for debugging, otherwise you can't really visualize what happens */
    border-radius: 10px;
    padding-left: 5px;
    padding-right: 5px;
}

label {
    display: flex;
    align-items: center;
    cursor: pointer;
    max-width: 300px;
    white-space: nowrap;
    overflow: hidden; 
    text-overflow: ellipsis;
    border-radius: 10px;
    margin: 0; 
}

form > label:not(:first-child) {
    margin-left: 5px;
}

*, *::before, *::after {
    box-sizing: border-box;
}

input[type="radio"]:checked + span {
    background-color: #747bff;
    padding: 10px;
    border-radius: 10px;
    margin: 0;
}

label span {
    margin-left: 0;
}

PS。我遇到的其他事情是,如果我选择第一个元素,则

form
label
之间存在轻微间隙。如果你们也能帮助我,那就太好了。

javascript html css forms svelte
1个回答
0
投票

这里有一篇文章可以帮助您制作单选按钮和复选框的动画 https://codepen.io/im_vikasdeep/pen/oNxwjZJ

您可以将复选框替换为单选框

* {
    box-shadow: none;
}

body {
    font-family: 'Poppins', sans-serif;
    margin: 0;
    width: 100%;
    height: 100vh;
    background-color: #d1dad3;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 17px;
}

.container {
    max-width: 1000px;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
}

.switch-holder {
    display: flex;
    padding: 10px 20px;
    border-radius: 10px;
    margin-bottom: 30px;
    box-shadow: -8px -8px 15px rgba(255,255,255,.7),
                10px 10px 10px rgba(0,0,0, .3),
                inset 8px 8px 15px rgba(255,255,255,.7),
                inset 10px 10px 10px rgba(0,0,0, .3);
    justify-content: space-between;
    align-items: center;
}

.switch-label {
    width: 150px;
}

.switch-label i {
    margin-right: 5px;
}

.switch-toggle {
    height: 40px;
}

.switch-toggle input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    z-index: -2;
}

.switch-toggle input[type="checkbox"] + label {
    position: relative;
    display: inline-block;
    width: 100px;
    height: 40px;
    border-radius: 20px;
    margin: 0;
    cursor: pointer;
    box-shadow: inset -8px -8px 15px rgba(255,255,255,.6),
                inset 10px 10px 10px rgba(0,0,0, .25);
    
}

.switch-toggle input[type="checkbox"] + label::before {
    position: absolute;
    content: 'OFF';
    font-size: 13px;
    text-align: center;
    line-height: 25px;
    top: 8px;
    left: 8px;
    width: 45px;
    height: 25px;
    border-radius: 20px;
    background-color: #d1dad3;
    box-shadow: -3px -3px 5px rgba(255,255,255,.5),
                3px 3px 5px rgba(0,0,0, .25);
    transition: .3s ease-in-out;
}

.switch-toggle input[type="checkbox"]:checked + label::before {
    left: 50%;
    content: 'ON';
    color: #fff;
    background-color: #0000ff;
    box-shadow: -3px -3px 5px rgba(255,255,255,.5),
                3px 3px 5px #0000ff;
}
<div class="container">
        
        <div class="switch-holder">
            <div class="switch-label">
                <i class="fab fa-bluetooth-b"></i><span>Bluetooth</span>
            </div>
            <div class="switch-toggle">
                <input type="checkbox" id="bluetooth">
                <label for="bluetooth"></label>
            </div>
        </div>

        <div class="switch-holder">
            <div class="switch-label">
                <i class="fas fa-wifi"></i><span>wi-fi</span>
            </div>
            <div class="switch-toggle">
                <input type="checkbox" id="wifi">
                <label for="wifi"></label>
            </div>
        </div>

        <div class="switch-holder">
            <div class="switch-label">
                <i class="fas fa-map-marker-alt"></i></i><span>Location</span>
            </div>
            <div class="switch-toggle">
                <input type="checkbox" id="location">
                <label for="location"></label>
            </div>
        </div>

    </div>

© www.soinside.com 2019 - 2024. All rights reserved.