作者:林原伟662532 | 来源:互联网 | 2023-10-14 10:53
右键菜单选项的设计效果图基本数据分析功能菜单的渲染模块右键菜单的样式最终结果当写完本系列后,我会把源代码分享出来给大家。本课程也会持续更新与矫正。欢迎留言指正
右键菜单选项的设计
- 效果图
- 基本数据
- 功能
- 菜单的渲染模块
- 右键菜单的样式
- 最终结果
当写完本系列后,我会把源代码分享出来给大家。本课程也会持续更新与矫正。欢迎留言指正!
效果图
基本数据
首先构建一个基本的配置数据
this.contextmenulist &#61; [{"icon": "","name": "复制","event": function () { }},{"icon": "","name": "剪切","event": function () { }},{"icon": "","name": "粘贴","event": function () { }},{"icon": "","name": "清空全部格式","event": function () {var cells__inputs &#61; document.getElementsByClassName("cells__input");var length &#61; cells__inputs.length;for(var i &#61; 0;i<length;i&#43;&#43;){cells__inputs[i].setAttribute("style","");}}}
];
分析
属性 | 说明 |
---|
icon | 图标 |
name | 按钮的文字 |
event | 点击该按钮后会执行的代码 |
功能
cellcontextmenu(ev){var that &#61; thisfunction copy (array) {let newArray &#61; []for(let item of array) {newArray.push(item);}return newArray;}var target &#61; ev.target || ev.srcElement;var contextmenulist &#61; copy(that.contextmenulist);that.updata_contextmenulist(contextmenulist,that.right__menu_list)console.log("单元格右击",target.getAttribute("data-x"),target.getAttribute("data-y"))var oDiv &#61; document.getElementById(that.right__menu);oDiv.style.display &#61; "block";oDiv.style.left &#61; ev.clientX&#43;"px";oDiv.style.top &#61; ev.clientY&#43;"px";that.focus["x"] &#61; target.getAttribute("data-x");that.focus["y"] &#61; target.getAttribute("data-y");console.log(that.focus);}
菜单的渲染模块
updata_contextmenulist(contextmenulist,id){ this.empty_id(id)var len &#61; contextmenulist.length;var Tags &#61; [];for(var i &#61; 0; i < len; i&#43;&#43; ){Tags.push({"Tag" : "li", "ID" : this.creatGlobalId(), "ClassName" : "right__menu__element", "Text":contextmenulist[i]["name"],"Data" : [{ "k":"id", "v":i }],"Action":{"click":function(ev){var target &#61; ev.target || ev.srcElement;console.log("右键菜单按钮"&#43;target.getAttribute("data-id")&#43;"点击事件");contextmenulist[target.getAttribute("data-id")]["event"]();}}});}this.renderDOM(id,Tags,function(target){},function(target){});}
右键菜单的样式
.right__menu{position: absolute;background: #fff;border: 1px solid black;display: none;
}.right__menu__element{list-style-type: none;margin: 10px;text-align: left;cursor:default;
}
最终结果
这个右键菜单会跟着鼠标的位置一起展示&#xff0c;如果点击其他位置&#xff0c;它就会消失。这里面用到了事件的冒泡机制。我后面会在更深一步介绍渲染器的代码里详细介绍到。