Mouseover or hover vue.js

前端 未结 13 1673
温柔的废话
温柔的废话 2021-01-30 02:13

I would like to show a div when hovering over an element in vue.js. But I can\'t seem to get it working.

It looks like there is no event for hover or mouseover in vue.j

相关标签:
13条回答
  • 2021-01-30 02:37

    Please take a look at the vue-mouseover package if you are not satisfied how does this code look:

    <div
        @mouseover="isMouseover = true"
        @mouseleave="isMouseover = false"
    />
    

    vue-mouseover provides a v-mouseover directive that automaticaly updates the specified data context property when the cursor enters or leaves an HTML element the directive is attached to.

    By default in the next example isMouseover property will be true when the cursor is over an HTML element and false otherwise:

    <div v-mouseover="isMouseover" />
    

    Also by default isMouseover will be initially assigned when v-mouseover is attached to the div element, so it will not remain unassigned before the first mouseenter/mouseleave event.

    You can specify custom values via v-mouseover-value directive:

    <div
        v-mouseover="isMouseover"
        v-mouseover-value="customMouseenterValue"/>
    

    or

    <div
        v-mouseover="isMouseover"
        v-mouseover-value="{
            mouseenter: customMouseenterValue,
            mouseleave: customMouseleaveValue
        }"
    />
    

    Custom default values can be passed to the package via options object during setup.

    0 讨论(0)
  • 2021-01-30 02:38

    There is a correct working JSFiddle: http://jsfiddle.net/1cekfnqw/176/

    <p v-on:mouseover="mouseOver" v-bind:class="{on: active, 'off': !active}">Hover over me!</p>
    
    0 讨论(0)
  • 2021-01-30 02:41

    With mouseover and mouseleave events you can define a toggle function that implements this logic and react on the value in the rendering.

    Check this example:

    var vm = new Vue({
    	el: '#app',
    	data: {btn: 'primary'}
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    
    
    <div id='app'>
        <button
            @mouseover="btn='warning'"
            @mouseleave="btn='primary'"
            :class='"btn btn-block btn-"+btn'>
            {{ btn }}
        </button>
    </div>

    0 讨论(0)
  • 2021-01-30 02:42

    Here is a very simple example for MouseOver and MouseOut:

    <div id="app">
       <div :style = "styleobj" @mouseover = "changebgcolor" @mouseout = "originalcolor"> 
       </div>
    </div>
    
    new Vue({
      el:"#app",
      data:{
         styleobj : {
           width:"100px",
           height:"100px",
           backgroundColor:"red"
         }
      },
      methods:{
        changebgcolor : function() {
          this.styleobj.backgroundColor = "green";
        },
        originalcolor : function() {
          this.styleobj.backgroundColor = "red";
        }
      }
    });
    
    0 讨论(0)
  • 2021-01-30 02:44

    I came up with the same problem, and I work it out !

            <img :src='book.images.small' v-on:mouseenter="hoverImg">

    0 讨论(0)
  • 2021-01-30 02:45

    With mouseover only the element stays visible when mouse leaves the hovered element, so I added this:

    @mouseover="active = !active" @mouseout="active = !active"

    <script>
    export default {
      data(){
       return {
         active: false
       }
     }
    </script>
    
    0 讨论(0)
提交回复
热议问题