开发案例-通过JS实现一个可拖拽的弹窗卡片 一个简单的可拖拽的弹窗样式HTML通过给可拖拽区域绑定mousedownstartDragvue的事件来实现元素的拖动功能。CSS通过background: linear-gradient(180deg, color 0%, color 100%)实现背景渐变色宽度通过width: fit-content实现自适应宽同时设置min-width和max-width确保比例不会太离谱。JS通过使用startDrag、onDrag、stopDrag等事件来控制拖动行为。源码divv-ifshowCardrefcardclasscard_infodivclasscard_headermousedownstartDrag/divdivclassclose_infoclickshowCard false×/divspan≡nbsp;nbsp;标题/spanp字段一nbsp;nbsp;{{ metarilInfo.mapCode }}/pp字段二nbsp;nbsp;{{ metarilInfo.areaCode }}/pp字段三nbsp;nbsp;{{ metarilInfo.mapDataCode }}/pp字段四nbsp;nbsp;{{ metarilInfo.coox }}, {{ metarilInfo.cooy }}/pp字段五nbsp;nbsp;{{ metarilInfo.shelfCode }}/pp字段六nbsp;nbsp;{{ metarilInfo.mapDataEmptyDate || / }}/p/divscriptlangtssetupimport{ref}fromvue;constshowCardref(false);//何时显示的代码自己定义constcardrefany(null);constisDraggingref(false);letstartX0;letstartY0;conststartDrag(event){//开始拖拽事件if(!card.value)returnisDragging.valuetrue;startXevent.clientX-card.value.offsetLeft;startYevent.clientY-card.value.offsetTop;document.addEventListener(mousemove,onDrag);document.addEventListener(mouseup,stopDrag);document.addEventListener(mouseleave,stopDrag);};constonDrag(event){//拖拽中事件if(!isDragging.value)returnevent.preventDefault();//阻止浏览器默认行为card.value.style.left${event.clientX-startX}px;card.value.style.top${event.clientY-startY}px;};conststopDrag(){//停止拖拽时销毁监听器isDragging.valuefalse;document.removeEventListener(mousemove,onDrag);document.removeEventListener(mouseup,stopDrag);document.removeEventListener(mouseleave,stopDrag);};/scriptstylelangscssscoped.card_info{position:absolute;top:20%;left:50%;transform:translateX(-50%);min-width:12vw;max-width:fit-content;pointer-events:all;background:linear-gradient(180deg,#236784f1 0%,#236784d2 20%,#2367848a 100%);padding:0.1vw 0.5vw 0.5vw 0.5vw;border:1px solid #44dbee;border-radius:0.25vw;::before{content:;position:absolute;top:0.7vw;left:0;height:1vw;width:0.2vw;background-color:#44dbee;}.card_header{position:absolute;top:0.3vw;left:0.3vw;height:1.5vw;width:90%;cursor:grab;//可拖拽样式z-index:1;:active{cursor:grabbing;//鼠标点击后变为拖拽中样式}}.close_info{font-size:2vh;width:1vw;height:1vw;color:#fff;display:flex;justify-content:center;align-items:center;position:absolute;top:0.1vw;right:0.1vw;:hover{cursor:pointer;background-color:red;}}span{font-size:1vw;font-weight:600;height:1.9vw;line-height:1.9vw;padding:0 1vw 0 0;z-index:2;}p{font-size:0.7vw;height:1.4vw;line-height:1.4vw;background-color:#00000050;margin-top:0.1vw;padding:0 0.3vw;white-space:nowrap;}}/style效果图

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/1 19:32:24
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/30 21:32:07
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/30 19:41:56
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/1 19:32:23
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/1 19:32:35
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/30 21:32:11

日新闻

周新闻

月新闻