🎉【实用技巧 | 高效前端开发】弹窗结构一览——最新代码拆解与提取提醒🎉
双回调自定义弹窗类(CSDN @LAOLONG-C)
🔧 核心结构:
<div class="modal" id="custom-modal"> <div class="modal-overlay" data-close></div> <div class="modal-container"> <div class="modal-header"> <h2>🎯 自定义标题</h2> <button class="modal-close" data-close>❌</button> </div> <div class="modal-body"> <p>📝 弹窗内容区域(支持HTML)</p> </div> <div class="modal-footer"> <button class="modal-btn confirm" data-callback="confirm">✅ 确认</button> <button class="modal-btn cancel" data-callback="cancel">❌ 取消</button> </div> </div> </div>
🎨 CSS亮点:
.modal-container { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); animation: slideIn 0.3s ease; } @keyframes slideIn { from { opacity: 0; transform: translate(-50%, -60%); } to { opacity: 1; transform: translate(-50%, -50%); } }
🚀 JavaScript逻辑:
class NewMsgBox { constructor(options) { this.title = options.title || '提示'; this.content = options.content || ''; this.callbacks = { confirm: () => {}, cancel: () => {} }; this.initDOM(); this.bindEvents(); } // 初始化DOM结构 initDOM() { /* 动态创建弹窗元素 */ } // 事件绑定(支持回车键确认/ESC取消) bindEvents() { document.addEventListener('keydown', (e) => { if (e.key === 'Enter') this.callbacks.confirm(); if (e.key === 'Escape') this.close(); }); } // 暴露确认/取消回调接口 on(event, callback) { if (['confirm', 'cancel'].includes(event)) { this.callbacks[event] = callback; } } }
函数式弹窗复用方案(Vue3 + TSX)
📦 Pinia状态管理:
// dialogStore.ts import { defineStore } from 'pinia'; export const useDialogStore = defineStore('dialog', { state: () => ({ visible: false, config: { title: '', content: '', onConfirm: () => {} } }), actions: { open(config) { this.config = config; this.visible = true; }, close() { this.visible = false; } } });
🔄 动态渲染组件:
// DialogComponent.vue export default defineComponent({ setup() { const store = useDialogStore(); return () => store.visible ? ( <div class="dialog-mask"> <div class="dialog-content"> <h3>{store.config.title}</h3> <p>{store.config.content}</p> <button onClick={() => { store.config.onConfirm(); store.close(); }}>确认</button> </div> </div> ) : null; } });
文本提取三板斧
const modal = document.querySelector('.modal.active'); const content = modal.querySelector('.modal-body').innerText;
// 腾讯云OCR调用示例 const client = new TencentCloud.ocr.v20181119.Client(); client.GeneralBasicOCR({ ImageUrl: '弹窗截图URL' }).then(data => console.log(data.TextDetections));
// 腾讯云NLP关键词提取 const nlp = new TencentCloud.nlp.v20190408.Client(); nlp.KeywordsExtraction({ Text: content, TopK: 5 }).then(res => console.log('关键信息:', res.Keywords));
自动化测试场景
🤖 Selenium处理弹窗:
# 切换至弹窗上下文 alert = driver.switch_to.alert print(f"弹窗内容:{alert.text}") alert.accept() # 确认 # alert.dismiss() # 取消
let timer; window.addEventListener('resize', () => { clearTimeout(timer); timer = setTimeout(() => adjustModalPosition(), 300); });
.modal-container { contain: layout style paint; /* 限制重绘范围 */ }
const worker = new Worker('modal-calculator.js'); worker.postMessage({ type: 'ANIMATE', data: frameData });
📌 最新趋势(参考《前端周刊第423期》):
<ClientOnly>
包裹弹窗组件 <script setup>
语法糖简化弹窗逻辑 has()
伪类实现更灵活的弹窗状态判断 💡 立即实践:访问GitHub代码库获取完整可运行示例(含TypeScript版本)!
本文由 终端故障调色盘 于2025-07-30发表在【云服务器提供商】,文中图片由(终端故障调色盘)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://vps.7tqx.com/fwqtj/482654.html
发表评论