点击button出现水平二级菜单

实现如下效果:

 

可以使用el组件el-popover

注意:

1.对el-popover的样式修改时,需要使用popper-class设置类名,并以.el-popover,不加空格(同级类名),然后加自己设置的类名,来设置样式。同时注意,style不能加scoped

 因为q-popover和 App.vue组件的div平级,所以需要设置全局style,在标签里,不加scoped就是全局style
然后因为el-popover是独立的div,不要将样式加在嵌套里,也不能用穿透符>>>因为el-popover不在当前组件之内

2.对于动态加载的元素,给二级ul设置位置时,不需要获取一级ul的宽度(层级太多,获取不到,浪费时间。。血的教训),可以给一级li设置position:relative,然后二级ul设置position:absolute后加上right:100%就可以实现。

<template>
  <div class="add-menu">
    <el-popover
      placement="left"
      :visible-arrow="false"
      popper-class="popover-menu"
      v-model="visible"
      >
        <ul class="first-menu" ref="firstMenu">
          <li v-for="item in options" :key="item.value" @click="handleClick(item)">
            <i v-if="!item.children" class="el-icon-caret-left icon-show"></i>
            <template v-if="item.children">
              <i class="el-icon-caret-left"></i>
              <ul class="second-menu" id="secondMenu">
                <li v-for="it in item.children" :key="it.value" @click.stop="handleClick(it)">{{it.label}}</li>
              </ul>
            </template>
            {{item.label}}
          </li>
        </ul>
      <el-button slot="reference" type="info">新增</el-button>
    </el-popover>
  </div>
</template>
<script>

