简体   繁体   English

将鼠标悬停在vuejs中的父菜单或子菜单上时如何继续显示动态子菜单?

[英]How to keep showing the dynamic submenu when hovering over parent menu or submenu in vuejs?

I need help regarding this sidebar in vuejs, I am trying to make a dynamic sidebar where I have to keep open the submenu while hovering over the parent menu or the submenu, whenever I remove the mouse from parent the submenu disappear.我需要关于 vuejs 中这个侧边栏的帮助,我正在尝试制作一个动态侧边栏,当我将鼠标从父级菜单或子菜单上移开时,我必须在其中保持打开子菜单,每当我从父级删除鼠标时,子菜单就会消失。 I need to keep opening the submenu.我需要继续打开子菜单。 Here is my code:这是我的代码:

<div v-for="item in items" class="categories" >
       <p  @mouseover="onOver(item)" @mouseleave="onLeave">{{item.title}}</p>
       <div v-if="ShowSubMenu" class="subMenu">
           <div v-for="childItem in item.child" > <p class="text"> {{childItem.childTitle}}</p> </div>
           </div>

       </div>

     </div>

Vue Js: Vue Js:

data: {
  ShowSubMenu: false,

    items: [
    {
       title: 'name'
      },
      {
       title:'hoverMe',
       child : [
       {childTitle : 'ChildTitle1'},
        {childTitle : 'ChildTitle2'},
         {childTitle : 'ChildTitle3'},

       ] ,
       },
       { 
        title: 'name3'}
    ]
  },
  methods: {
    onOver: function(item){
        if (item.child != null) {
      this.ShowSubMenu=true;


      }
    },
    onLeave: function(){

      this.ShowSubMenu=false;



    }
  }

css: css:

.categories{
  background-color:red;
  width:100px;
  height:200px;
}
.subMenu{
  background-color:red;



  height:150px;
  width:150px;
  position:absolute;
  top:100px;
  left:150px;

}
.text{
  color:blue;





}

This would be one of the possible solutions to your problem.这将是您的问题的可能解决方案之一。 It's purely CSS based too:它也纯粹基于 CSS:

https://jsfiddle.net/vugrjbn6/16/ https://jsfiddle.net/vugrjbn6/16/

.categories{
  background-color: red;
  width: 100px;
  height: 200px;
  position: relative;
}

.subMenu{
  display: none;
  position: absolute;
  background-color: red;
  height: 150px;
  width: 150px;
  top: 0;
  left: 100%;
}

.categories:hover .subMenu {
  display: block;
}

It works by displaying the submenu only if the category is hovered.只有当类别悬停时,它才会显示子菜单。 The important thing to realize is that category contains the title AND submenu.要意识到的重要一点是该类别包含标题和子菜单。 So by default submenu is hidden, if you first hover the title it gets displayed, and it stays displayed as long as you keep hovering title or submenu (as both are part of category).因此,默认情况下子菜单是隐藏的,如果您首先 hover 显示它的标题,并且只要您保持悬停标题或子菜单(因为两者都是类别的一部分),它就会保持显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM