VUE3 + CSS实现有趣的单选框示例

本文介绍了一个比传统和UI框架的radio更具特色的选择器,可改造为自定义radio组件,示例中使用了element-ui的分割线和文字提示组件。还提到使用animate css动画库让页面更生动,使用时需添加特定类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        不管是和传统的radiobox还是市面上的UI框架的radio对比。我这个选择器更加显得花里胡哨。能给页面增加不少色彩。你可以稍微改造一下就能变成一个自己的radio组件。示例中使用了element-ui的el-divider分割线组件和el-tooltip文字提示组件。完整代码如下。因为我做了自动按需引入,所以部分依赖需要自己手动引入一下。

各种表情符号等Unicode到这里拿 Unicode 字符百科

<template>
  <div style="width: 500px;">
    <el-divider>当前主题</el-divider>
    <div class="ts-flex-justify--evenly ts-admin-layout--theme">
      <el-tooltip
          effect="dark"
          placement="bottom"
          v-for="(item, index) in themeList"
          :key="index"
          :content="item.title"
      >
        <div
            class="ts-admin-theme--item"
            :class="{'is-active' : current === item.value}"
            :style="{ backgroundColor: item.color }"
            @click="changeTheme(item.value)"
        />
      </el-tooltip>
    </div>
  </div>
</template>

<script lang='ts'>

export default defineComponent({
  setup() {

    const state = reactive({
      current: 1,
      themeList: [
        {
          title: '深色主题',
          value: 1,
          color: '#262626'
        },
        {
          title: '绿色主题',
          value: 2,
          color: '#009688'
        },
        {
          title: '浅色主题',
          value: 3,
          color: '#ffffff'
        },
        {
          title: '灰色模式',
          value: 4,
          color: '#575757'
        },
      ]
    })

    const changeTheme = (value: number) => {
      state.current = value
    }

    return {
      ...toRefs(state),
      changeTheme,
    }
  }
})
</script>

<style lang="scss">
  .ts-flex-justify-between, .ts-flex-justify--evenly {
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    .ts-admin-theme--item {
      width: 25px;
      height: 25px;
      border: 1px solid #c4e8e5;
      border-radius: 50%;
      cursor: pointer;
      position: relative;
      &.active {
         border-color: red;

        &::before {
           content: '\2713';
           font-size: 14px;
           color: #fff;
           position: absolute;
           top: -1px;
           left: 3px;
        }
      }
    }
  }

  .ts-admin-layout--theme {
    .ts-admin-theme--item {
      width: 60px;
      height: 60px;
      border: 5px solid #c4e8e5;
      &.is-active {
        &::before {
          content: '\1F61B';
          font-size: 48px;
          color: #0960BD;
          position: absolute;
          top: -5px;
          left: -3px;
        }
      }
    }
  }
</style>

接下来使用animate css动画库让页面动起来,显得更加的生动👇👇👇👇👇👇

// 安装库
npm install animate.css
// 引入
import 'animate.css'

注意使用animate的时候一定要加上animate__animated类,不然不起作用❗️❗️❗️❗️❗️❗️

<template>
  <div style="width: 500px;">
    <el-divider>当前主题</el-divider>
    <div class="ts-flex-justify--evenly ts-admin-layout--theme animate__animated animate__fadeInDown">
      <el-tooltip
          effect="dark"
          placement="bottom"
          v-for="(item, index) in themeList"
          :key="index"
          :content="item.title"
      >
        <div
            class="ts-admin-theme--item"
            :class="{'is-active' : current === item.value, 'animate__bounceIn' : current == item.value }"
            :style="{ backgroundColor: item.color }"
            @click="changeTheme(item.value)"
        />
      </el-tooltip>
    </div>
  </div>
</template>

我是Etc.End。如果文章对你有所帮助,能否帮我点个免费的赞和收藏😍。

👇 👇 👇 👇 👇 👇 👇 👇 👇 👇 👇 👇

Vue 3 中结合 Ant Design 实现表格穿梭框(Table Switcher),你可以利用 Ant Design 提供的 Table 组件和 Checkbox 组件来创建这个功能。以下是基本步骤: 1. 首先,在你的项目中安装 Ant Design 的依赖: ```bash npm install antd @vue/cli-plugin_ant design ``` 2. 引入必要的组件和样式: ```javascript import { Table, Input, Checkbox } from &#39;ant-design-vue&#39;; import &#39;ant-design-vue/dist/antd.css&#39;; ``` 3. 创建数据结构,例如一个数组,表示需要处理的数据项,每个数据项包含一个 checkbox 属性用于标识是否选中: ```javascript const data = [ { key: &#39;1&#39;, name: &#39;Item 1&#39;, selected: false }, // ...其他数据项... ]; ``` 4. 定义表格组件,并设置列模板: ```javascript <template> <div> <Table :data="data" row-key="key" border :columns={[ { title: &#39;操作&#39;, render: (text, record) => ( <Checkbox v-model="record.selected" /> ), }, //...其他的列配置... ]} /> </div> </template> ``` 5. 在 script 中管理选中的状态,比如在表单提交、切换选择等事件中更新数据: ```javascript <script setup> import { ref } from &#39;vue&#39;; const checkedData = ref([]); function handleSelectChange(row) { if (row.selected) { checkedData.value.push(row); } else { checkedData.value = checkedData.value.filter(item => item.key !== row.key); } } //...其他业务逻辑... </script> ``` 6. 如果需要用户输入搜索条件,可以添加一个输入框配合 Table 的 filter prop 或者使用 Vue 自定义事件来过滤数据。 注意:以上代码示例是一个基础框架,实际应用可能还需要处理更多细节,如状态管理、错误处理、分页等。记得在适当的地方添加适当的事件监听器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wait.End

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值