Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
Custom directive to replace @mouseenter + @mouseleave event?

I'm having lots of elements on which @mouseenter set a value to true and @mouseleave sets it to false. Basically what I need is a way to set a reactive variable to true if the mouse hovers the element.

I've been trying to figure out how to write such custom directive from the docs but it only mentions how to use .focus() js function on an element. Which js functions would be used for said directive?

Something like:

const vHover = {
    mounted: (el) => {
        el.addEventListener('mouseenter', state.hover=true)
        el.addEventListener('mouseleave', state.hover=false)
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think you could do something like:

app.directive('hover', {
    created(el, binding) {
        const callback = binding.value
        el.onmouseenter = () => callback(true)
        el.onmouseleave = () => callback(false)
    },
    unmounted(el) {
        el.onmouseenter = null
        el.onmouseleave = null
    }
})

Template:

<button v-hover="onHoverChange">Example</button>

Methods:

onHoverChange(isHovered) {
    console.log(isHovered)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I believe this is not the intended use of directives. The value of the state cannot be mutated within the directive. You can pass the variable through the binding, but you cannot update it.

binding: an object containing the following properties.

  • value: The value passed to the directive. For example in v-my-directive="1 + 1", the value would be 2.
  • oldValue: The previous value, only available in beforeUpdate and updated. It is available whether or not the value has changed.

so if you do el.addEventListener('mouseenter', binding.hover=true), as you may have noticed, it will not update the state.

However, if we use the internals (PSA: though not recommended since they could potentially change at any time), you could get instance using the vnode, and use the binding.arg to denote which Proxy (state)

so you could get the reactive variable with vnode.el.__vueParentComponent.data[binding.arg]

<script>
export default {
  data(){
    return {
      state: { hover:false }
    }
  },
  directives: {
    hover: {
      mounted(el, binding, vnode) {
        el.addEventListener('mouseenter', () => {
          vnode.el.__vueParentComponent.data[binding.arg].hover = true
        })
        el.addEventListener('mouseleave', () => {
          vnode.el.__vueParentComponent.data[binding.arg].hover = false
        })
      },
    }
  }
}
</script>

<template>
  <h1 v-hover:state="state">HOVER {{ state }}</h1>
</template>

SFC playground link

of course you might want to add the unmounted and even consider adding mouseleave dynamically only when mouseenter fires

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda