当前位置: 首页 > news >正文

vue弹出是否继续操作

Vue确认操作弹窗设计

我将设计一个优雅的Vue确认操作弹窗,让用户在执行重要操作前进行确认。

设计思路

  • 创建一个可复用的确认弹窗组件

  • 支持自定义标题、内容和按钮文本

  • 使用Promise处理异步确认/取消操作

  • 添加动画效果提升用户体验

  • 响应式设计确保在各种设备上正常显示

实现方案

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Vue确认操作弹窗</title><script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script><style>* {margin: 0;padding: 0;box-sizing: border-box;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}body {background-color: #f5f7fa;padding: 20px;color: #333;line-height: 1.6;}.container {max-width: 1000px;margin: 0 auto;padding: 30px;}header {text-align: center;margin-bottom: 40px;}h1 {color: #2c3e50;margin-bottom: 10px;font-size: 2.5rem;}.subtitle {color: #7f8c8d;font-size: 1.2rem;}.demo-section {background: white;border-radius: 12px;padding: 30px;box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);margin-bottom: 30px;}h2 {color: #2c3e50;margin-bottom: 20px;padding-bottom: 10px;border-bottom: 1px solid #eee;}.button-group {display: flex;flex-wrap: wrap;gap: 15px;margin: 20px 0;}button {padding: 12px 24px;border: none;border-radius: 6px;cursor: pointer;font-weight: 600;transition: all 0.3s ease;font-size: 16px;}.btn-primary {background-color: #3498db;color: white;}.btn-primary:hover {background-color: #2980b9;transform: translateY(-2px);}.btn-danger {background-color: #e74c3c;color: white;}.btn-danger:hover {background-color: #c0392b;transform: translateY(-2px);}.btn-warning {background-color: #f39c12;color: white;}.btn-warning:hover {background-color: #d35400;transform: translateY(-2px);}.btn-success {background-color: #2ecc71;color: white;}.btn-success:hover {background-color: #27ae60;transform: translateY(-2px);}.result {margin-top: 20px;padding: 15px;background-color: #f8f9fa;border-radius: 6px;border-left: 4px solid #3498db;}/* 弹窗样式 */.modal-overlay {position: fixed;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.5);display: flex;justify-content: center;align-items: center;z-index: 1000;opacity: 0;animation: fadeIn 0.3s forwards;}.modal-content {background: white;border-radius: 10px;width: 90%;max-width: 500px;box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);transform: translateY(-20px);animation: slideUp 0.3s forwards;overflow: hidden;}.modal-header {padding: 20px 25px;border-bottom: 1px solid #eee;display: flex;justify-content: space-between;align-items: center;}.modal-title {font-size: 1.4rem;font-weight: 600;color: #2c3e50;}.modal-close {background: none;border: none;font-size: 1.5rem;color: #7f8c8d;cursor: pointer;padding: 5px;}.modal-body {padding: 25px;}.modal-message {font-size: 1.1rem;color: #555;line-height: 1.6;}.modal-footer {padding: 20px 25px;background-color: #f8f9fa;display: flex;justify-content: flex-end;gap: 15px;}.btn-cancel {background-color: #95a5a6;color: white;}.btn-cancel:hover {background-color: #7f8c8d;}.btn-confirm {background-color: #e74c3c;color: white;}.btn-confirm:hover {background-color: #c0392b;}/* 动画 */@keyframes fadeIn {to {opacity: 1;}}@keyframes slideUp {to {transform: translateY(0);}}.fade-out {animation: fadeOut 0.2s forwards;}@keyframes fadeOut {to {opacity: 0;}}/* 响应式设计 */@media (max-width: 768px) {.container {padding: 15px;}.demo-section {padding: 20px;}.button-group {flex-direction: column;}button {width: 100%;}}.code-block {background-color: #2d2d2d;color: #f8f8f2;padding: 20px;border-radius: 6px;overflow-x: auto;margin: 20px 0;font-family: 'Courier New', monospace;font-size: 14px;}</style>
</head>
<body><div id="app"><div class="container"><header><h1>Vue确认操作弹窗</h1><p class="subtitle">优雅的用户确认交互设计</p></header><section class="demo-section"><h2>演示示例</h2><p>点击下面的按钮体验不同类型的确认弹窗:</p><div class="button-group"><button class="btn-primary" @click="showDefaultConfirm">默认确认弹窗</button><button class="btn-danger" @click="showDeleteConfirm">删除确认</button><button class="btn-warning" @click="showWarningConfirm">警告确认</button><button class="btn-success" @click="showCustomConfirm">自定义弹窗</button></div><div class="result" v-if="lastAction"><strong>操作结果:</strong> {{ lastAction }}</div></section><section class="demo-section"><h2>实现代码</h2><p>以下是如何在Vue中实现确认弹窗的核心代码:</p><div class="code-block">&lt;!-- 确认弹窗组件 --&gt;&lt;div class="modal-overlay" v-if="showModal" @click.self="closeModal"&gt;&lt;div class="modal-content"&gt;&lt;div class="modal-header"&gt;&lt;h3 class="modal-title"&gt;{{ modalTitle }}&lt;/h3&gt;&lt;button class="modal-close" @click="closeModal"&gt;&times;&lt;/button&gt;&lt;/div&gt;&lt;div class="modal-body"&gt;&lt;p class="modal-message"&gt;{{ modalMessage }}&lt;/p&gt;&lt;/div&gt;&lt;div class="modal-footer"&gt;&lt;button class="btn-cancel" @click="cancel"&gt;{{ cancelText }}&lt;/button&gt;&lt;button class="btn-confirm" @click="confirm"&gt;{{ confirmText }}&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</div><div class="code-block">// 使用方法this.$confirm({title: '确认操作',message: '您确定要执行此操作吗?',confirmText: '确定',cancelText: '取消'}).then(() => {// 用户点击确定console.log('操作已确认');}).catch(() => {// 用户点击取消console.log('操作已取消');});</div></section></div><!-- 确认弹窗组件 --><div class="modal-overlay" v-if="showModal" @click.self="closeModal"><div class="modal-content"><div class="modal-header"><h3 class="modal-title">{{ modalTitle }}</h3><button class="modal-close" @click="closeModal">&times;</button></div><div class="modal-body"><p class="modal-message">{{ modalMessage }}</p></div><div class="modal-footer"><button class="btn-cancel" @click="cancel">{{ cancelText }}</button><button class="btn-confirm" @click="confirm">{{ confirmText }}</button></div></div></div></div><script>// 确认弹窗插件const ConfirmPlugin = {install(Vue) {Vue.prototype.$confirm = function(options) {return new Promise((resolve, reject) => {const vm = this;vm.$set(vm, 'showModal', true);vm.$set(vm, 'modalTitle', options.title || '确认操作');vm.$set(vm, 'modalMessage', options.message || '您确定要执行此操作吗?');vm.$set(vm, 'confirmText', options.confirmText || '确定');vm.$set(vm, 'cancelText', options.cancelText || '取消');// 存储resolve和reject以便在按钮点击时调用vm.$set(vm, 'confirmResolve', resolve);vm.$set(vm, 'confirmReject', reject);});};}};Vue.use(ConfirmPlugin);new Vue({el: '#app',data: {showModal: false,modalTitle: '确认操作',modalMessage: '您确定要执行此操作吗?',confirmText: '确定',cancelText: '取消',lastAction: '',confirmResolve: null,confirmReject: null},methods: {showDefaultConfirm() {this.$confirm({title: '确认操作',message: '这是一个默认的确认弹窗示例。',confirmText: '继续',cancelText: '取消'}).then(() => {this.lastAction = '用户确认了默认操作';}).catch(() => {this.lastAction = '用户取消了默认操作';});},showDeleteConfirm() {this.$confirm({title: '删除确认',message: '此操作将永久删除该项,且无法恢复。您确定要继续吗?',confirmText: '删除',cancelText: '取消'}).then(() => {this.lastAction = '用户确认删除操作';}).catch(() => {this.lastAction = '用户取消了删除操作';});},showWarningConfirm() {this.$confirm({title: '警告',message: '此操作可能会导致系统异常,建议在非工作时间执行。您确定要继续吗?',confirmText: '继续',cancelText: '取消'}).then(() => {this.lastAction = '用户确认了警告操作';}).catch(() => {this.lastAction = '用户取消了警告操作';});},showCustomConfirm() {this.$confirm({title: '自定义确认',message: '这是一个完全自定义的确认弹窗,您可以修改标题、内容和按钮文字。',confirmText: '我确定',cancelText: '再想想'}).then(() => {this.lastAction = '用户确认了自定义操作';}).catch(() => {this.lastAction = '用户取消了自定义操作';});},closeModal() {this.showModal = false;if (this.confirmReject) {this.confirmReject();}},cancel() {this.showModal = false;if (this.confirmReject) {this.confirmReject();}},confirm() {this.showModal = false;if (this.confirmResolve) {this.confirmResolve();}}}});</script>
</body>
</html>

功能说明

这个Vue确认弹窗具有以下特点:

  1. 可复用组件:通过Vue插件方式实现,可在任何组件中调用

  2. 自定义选项:支持自定义标题、消息内容和按钮文字

  3. Promise支持:使用Promise处理确认和取消操作

  4. 动画效果:弹窗出现和消失有平滑的动画效果

  5. 响应式设计:在移动设备和桌面设备上都能良好显示

  6. 多种示例:提供了四种不同类型的确认弹窗示例

使用方法

在Vue组件中,可以通过以下方式调用确认弹窗:

this.$confirm({title: '确认操作',message: '您确定要执行此操作吗?',confirmText: '确定',cancelText: '取消'
}).then(() => {// 用户点击确定console.log('操作已确认');
}).catch(() => {// 用户点击取消console.log('操作已取消');
});

这个实现既美观又实用,可以满足大多数确认操作的需求。

http://www.dtcms.com/a/594542.html

相关文章:

  • 学校网站建设钉钉花桥网站建设
  • 网站微商城的建设网站关键词建设
  • 企业网站建设的原则做公众号一个月挣多少钱
  • yum 源无法访问及DNS 解析失败问题
  • 咸阳网站开发wordpress作者插件
  • STM32配置注意事项
  • 做中国菜的外国网站网站建设与设计ppt模板下载
  • 东莞模板网站制作哪家好江苏省造价信息工程网
  • 常用数学函数详解:从基础运算到图形学应用
  • 杭州网站改版公司兰州网站设计公司排名
  • vcpkg安装包报错 错误 TRK0005: 未能找到: rc.exe ,系统找不到指定的文件问题解决
  • 旅游网站模板免费室内设计联盟论坛官网
  • wordpress 用户名 密码合肥seo网站多少钱
  • 11.10 脚本网页 中国象棋2版
  • 基于站点的网络营销方法app开发多少钱
  • 无忧网站建设费用做一个手机app大概需要多少钱
  • winlogon!SASWndProc函数分析之win+L键的处理
  • Uni-app条件编译(// #ifndef APP)
  • 做网站为什么很复杂建好网站是不是还得维护
  • 非专业人士可以做网站编辑的工作吗WordPress文章生成图片
  • 平凉市城乡建设局网站让别人访问自己做的网站
  • 企业经营异常信息查询接口分享、技术文档
  • seo网站分析报告网站分享的功能怎么做
  • idae快捷键
  • 推荐西安优秀的响应式网站建设公司教务管理系统下载
  • 企业部署智能决策系统成本高吗?
  • PCB之电源完整性之电源网络的PDN仿真CST---06
  • 搭建一个简单的springcloud服务
  • 重庆科技网站建设婚纱摄影哪家好
  • 不让Django DRF ListAPIView 类进行2次查询