CSS——js 动态改变原生 radio、switch 的选中样式

news2024/9/25 8:29:39

导航

  • 1. radio
    • 1-1. 业务场景:
    • 1-2. 效果:
    • 1-3. 问题点:
    • 1-4. 解决方案:
    • 1-5. 代码:
      • 1-5-1. HTML
      • 1-5-2. JS
      • 1-5-3. html 内容排版的 css
      • 1-5-4. 实现 radio 效果的 css
  • 2. switch
    • 2-1. 业务场景:
    • 2-2. 效果:
    • 2-3. 问题点:
    • 2-4. 解决方案:
    • 2-5. 代码:
      • 2-5-1. HTML
      • 2-5-2. JS
      • 2-5-3. html 内容排版的 css
      • 2-5-4. 实现 switch 效果的 css
  • 3. 公共文件
    • 3-1. 基础 css

本文代码齐全,可直接复制各版块代码,略作修改即可生成 demo。

1. radio

1-1. 业务场景:

  • 当某用户满足条件 A 时,方式一 与 方式二 可任意选择,即点击哪个高亮哪个。
  • 当不满足条件 A 时,只可选择方式一,当选择方式二时,弹出提示,且选中的情况不发生变化。

1-2. 效果:

在这里插入图片描述

1-3. 问题点:

当直接使用 input 的 checked 属性时,直接点击 input 控件,被点击的就高亮了,且该控件的高亮不受 checked 控制,即使将该 input 的值设置为空,被点击控件的高亮并不消失。

1-4. 解决方案:

不使用 input 的 checked 属性,直接使用 class,通过 js 改变 class 达到 radio 的动态高亮效果。

1-5. 代码:

1-5-1. HTML

<div class="fm-radio-item">
  <label class="item-label require">签约方式</label>
  <div class="radio-group">
    <span (click)="changeRadio(1)">
      <input
        type="radio"
        id="contractMode1"
        [(ngModal)]="shopForm.contractMode"
        [class]="
          shopForm.contractMode === 1
            ? 'radio-component-checked'
            : 'radio-component'
        "
      />
      <label
        for="contractMode1"
        class="radio-label"
        (click)="forbidden($event)"
        >方式一</label
      >
    </span>
    <span (click)="changeRadio(2)">
      <input
        type="radio"
        id="contractMode2"
        [(ngModal)]="shopForm.contractMode"
        [class]="
          shopForm.contractMode === 2
            ? 'radio-component-checked'
            : 'radio-component'
        "
      />
      <label for="contractMode2" (click)="forbidden($event)">方式二</label>
    </span>
  </div>
</div>

1-5-2. JS

changeRadio = (value: number) => {
  if (value === 2) {
  	alert('条件不满足,方式二不可选');
    return;
  }
  this.shopForm[type] = value;
};
// 不阻止的话会触发2次
forbidden = (e: Event) => {
  e.preventDefault();
};

1-5-3. html 内容排版的 css

@import "./base";
@import "./radio-component-checked";

$label-width: 160px;

.fm-radio-item {
  padding: 20px;
  display: flex;
  align-items: center;
  position: relative;
  .item-label {
    display: inline-block;
    width: $label-width;
    line-height: 64px;
    font-size: $f-s-default;
    &.require {
      @include requiredAfter();
    }
  }
  .radio-group {
    flex: 1;
    width: 100%;
    padding: 11px 0;
    line-height: 40px;
    font-size: $f-s-default-max;
    .radio-label {
      margin-right: 40px;
    }
  }
}

1-5-4. 实现 radio 效果的 css

/* radio-component-checked.css */
@import "./base";
.radio-component {
  width: 32px;
  height: 32px;
  position: relative;
  top: -4px;
  margin-right: 10px;
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
  padding: 0;
  /* 按钮 */
  &::before {
    content: "";
    width: 32px;
    height: 32px;
    border: 1px solid #7d7d7d;
    display: inline-block;
    border-radius: 50%;
    vertical-align: middle;
  }
}

