前端每日实战:5# 视频演示如何用 CSS 创作一个立体滑动 toggle 交互控件

图片描述

效果预览

按下右侧的“点击预览”按钮在当前页面预览,点击链接全屏预览。

https://codepen.io/zhang-ou/pen/zjoOgX

可交互视频教程

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

https://scrimba.com/c/cPvMzTg

源代码下载

请从 github 下载。

https://github.com/comehope/front-end-daily-challenges/tree/master/005-sleek-sliding-toggle-checkbox

代码解读

定义 dom,是嵌套了3层的容器:

<div class="checkbox">
    <div class="inner">
        <div class="toggle"></div>
    </div>
</div>

居中显示:

html, 
body,
.checkbox,
.checkbox .inner,
.checkbox .inner .toggle {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

画出外侧椭圆:

.checkbox {
    width: 10em;
    height: 5em;
    background: linear-gradient(silver, whitesmoke);
    border-radius: 2.5em;
    font-size: 40px;
}

画出内侧椭圆:

.checkbox .inner {
    width: 8em;
    height: 3.5em;
    background: linear-gradient(dimgray, silver);
    border-radius: 2em;
    box-shadow: inset 0 0 1.5em rgba(0, 0, 0, 0.5);
}

画出圆形按钮:

.checkbox .inner .toggle {
    width: 3.5em;
    height: 3.5em;
    background: linear-gradient(to top, silver, whitesmoke);
    border-radius: 50%;
    box-shadow: 0 0.4em 0.6em rgba(0, 0, 0, 0.2);
    position: relative;
    left: -30%;
}

为圆形按钮增加立体效果:

.checkbox .inner .toggle::before {
    content: '';
    position: absolute;
    height: 80%;
    width: 80%;
    background: linear-gradient(whitesmoke, silver);
    border-radius: 50%;
}

在按钮上写上 OFF,行高是根据父元素的高度计算出的:

.checkbox .inner .toggle::before {
    content: 'OFF';
    text-align: center;
    line-height: calc(3.5em * 0.8);
    font-family: sans-serif;
    color: gray;
}

引入jquery:

<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>

编写脚本,在点击按钮时切换样式类:

$(document).ready(function() {
    $('.toggle').click(function() {
        $('.inner').toggleClass('active');
    });
});

设置 active 时控件的样式:

.checkbox .inner.active {
    background: linear-gradient(green, limegreen);
}

.checkbox .inner.active .toggle {
    left: 30%;
}

.checkbox .inner.active .toggle::before {
    content: 'ON';
    color: limegreen;
}

最后,为按钮设置缓动时间,实现动画效果

.checkbox .inner .toggle {
    transition: 0.5s;
}

大功告成!

知识点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的HTML5 + CSS3代码示例,展示了杭州的美食,并使用JavaScript实现了一些交互效果。 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>杭州美食</title> <style> body { font-family: Arial, sans-serif; } header { background-color: #FFC107; padding: 20px; text-align: center; color: #fff; } h1 { margin-top: 0; } section { display: flex; flex-wrap: wrap; justify-content: center; } .card { background-color: #fff; border-radius: 5px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); margin: 20px; width: 300px; height: 400px; overflow: hidden; position: relative; } .card img { width: 100%; height: 200px; object-fit: cover; } .card h2 { margin: 20px; font-size: 24px; color: #333; } .card p { margin: 20px; font-size: 16px; color: #666; height: 120px; overflow: hidden; } .card:hover { transform: translateY(-5px); box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2); } .card:hover img { transform: scale(1.1); } .btn { background-color: #FFC107; color: #fff; padding: 10px 20px; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; position: absolute; bottom: 20px; right: 20px; transition: background-color 0.3s ease; } .btn:hover { background-color: #FF9800; } </style> </head> <body> <header> <h1>杭州美食</h1> </header> <section> <div class="card"> <img src="https://picsum.photos/id/10/300/200" alt="美食1"> <h2>东坡肉</h2> <p>东坡肉是中国浙江杭州的一道名菜。以猪肉为主料,红烧而成。它色泽红润,肥而不腻,肉质细嫩,香而不腥,入口后回味无穷。</p> <button class="btn" onclick="alert('这是东坡肉')">了解更多</button> </div> <div class="card"> <img src="https://picsum.photos/id/20/300/200" alt="美食2"> <h2>龙井虾仁</h2> <p>龙井虾仁是杭州传统的一道名菜,选用新鲜的虾仁和杭州特产的西湖龙井茶叶,烹饪过程中需要掌握火候和时间,口感鲜美。</p> <button class="btn" onclick="alert('这是龙井虾仁')">了解更多</button> </div> <div class="card"> <img src="https://picsum.photos/id/30/300/200" alt="美食3"> <h2>叫化鸡</h2> <p>叫化鸡是杭州的一道传统名菜,选用优质鸡肉,烹制而成。皮脆肉嫩,口感鲜美,是杭州的一道必尝美食。</p> <button class="btn" onclick="alert('这是叫化鸡')">了解更多</button> </div> </section> <script> const cards = document.querySelectorAll('.card'); cards.forEach(card => { card.addEventListener('click', () => { card.classList.toggle('active'); }); }); </script> </body> </html> ``` 这段代码会生成一个简单的网页,其中包含了三个美食卡片,当你点击卡片时,会出现卡片的详细信息。你可以根据自己的需求修改和调整这段代码,以适应你的项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值