vue3获取验证码+背景切换

需要安装element-plus组件库

用户获取验证码后按钮会被禁用,等待10秒后重新获取验证码

验证码

元素布局template部分

javascript 复制代码
<template>
    <div>
        <el-button type="success" round :disabled="isSend" @click="countDown">
            {{ codeName }}
        </el-button>
    </div>
</template>

script功能部分

javascript 复制代码
<script setup>
import { ref, reactive, watch } from 'vue';
import { ElMessage } from 'element-plus'

const isSend = ref(false); // 禁用
const codeName = ref("发送验证码");
const totalTime = 10; // 10秒倒计时
let timer = null; // 定时器
const code = ref() // 验证码存放处
const col = ref("") // 背景颜色rgb()

const countDown = () => {
    if (isSend.value) return;
    isSend.value = true;
    codeName.value = `${totalTime}s后重新发送`;

    // 生成随机验证码
    const len = 6;
    const codeList = [];
    const chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789";
    const charsLen = chars.length;
    for (let i = 0; i < len; i++) {
        codeList.push(chars.charAt(Math.floor(Math.random() * charsLen)));
    }
    code.value = codeList.join("")
    console.log(codeList.join(""));

    ElMessage({
        message: '验证码提示---' + code.value,
        type: 'success',
    })
    
    // 倒计时
    let currentTime = totalTime;
    timer = setInterval(() => {
        currentTime--;
        codeName.value = `${currentTime}s后重新发送`;
        if (currentTime < 1) {
            clearInterval(timer);
            codeName.value = "重新发送验证码";
            isSend.value = false;
        }
    }, 1000);
};
</script>
  

样式这里没有

随机背景颜色

template部分

javascript 复制代码
<template>
    <div>
        <div class="box">
            <h2>随机切换背景</h2>
        </div>
    </div>
</template>

script部分

javascript 复制代码
<script setup>
// 生成一个随机的颜色
const randomColor = () => {
    const r = Math.floor(Math.random() * (255 - 0) + 0);
    const g = Math.floor(Math.random() * (255 - 0) + 0);
    const b = Math.floor(Math.random() * (255 - 0) + 0);
    return "rgb(" + r + "," + g + "," + b + ")"
}

const changecolor = () => {
    const box = document.querySelector('.box')
    const color = randomColor()
    box.style.backgroundColor = color
}
</script>

style部分

javascript 复制代码
<style scoped>
.box {
    height: 100px;
    width: 200px;
    background-color: bisque;
    text-align: center;
    line-height: 100px;
}
</style>

完整代码:希望能帮助到你

javascript 复制代码
<template>
    <div>
        <el-button type="success" round :disabled="isSend" @click="countDown">
            {{ codeName }}
        </el-button>
        <div class="box">
            <h2>随机切换背景</h2>
        </div>
    </div>
</template>
  
<script setup>
import { ref, reactive, watch } from 'vue';
import { ElMessage } from 'element-plus'

const isSend = ref(false); // 禁用
const codeName = ref("发送验证码");
const totalTime = 10; // 10秒倒计时
let timer = null; // 定时器
const code = ref() // 验证码存放处
const col = ref("") // 背景颜色rgb()

const countDown = () => {
    if (isSend.value) return;
    isSend.value = true;
    codeName.value = `${totalTime}s后重新发送`;

    // 生成随机验证码
    const len = 6;
    const codeList = [];
    const chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789";
    const charsLen = chars.length;
    for (let i = 0; i < len; i++) {
        codeList.push(chars.charAt(Math.floor(Math.random() * charsLen)));
    }
    code.value = codeList.join("")
    console.log(codeList.join(""));

    ElMessage({
        message: '验证码提示---' + code.value,
        type: 'success',
    })

    ElMessage({
        message: '背景颜色提示---' + randomColor(),
        type: 'success',
    })

    changecolor()
    // 倒计时
    let currentTime = totalTime;
    timer = setInterval(() => {
        currentTime--;
        codeName.value = `${currentTime}s后重新发送`;
        if (currentTime < 1) {
            clearInterval(timer);
            codeName.value = "重新发送验证码";
            isSend.value = false;
        }
    }, 1000);
};

// 生成一个随机的颜色
const randomColor = () => {
    const r = Math.floor(Math.random() * (255 - 0) + 0);
    const g = Math.floor(Math.random() * (255 - 0) + 0);
    const b = Math.floor(Math.random() * (255 - 0) + 0);
    return "rgb(" + r + "," + g + "," + b + ")"
}

const changecolor = () => {
    const box = document.querySelector('.box')
    const color = randomColor()
    box.style.backgroundColor = color
}

</script>
  
<style scoped>
.box {
    height: 100px;
    width: 200px;
    background-color: bisque;
    text-align: center;
    line-height: 100px;
}
</style>
  

效果展示:

练习验证码实现

相关推荐
SUPER52662 小时前
FastApi项目启动失败 got an unexpected keyword argument ‘loop_factory‘
java·服务器·前端
sanx182 小时前
专业电竞体育数据与系统解决方案
前端·数据库·apache·数据库开发·时序数据库
你的人类朋友4 小时前
【Node】认识一下Node.js 中的 VM 模块
前端·后端·node.js
Cosolar4 小时前
FunASR 前端语音识别代码解析
前端·面试·github
@大迁世界7 小时前
Vue 设计模式 实战指南
前端·javascript·vue.js·设计模式·ecmascript
芭拉拉小魔仙7 小时前
Vue项目中如何实现表格选中数据的 Excel 导出
前端·vue.js·excel
jump_jump7 小时前
妙用 localeCompare 获取汉字拼音首字母
前端·javascript·浏览器
U.2 SSD7 小时前
Echarts单轴坐标系散点图
前端·javascript·echarts
不做无法实现的梦~8 小时前
jetson刷系统之后没有浏览器--解决办法
开发语言·javascript·ecmascript
德育处主任Pro8 小时前
前端玩转大模型,DeepSeek-R1 蒸馏 Llama 模型的 Bedrock 部署
前端·llama