.radio-component-checked {
  width: 32px;
  height: 32px;
  position: relative;
  top: -4px;
  margin-right: 10px;
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
  padding: 0;
  /* 按钮 */
  &::before {
    content: "";
    width: 32px;
    height: 32px;
    border: 2px solid $color-primary;
    display: inline-block;
    border-radius: 50%;
    vertical-align: middle;
  }
  &::after {
    content: "";
    width: 18px;
    height: 18px;
    background-color: $color-primary;
    border-radius: 50%;
    text-align: center;
    display: block;
    position: absolute;
    top: 13px;
    left: 7px;
    vertical-align: middle;
  }
}

2. switch

2-1. 业务场景:

切换营业状态,在『开』状态时高亮。

2-2. 效果:

在这里插入图片描述
在这里插入图片描述

2-3. 问题点:

原生 input 没有 switch 属性。Switch 组件只在第三方组件中存在,如:elementUI、antd 等。

2-4. 解决方案:

使用原生开发,css 需改 input=checkbox 的基本样式为滑动按钮,js 动态改变 class 属性做到高亮的变化效果。

2-5. 代码:

2-5-1. HTML

<div class="fm-item">
  <label class="item-label require">营业状态</label>
  <div class="btn-group-con">
    <div class="radio-group">
      <input
        type="checkbox"
        (change)="changeStatus('shopStatus')"
        [ngClass]="
          !!timeForm.shopStatus
            ? 'switch-component-checked'
            : 'switch-component'
        "
      />
      <span class="tip">{{
        timeForm.shopStatus ? "营业中" : "打烊中"
      }}</span>
    </div>
  </div>
</div>

2-5-2. JS

changeStatus = (type: string) => {
  this.timeForm[type] = this.timeForm[type] === 1 ? 0 : 1;
};

2-5-3. html 内容排版的 css

.fm-item {
  display: flex;
  align-items: center;
  position: relative;
  padding: 15px 30px;
  background-color: $bg-color-main;
  border-radius: 10px;
  .item-label {
    display: inline-block;
    width: $fm-item-label-width;
    line-height: 64px;
    font-size: $f-s-default;
    &.require {
      @include requiredAfter();
    }
  }
  .radio-group {
    flex: 1;
    width: 100%;
    padding: 11px 0;
    line-height: 40px;
    font-size: $f-s-default-max;
    .tip {
      font-size: $f-s-default-min;
      color: $text-color-lightGray;
      margin-left: 20px;
    }
  }
}

2-5-4. 实现 switch 效果的 css

/* switch-component-checked */
@import "./base";
.switch-component {
  width: 80px;
  height: 40px;
  position: relative;
  top: 10px;
  background-color: $text-color-lightGray;
  border-radius: 30px;
  border: none;
  outline: none;
  -webkit-appearance: none;
  transition: all 0.2s ease;
  /* 按钮 */
  &::after {
    content: "";
    position: absolute;
    top: 0;
    left: 1px;
    width: 50%;
    height: 100%;
    background-color: $bg-color-main;
    border-radius: 50%;
    transition: all 0.2s ease;
  }
}

.switch-component-checked {
  width: 80px;
  height: 40px;
  position: relative;
  top: 10px;
  background-color: $color-primary;
  border-radius: 30px;
  border: none;
  outline: none;
  -webkit-appearance: none;
  transition: all 0.2s ease;
  /* 按钮 */
  &::after {
    content: "";
    position: absolute;
    top: 0;
    left: 49%;
    width: 50%;
    height: 100%;
    background-color: $bg-color-main;
    border-radius: 50%;
    transition: all 0.2s ease;
  }
}

3. 公共文件

3-1. 基础 css

// 基础类型颜色
$color-primary: #1890ff;
$color-success: #52c41a;
$color-warning: #faad14;
$color-danger: #ff4d4f;
$color-purple: rgb(117, 117, 255);

// 文字颜色
$text-color-placeholder: #cccccc;
$text-color-lightGray: #999999;
$text-color-gray: #666666;
$text-color-darkGray: #333333;
$text-color-default: rgba(0, 0, 0, 0.85);
$text-color-white: #ffffff;

// 字体大小
$f-s-default-max: 32px;
$f-s-default-big: 30px;
$f-s-default: 28px;
$f-s-default-small: 26px;
$f-s-default-min: 24px;

// 行高
$l-h-default-max: 38px;
$l-h-default-big: 36px;
$l-h-default: 34px;
$l-h-default-small: 32px;
$l-h-default-min: 30px;

// 背景色
$bg-color-default: rgb(240, 240, 245);
$bg-color-main: rgb(255, 255, 255);

