简体   繁体   English

悬停在vue框架中更改多个元素图标

[英]On hover change multiple elements icon in vue framework

I have four elements, On hover I want to change the icon of the hovered element, I know I can do it with JS/jQuery but I'm new to vue and want to do it in Vue. 我有四个元素,在悬停时我想改变悬停元素的图标,我知道我可以用JS / jQuery做到这一点,但我是vue的新手,想要在Vue中做到这一点。 Now It changed the all four elements icon with this code: 现在它使用以下代码更改了所有四个元素图标:

HTML: HTML:

<div class="col-md-3">
<div class="welcome-latest">
    <div class="hover-icon animated" v-bind:class="{ latestActive : isActive }">
        <i class="fa fa-play" aria-hidden="true"></i>
    </div>
    <div class="welcome-latest-part"
            v-on:mouseover="latestActive"
            v-on:mouseleave="latestInActive">
        <a href="#">
            <div class="latest-icon">
                <i class="fa fa-play-circle-o" aria-hidden="true"></i>
            </div>
            <hr>
            <div class="latest-title">
                <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit</h2>
            </div>
        </a>
    </div>
</div>
</div>

Vue: VUE:

new Vue({
    el: '#app',

    data : {
        isActive : true
    },

    methods : {
        latestActive (part) {
            this.isActive = false;
        },
        latestInActive(part) {
            this.isActive = true;
        }
    }
});

but I want to change only the hovered element! 但我想只改变悬停的元素! How I will do that? 我该怎么做? Thank in advanced. 谢谢高级。

This is my fiddle ! 这是我的小提琴

在此输入图像描述

Essentially your problem is you are using a global state to set the class for all the elements here. 基本上你的问题是你使用全局状态来设置所有元素的类。

v-bind:class="{ latestActive : isActive }"

One way to solve this would be to turn your columns into components. 解决此问题的一种方法是将列转换为组件。 That way, they would each have their own state. 这样,他们每个人都有自己的状态。

Vue.component("component",{
  props:["text"],
  template:"#component",
  data(){
    return {
        isActive:true
    }
  }
})

<template id="component">
  <div class="welcome-latest">
    <div class="hover-icon animated" :class="{ latestActive : isActive }">
      <i class="fa fa-play" aria-hidden="true"></i>
    </div>
    <div class="welcome-latest-part"
         v-on:mouseover="isActive=true"
         v-on:mouseleave="isActive=false">
      <a href="#">
        <div class="latest-icon">

        </div>
        <hr>
        <div class="latest-title">
          <h2>{{text}}</h2>
        </div>
      </a>
    </div>
  </div>
</template>

And then modify #app like this: 然后像这样修改#app

<div class="col-md-3">
    <component text="Lorem ipsum dolor sit amet, consectetur adipisicing elit"></component>
</div>

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

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