export default {
  data() {
    return {
      options:[{
          value: 'zhinan',
          label: '指南',
          children: [{
            value: 'shejiyuanze',
            label: '设计原则',
            children: [{
              value: 'yizhi',
              label: '一致'
            }, {
              value: 'fankui',
              label: '反馈'
            }, {
              value: 'xiaolv',
              label: '效率'
            }, {
              value: 'kekong',
              label: '可控'
            }]
          }, {
            value: 'daohang',
            label: '导航',
          }]
        }, {
          value: 'zujian',
          label: '组件',
          children: [{
            value: 'basic',
            label: 'Basic',
            children: [{
              value: 'layout',
              label: 'Layout 布局'
            }, {
              value: 'color',
              label: 'Color 色彩'
            }, {
              value: 'typography',
              label: 'Typography 字体'
            }, {
              value: 'icon',
              label: 'Icon 图标'
            }, {
              value: 'button',
              label: 'Button 按钮'
            }]
          }, {
            value: 'form',
            label: 'Form',
            children: [{
              value: 'radio',
              label: 'Radio 单选框'
            }, {
              value: 'checkbox',
              label: 'Checkbox 多选框'
            }, {
              value: 'input',
              label: 'Input 输入框'
            }, {
              value: 'input-number',
              label: 'InputNumber 计数器'
            }, {
              value: 'select',
              label: 'Select 选择器'
            }, {
              value: 'cascader',
              label: 'Cascader 级联选择器'
            }, {
              value: 'switch',
              label: 'Switch 开关'
            }, {
              value: 'slider',
              label: 'Slider 滑块'
            }, {
              value: 'time-picker',
              label: 'TimePicker 时间选择器'
            }, {
              value: 'date-picker',
              label: 'DatePicker 日期选择器'
            }, {
              value: 'datetime-picker',
              label: 'DateTimePicker 日期时间选择器'
            }, {
              value: 'upload',
              label: 'Upload 上传'
            }, {
              value: 'rate',
              label: 'Rate 评分'
            }, {
              value: 'form',
              label: 'Form 表单'
            }]
          }, {
            value: 'data',
            label: 'Data',
            children: [{
              value: 'table',
              label: 'Table 表格'
            }, {
              value: 'tag',
              label: 'Tag 标签'
            }, {
              value: 'progress',
              label: 'Progress 进度条'
            }, {
              value: 'tree',
              label: 'Tree 树形控件'
            }, {
              value: 'pagination',
              label: 'Pagination 分页'
            }, {
              value: 'badge',
              label: 'Badge 标记'
            }]
          }, {
            value: 'notice',
            label: 'Notice',
            children: [{
              value: 'alert',
              label: 'Alert 警告'
            }, {
              value: 'loading',
              label: 'Loading 加载'
            }, {
              value: 'message',
              label: 'Message 消息提示'
            }, {
              value: 'message-box',
              label: 'MessageBox 弹框'
            }, {
              value: 'notification',
              label: 'Notification 通知'
            }]
          }, {
            value: 'others',
            label: 'Others',
            children: [{
              value: 'dialog',
              label: 'Dialog 对话框'
            }, {
              value: 'tooltip',
              label: 'Tooltip 文字提示'
            }, {
              value: 'popover',
              label: 'Popover 弹出框'
            }, {
              value: 'card',
              label: 'Card 卡片'
            }, {
              value: 'carousel',
              label: 'Carousel 走马灯'
            }, {
              value: 'collapse',
              label: 'Collapse 折叠面板'
            }]
          }]
        }, {
          value: 'ziyuan',
          label: '资源',
          children: [{
            value: 'axure',
            label: 'Axure Components'
          }, {
            value: 'sketch',
            label: 'Sketch Templates'
          }, {
            value: 'jiaohu',
            label: '组件交互文档'
          }]
        }]
      visible: false,
    }
  },
  methods: {
    handleClick(row) {
      this.visible = row.children ? true : false
    },
}
</script>

<style lang="scss">
ul {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size:14px;
  background-color:white;
  border: 1px solid #ccc;
  li {
    padding: 8px;
  }
  li:hover {
    background-color: #ccc;
  }
}
  .first-menu > li ul {
    display: none;
  }
  .first-menu > li:hover ul {
    display: block;
  }

.add-menu {
  display: inline-block;
  position: absolute;
  right: 80px;
  top: 400px;
}
.first-menu {
  &> li {
    position: relative;
  }
}
.second-menu {
  min-width:80px;
  position: absolute;
  top: 0;
  right:100%
}
.icon-show {
  visibility: hidden;
}
</style>
<style>
.el-popover.popover-menu {
  background-color: transparent;
  min-width: 100px;
}
.el-popover.popover-menu .el-popover__content {
  padding: 0;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一个JavaFX实现的示例代码,可以通过点击Button来打开一个快捷菜单,并在快捷菜单中添加一个选项,用于打开文件目录。 ``` import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; import javafx.scene.layout.StackPane; import javafx.stage.DirectoryChooser; import javafx.stage.Stage; import java.io.File; public class ShortcutMenuDemo extends Application { @Override public void start(Stage primaryStage) throws Exception { // 创建一个Button Button button = new Button("打开快捷菜单"); // 创建一个ContextMenu ContextMenu contextMenu = new ContextMenu(); // 创建一个MenuItem,用于打开文件目录 MenuItem openDirectoryItem = new MenuItem("打开文件目录"); openDirectoryItem.setOnAction(event -> { DirectoryChooser directoryChooser = new DirectoryChooser(); directoryChooser.setTitle("选择文件目录"); File directory = directoryChooser.showDialog(primaryStage); if (directory != null) { // 执行打开文件目录的操作 System.out.println("选择的目录:" + directory.getAbsolutePath()); } }); contextMenu.getItems().add(openDirectoryItem); // 将ContextMenu绑定到Buttonbutton.setContextMenu(contextMenu); // 将Button添加到布局中 StackPane root = new StackPane(); root.getChildren().add(button); // 创建一个Scene并显示 Scene scene = new Scene(root, 300, 250); primaryStage.setScene(scene); primaryStage.setTitle("快捷菜单示例"); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个示例中,我们创建了一个Button和一个ContextMenu,并将ContextMenu绑定到Button上。然后,我们创建了一个MenuItem,用于打开文件目录,并将它添加到ContextMenu上。最后,我们将Button添加到一个StackPane布局中,并创建一个Scene并显示。 当用户点击Button时,ContextMenu会弹出,并显示一个选项,用于打开文件目录。当用户选择该选项时,会弹出一个文件选择框,用户选择文件目录后,程序会输出所选择的目录路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值