在vue.js中将鼠标悬停在上面

时间:2019-03-01 00:39:50

标签: vue.js hover

我试图将鼠标悬停在vue.js中的图像上时显示文本,但我有点卡住了。我试图在具有多个图像的数组上实现此示例: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade

我有一个很大的Vue文件,但这是必不可少的:

模板:

</template>

[...]

<div v-for="item in filteredPeople" v-bind:key="item.id" class="list-complete-item">
  <router-link @mouseover.native="hovertext" :to="'/'+item.link">
    <img class="list-complete-img" :src="'http://placehold.it/400x300?text='+item.id" alt>  
  </router-link>
</div>

[...]

</template>

脚本

<script>
export default {
  data() {
    return {
      info: [
        {
          id: 1,
          title: "Title one",
          link: "project1",
          hovertext: "project1 hover text lorem lorem lorem",
          category_data: {
            "1": "Category 1"
          }
        },
[...]
  methods: {
    hovertext() {
      console.log("");
    },

我有一个主意,尝试使用一种方法将文本放在图像下方的div中,但是在悬停时我无法使文本位于图像上方。而且我无法正确使用该方法...不确定此方法是否很好,

我还找到了这个Codepen示例: https://codepen.io/oliviaisarobot/pen/xzPGvY

这与我要执行的操作非常接近,但是我也无法在此处显示文本。

我有点迷茫。有任何帮助如何在vue中做到这一点?

----------更新----------

我希望图像框能够拉伸,以便它们始终适合窗口,但是我的flexbox行之间似乎有缝隙,这似乎无法消除。您可以看到空白。我附上我的样式表。

    .list-complete {       显示:flex;       高度:自动;       flex-direction:行;       flex-flow:行换行;     }

.list-complete-item {
  flex: 0 1 50%;
  display: inline-block;
}

.list-complete-item a {
  display: inline-block;
  position: relative;
  width: 100%;
  height: auto;
  outline: 1px solid #fff;
}

.list-complete-img {
  width: 100%;
}

.text {
  color: rgb(186, 74, 74);
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
}

.list-complete-item a:hover .overlay {
  opacity: 1;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  height: 60%;
  width: 100%;
  opacity: 0;
  transition: 0.5s ease;
  background-color: #008cba;
}

1 个答案:

答案 0 :(得分:1)

您不需要使用js(vue)事件。用普通的CSS来做,就像您链接到的例子一样。

使用此模板:

<div v-for="item in filteredPeople" v-bind:key="item.id" class="list-complete-item">
    <router-link :to="'/'+item.link">
        <img class="list-complete-img" :src="'http://placehold.it/400x300?text='+item.id" alt>
        <div class="overlay">
            <div class="text">{{ item.hovertext }}</div>
        </div>
    </router-link>
</div>

并设置样式:

.list-complete-item {
    width: 400px;
    height: 300px;
    display: inline-block;
}
.list-complete-item a {
    display: inline-block;
    position: relative;
    width: 400px;
    height: 300px;
}
.list-complete-item a .image {
    display: block;
    width: 100%;
    height: auto;
}
.list-complete-item a .overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;
    background-color: #008CBA;
}
.list-complete-item a:hover .overlay {
    opacity: 1;
}
.list-complete-item a .text {
    color: white;
    font-size: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    text-align: center;
}

最终结果:

enter image description here