【问题标题】:How to close outer v-tooltip on inner v-tooltip when there are many containers有很多容器时如何在内部 v-tooltip 上关闭外部 v-tooltip
【发布时间】:2020-10-31 16:04:34
【问题描述】:

我使用了这个参考: https://stackoverflow.com/a/53108600 在内部 v-tooltip 悬停时关闭外部 v-tooltip。
问题是这种方法只有在有一个容器要处理时才是优雅的。
当您开始拥有许多容器时,您必须在 vue 中为每个容器创建一个数据变量。
代码示例: https://jsfiddle.net/jyts1fug/6/
如小提琴所示,我现在有 2 个变量,isOpenisOpen2
我想为此找到一个更优雅的解决方案。

【问题讨论】:

    标签: javascript vue.js vuejs2 v-tooltip


    【解决方案1】:

    您需要一些东西来跟踪哪些元素是打开的,哪些不是。这将在很大程度上依赖于您正在使用的现有数据结构。

    在这种情况下,在您的代码示例中,我会执行以下操作:

    注意:index+'inner' 不是很干净的方式,它只是为了演示。您可以选择循环内的任何变量进行跟踪。例如用户 ID 和用户帖子 ID。

    console.clear()
    
    new Vue({
      el: '#app',
      data: {
        openId: null,
        message: 'Outer Tooltip'
      },
    })
    body {
      font-family: sans-serif;
      margin: 42px;
    }
    
    .tooltip {
      display: block !important;
      z-index: 10000;
    }
    
    .tooltip .tooltip-inner {
      background: black;
      color: white;
      border-radius: 16px;
      padding: 5px 10px 4px;
    }
    
    .tooltip .tooltip-arrow {
      width: 0;
      height: 0;
      border-style: solid;
      position: absolute;
      margin: 5px;
      border-color: black;
    }
    
    .tooltip[x-placement^="top"] {
      margin-bottom: 5px;
    }
    
    .tooltip[x-placement^="top"] .tooltip-arrow {
      border-width: 5px 5px 0 5px;
      border-left-color: transparent !important;
      border-right-color: transparent !important;
      border-bottom-color: transparent !important;
      bottom: -5px;
      left: calc(50% - 5px);
      margin-top: 0;
      margin-bottom: 0;
    }
    
    .tooltip[x-placement^="bottom"] {
      margin-top: 5px;
    }
    
    .tooltip[x-placement^="bottom"] .tooltip-arrow {
      border-width: 0 5px 5px 5px;
      border-left-color: transparent !important;
      border-right-color: transparent !important;
      border-top-color: transparent !important;
      top: -5px;
      left: calc(50% - 5px);
      margin-top: 0;
      margin-bottom: 0;
    }
    
    .tooltip[x-placement^="right"] {
      margin-left: 5px;
    }
    
    .tooltip[x-placement^="right"] .tooltip-arrow {
      border-width: 5px 5px 5px 0;
      border-left-color: transparent !important;
      border-top-color: transparent !important;
      border-bottom-color: transparent !important;
      left: -5px;
      top: calc(50% - 5px);
      margin-left: 0;
      margin-right: 0;
    }
    
    .tooltip[x-placement^="left"] {
      margin-right: 5px;
    }
    
    .tooltip[x-placement^="left"] .tooltip-arrow {
      border-width: 5px 0 5px 5px;
      border-top-color: transparent !important;
      border-right-color: transparent !important;
      border-bottom-color: transparent !important;
      right: -5px;
      top: calc(50% - 5px);
      margin-left: 0;
      margin-right: 0;
    }
    
    .tooltip[aria-hidden='true'] {
      visibility: hidden;
      opacity: 0;
      transition: opacity .15s, visibility .15s;
    }
    
    .tooltip[aria-hidden='false'] {
      visibility: visible;
      opacity: 1;
      transition: opacity .15s;
    }
    
    .box{
      border: 1px solid red;
      border-radius: 2px;
      padding: 15px;
      margin: 20px;
      text-align: center;
      cursor: pointer;
    }
    .box:hover{
      box-shadow: 0 0 4px;
    }
    <script src="https://unpkg.com/popper.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/v-tooltip"></script>
    
    <div id="app">
      <div v-for="index in 2" 
           v-tooltip="{content: message, show: openId === index}" 
           class="box" 
           @mouseover.stop="openId=index"
      >
        
        {{ message }}
        
        <div v-tooltip="{content: 'Inner',
        show: openId === index+'inner'}" @mouseover.stop="openId=index+'inner'" class="box">
          okokok
        </div>
        
      </div>
    </div>

    【讨论】:

    • 我不一定将所有容器都添加到 v-for 标签下。这些容器可能位于 html 中的不同位置。
    • v-for 不是必须的,你可以使用任何唯一的 id。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2018-06-25
    • 2020-01-24
    • 2021-04-29
    • 1970-01-01
    • 2020-09-08
    相关资源
    最近更新 更多