[100天挑战100个前端效果]第二十一天---switch切换按钮

news/2024/7/19 14:44:54 标签: js, css, javascript, html, html5

switch切换按钮

  • 让我们先来看看实现的效果
    • html(包含html" title=js>js)代码
    • html" title=css>css代码
  • 设计思路与今日份知识总结
    • 设计思路
    • 今日份知识总结

让我们先来看看实现的效果

在这里插入图片描述

htmlhtml" title=js>js_3">html(包含html" title=js>js)代码

html"><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.html" title=css>css">
    <title>第二十一天</title>
</head>
<body>
    <div class="channel">
        switch切换按钮啦!
    </div>
    <div class="container">
        <div id="switch" >
            <div id="circle"></div>
        </div>
    </div>
    <script>html" title=javascript>javascript">
        let swch=document.getElementById('switch');
        swch.addEventListener('click',(e)=>{
            // swch.classList.toggle('switched');
            if(swch.classList.contains('switched')){
                swch.classList.remove('switched');
            } else{
                swch.classList.add('switched');
            }
        });
    </script>
</body>
</html>

html" title=css>css_39">html" title=css>css代码

html" title=css>css">:root {
    --background-color: #2c3e50;
    --border-color: #7591AD;
    --text-color: #34495e;
    --color1: #EC3E27;
    --color2: #fd79a8;
    --color3: #0984e3;
    --color4: #00b894;
    --color5: #fdcb6e;
    --color6: #e056fd;
    --color7: #F97F51;
    --color8: #BDC581;
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html{
    font-size: 14px;
}
body{
    width: 100vw;
    height: 100vh;
    background-color: var(--background-color);
    display: flex;
    justify-content: center;
    align-items: center;
    font-family:  'Montserrat', sans-serif, Arial, 'Microsoft Yahei';
}
.channel{
    position: absolute;
    width: 80%;
    text-align: center;
    top: 10vh;
    left: 50%;
    transform: translateX(-50%);
    font-size: 5vmin;
    font-weight:bold ;
    color: #353535;
    z-index: 10000;
}
.container{
    width: 100vw;
    height: 100vh;
    /* background-color: #00b894; */
    display: flex;
    justify-content: center;
    align-items: center;
}
#switch{
    position: absolute;
    width: 60px;
    height: 30px;       
    border: 2px solid #333;     
    border-radius: 30px;          
    cursor: pointer;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
}

#switch::before,
#switch::after{
    position: absolute;
    line-height: 26px;
    text-transform: uppercase;
    padding: 0 20px;
    cursor: initial; 
    pointer-events: none;
}
#switch::before{
    content: 'slow';
    right: 90%;
}

#switch::after{
    content: 'fast';
    left: 90%;
}

#switch #circle{
    position: absolute;
    top: 3px;
    left: 3px;
    width: 20px;
    height: 20px;
    border: 2px solid #333;
    border-radius: 50%;
    box-shadow: inset 4px -2px 0 0 var(--color8);
    transition: all .5s cubic-bezier(.74,1.28,.73,.99);
}

#switch.switched #circle{
    left: calc(100% - 20px - 3px);
    box-shadow: inset 4px -2px 0 0 var(--color3);
}

设计思路与今日份知识总结

设计思路

我们为什么不用checkbox呢?因为不好看呗!

  • 在switch块先画一个圆框
  • 在circle画一个圆
  • 然后用html" title=js>js控制switch的classList,通过给switch添加点击响应,来改变switch的样式
  • 两个样式,一个左边,一个右边就成了
  • 细节的改变看下面的知识总结把!

今日份知识总结

有些知识前面多次出现的就不写了哦!

标签作用
classList.contains看当前元素的class是否包含
classList.remove移除当前元素的某个class属性
classList.add给当前元素添加某个class属性
box-sizingbox-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素。
cursor定义鼠标的样式
pointer-events禁止鼠标事件
line-heightline-height 属性设置行间的距离(行高)
cubic-beziercubic-bezier() 函数定义了一个贝塞尔曲线(Cubic Bezier)。用来控制变化速度

cubic-bezier计算工具


http://www.niftyadmin.cn/n/1847173.html

相关文章

Suzuki EN3F 参数

转载于:https://www.cnblogs.com/fatt/p/4401667.html

UVA - 796 Critical Links (无向图桥模板+两种实现)

链接&#xff1a;https://cn.vjudge.net/problem/UVA-796 题意&#xff1a;求无向图桥的个数以及按字典序输出桥。 思路&#xff1a;只需判断u的孩子节点的lowv>u。 #include <bits/stdc.h> #include <unordered_map> #define ll long long using namespace…

javascript基础——属性操作

HTML 的属性操作&#xff1a;读、写 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>JS属性操作</title><script>/*HTML 的属性操作&#xff1a;读、写属性名&#xff1a;属性值&#xff…

[JavaScript能力测评]9.查找重复元素(三种方法!)

JS能力测评第九题题目如下indexof和lastindexof数组排序&#xff0c;当前元素和后一个元素比较filterindexlastindexof题目如下 找出书中arr中重复出现过的元素 indexof和lastindexof function duplicates(arr) {var newArr [];arr.forEach((el, index)>{if(newArr.inde…

Linux(Ubuntu)下MySQL的安装与配置

2019独角兽企业重金招聘Python工程师标准>>> 在Linux下MySQL的安装&#xff0c;我一直觉得挺麻烦的&#xff0c;因为之前安装时就是由于复杂的配置导致有点晕。今天&#xff0c;需要在Linux下用Qt连接MySQL。遂安装配置了一把。 1&#xff09;首先检查系统中是否已经…

stringstream流用法

参考博客&#xff1a;https://blog.csdn.net/xw20084898/article/details/21939811 題目&#xff1a;输入的第一行有一个数字 N 代表接下來有 N 行資料&#xff0c;每一行資料里有不固定個數的整數(最多 20 個&#xff0c;每行最大 200 個字元)&#xff0c;請你寫一個程式將每…

css学习心得-为样式找到应用目标

2019独角兽企业重金招聘Python工程师标准>>> 常用选择器 # 类型选择器 # 后代选择器 伪类 :link :visited 链接伪类 &#xff0c;只能用于锚元素 :hover :active :focus 动态伪类&#xff0c;可以应用于任何元素 ps: ie6 忽略掉 :focus伪类。ie7任何元素都支持:hov…

pip install 拒绝访问如何处置?

pip install 拒绝访问如何处置&#xff1f;让我们先看看问题&#xff01;解决办法让我们先看看问题&#xff01; 今天博主想下载一个包&#xff0c;rasa_nlu&#xff0c;但是遇到了上面的报错&#xff01; pip install rasa_nlu解决办法 在代码的后面加上–user即可 pip ins…