// border 线
$border-lightGray: 1px solid #cccccc;
/**
 * 文字后的星号(必填标识)
 */
@mixin requiredAfter($left: 10px) {
  &::after {
    content: "*";
    color: $color-danger;
    position: relative;
    left: $left;
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/439469.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Vue3+Typescript+Vitest单元测试环境+组件事件测试篇

上一节我们学会了组件测试的基础测试部分组件测试基础篇&#xff0c;这一节&#xff0c;我们学习一下深入测试组件的事件 在component中增加一个新的组件,名字就叫做Zmbutton2吧 import { defineComponent } from "vue";const ZmButton2 defineComponent({name: &…

《Spring MVC》 第一章 MVC模式

前言 MVC 模式&#xff0c;全称为 Model-View-Controller&#xff08;模型-视图-控制器&#xff09;模式。是一种软件架构模式。 分层描述Model&#xff08;模型&#xff09;它是应用程序的主体部分&#xff0c;主要由以下 2 部分组成&#xff1a; 实体类 Bean&#xff1a;专…

(4.7-4.13)【大数据新闻速递】上海、广州、青海、贵阳大力发展大数据产业;2026年中国大数据市场规模预计达365亿美元

01【贵阳大数据交易所发布全国首个交易激励计划】 4月6日&#xff0c;贵阳大数据交易所发布了“交易激励计划”&#xff0c;旨在提高数据场内交易的吸引力&#xff0c;解决交易入场难的问题。该计划是落实《关于构建数据基础制度更好发挥数据要素作用的意见》和《关于支持贵州…

阿里云服务器网络收发包PPS性能25万/80万/100万PPS详解

阿里云服务器ECS网络收发包PPS是什么&#xff1f;云服务器PPS多少合适&#xff1f;网络收发包PPS是指云服务器每秒可以处理的网络数据包数量&#xff0c;单位是PPS即packets per second每秒发包数量。阿里云百科来详细说下阿里云服务器网络收发包PPS性能参数表&#xff0c;以及…

Vue 事件处理器

文章目录 Vue 事件处理器事件修饰符按键修饰符 Vue 事件处理器 事件监听可以使用 v-on 指令&#xff1a; v-on <div id"app"><button v-on:click"counter 1">增加 1</button><p>这个按钮被点击了 {{ counter }} 次。</p>…

入门神经网络——浅层神经网络

文章目录 一、基础知识1.浅层神经网络介绍2.浅层神经网络的正向传播3.反向传播 二、浅层神经网络代码实例 一、基础知识 1.浅层神经网络介绍 此次构件浅层神经网络&#xff0c;相比于单神经元&#xff0c;浅层神经网络拥有多个神经元&#xff0c;因此又可以称为多神经元网络&…

【Python杂事】 处理Excel中日期时间之间没有空格问题

目录 一、数据源要求二、逻辑思路1、ROI区域2、对ROI区域进行判定 三、代码实现四、成果展示总结 碰到一个非常有趣的问题&#xff1a; 在我们的日常工作中&#xff0c;常常需要处理各种各样的数据&#xff0c;其中不乏包含日期和时间信息的数据。有时候我们会发现这些数据中日…

事件抽取的概念

1. 事件的定义 事件是指发生在某个特定的时间点或时间段、某个特定的地域范围内&#xff0c;由一个或者多个角色参与的一个或者多个动作组成的事情或状态的改变。 2. 事件基本元素 时间、地点、人物、原因、结果 3.事件抽取的定义 主要研究如何从描述事件信息的文本中抽取…

python共词矩阵分析结果一步到位

import os import re import pandas as pd from PyPDF2 import PdfFileReader import string import yakeif __name__ __main__:# 运行第一部分代码pdf_files_path C:/Users/win10/Documents/美国智库/pdf_files# 定义一个函数&#xff0c;用于读取PDF文件并将其转化成文本de…

C++学习之交互式表达式求值的详细讲解和简单代码示例

一、怎么理解交互式表达式求值&#xff1a; 交互式求表达式值是指通过命令行交互的方式&#xff0c;接收用户输入的数值和运算符号&#xff0c;然后计算出其表达式的值并返回给用户。这种方式可以方便地进行简单的计算和查错&#xff0c;也可以用于程序设计中一系列复杂的计算…

单CDN与融合CDN之间的对比:优缺点及注意事项

CDN是现代互联网服务的重要组成部分&#xff0c;它CDN可帮助内容提供者高速交付内容&#xff0c;不同的服务器部署在全球不同的数据中心&#xff0c;并在它们之间共享相同的网络路径。随着企业意识到CDN的重要性&#xff0c;越来越多的企业正在使用CDN作为内容分发工具。互联网…

完美解决丨RuntimeError: create_session() called before __init__().

错误&#xff1a; import sys sys.path.append(/home/pi/ssd-detect) import ssd_detect ssd_detect.detect(/home/pi/ssd-detect/test.jpg) 报错如下&#xff1a; Traceback (most recent call last): File "test.py", line 6, in <module ssd_detect.detect(/ho…

ArcGIS制作建设项目使用林地现状图

近年来&#xff0c;随着经济社会的快速发展&#xff0c;各地建设项目不断增多&#xff0c;占用征用林地项目的数量也呈逐年上升的趋势。根据《占用征用林地审核审批管理规范》规定&#xff0c;建设项目申请占用征用林地&#xff0c;应编制的项目使用林地可行性报告&#xff1b;…

Linux-升级CMake版本(Ubuntu18.4)

一、简介 在一些场景中&#xff0c;因为CMake版本过低而无法编译&#xff0c;此时就需要升级CMake的版本。 二、升级 卸载 先卸载旧的cmake sudo apt-get autoremove cmake 安装 切换文件夹 cd /usr/src 下载cmake包。需要哪个版本&#xff0c;可以自行修改版本号。 注&#…

中国人工智能公司CIMCAI集装箱识别云服务全球用户量领先核心科技领先,免费人工智能集装箱识别云服务,智能化港航中国人工智能公司

中国人工智能公司CIMCAI集装箱识别云服务全球用户量领先核心科技领先&#xff0c;免费人工智能集装箱识别云服务&#xff0c;智能化港航智慧港航.全球领先CIMCAI ENGINE集装箱AI检测云服务&#xff0c;集装箱号识别率99.98%&#xff0c;全球超4000企业用户使用&#xff0c;集装…

npm 包本地调试(详细流程:包本地路径、npm link 、yalc)

一、使用 包本地路径安装 package.json 中的包名叫 dzmtest&#xff08;这个才是重点&#xff09;&#xff0c;包的文件夹名 test。 获取到包的文件夹路径 执行安装命令&#xff0c;然后提示安装成功 $ npm install 包文件夹路径# 例如&#xff1a; $ npm install /Users/d…

【Linux】Linux第一个小程序-进度条

前言&#xff1a; 在之前的学习中&#xff0c;我们已经基本掌握了关于了 Linux 下的一些工具的使用&#xff0c;接下来我们运用之前学到的知识&#xff0c;我将带领大家写了一个关于 进度条 的小程序来练练手&#xff01;&#xff01;&#xff01; 本文目录 &#xff08;一&am…

密码模块非入侵式攻击及其缓解技术GMT0083-2020

密码模块非入侵式攻击&#xff0c;是一种攻击方式&#xff0c;在这种攻击中&#xff0c;攻击者不会直接访问受攻击者的密码模块。相反&#xff0c;攻击者会利用其他途径获得用户的凭证&#xff0c;然后使用这些凭证去访问密码模块。这种攻击通常使用钓鱼邮件、社交工程等技巧&a…

户外软件推荐与介绍——奥维和两步路

文章目录 &#xff08;一&#xff09;记录坐标的网站1、奥维互动地图注册2、记录坐标3、如何导出坐标点到ArcGIS4、导航5、记录轨迹6、下载离线地图 &#xff08;二&#xff09;记录轨迹的软件&#xff08;三&#xff09;离线地图的下载&#xff08;四&#xff09;如何使用3D地…

Chapter5-消息队列的核心机制

Broker 是 RocketMQ 的核心&#xff0c;大部分‘重量级”工作都是由 Broker 完成的&#xff0c;包括接收 Producer 发过来的消息、处理 Consumer 的消费消息请求、&#xff0d;消息的持久化存储、消息的 HA 机制以及服务端过滤功能等。 5.1 消息存储和发送 分布式队列因为有高…