我使用了这个参考:
https :
//stackoverflow.com/a/53108600 在内部 v-tooltip 悬停时关闭外部 v-tooltip。
问题是这种方法只有在有一个容器要处理时才优雅。
当您开始拥有许多容器时,您必须在 vue 中为每个容器创建一个数据变量。
代码示例:
https
://jsfiddle.net/jyts1fug/6/
如小提琴所示,我现在有 2 个变量,isOpen
并且isOpen2
.
我想为此找到一个更优雅的解决方案。
问问题
244 次
1 回答
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>
于 2020-10-31T17:40:21.523 回答