mouseOver和mouseLeave事件在vue中的应用

目标:利用mouseOver和mouseLeave实现鼠标移入和移出时的不同效果。

初始状态:

 点击最右侧按钮后的效果:(具体实现方法见下篇文章)

 要完成的效果:在“已完成编辑 √”状态时,鼠标移入,出现如下效果,移出时恢复

 具体实现:

template中:

<div class="resultType " ref="paintOne" :style="activeOne">
                        <span class="resultTypeTitle">我的偶像</span>
                        <span class="paintWord" v-show="isShowPaintWordOne">
                            我的偶像是千玺
                        </span>
                        <span
                            class="paintWordFinish"
                            @mouseleave="mouseLeaveOne"
                            @mouseover="mouseOverOne"
                            v-show="isShowPaintWordFinishOne"
                        >
                            偶像最棒&nbsp;&nbsp;&nbsp;√
                        </span>
                        <img
                            src="./theme/img/editWhite.png"
                            @click="finishPaintOne"
                            v-show="isShowPaintWordOne"
                        />
                        <img
                            src="./theme/img/editBlue.png"
                            @click="finishPaintRecoverOne"
                            v-show="isShowPaintWordFinishOne"
                        />
</div>
     <div class="resultTypeContent" ref="contentOne">
                    他是易烊千玺,他很棒!
     </div>

script的data中:

export default {
    data() {
        return {
            activeOne:'',
               }
            }
          }

script的methods中:

 mouseOverOne() {
       this.activeOne =
      'border: 1px solid rgba(107,195,255,0.80); box-shadow: #1a446a 0px 0px 30px inset;';
       // 操作dom 获取p标签改变其样式
       var paintOne = this.$refs.paintOne;
        this.$refs.contentOne.style.display = 'block';
        },
        // 鼠标移出
        mouseLeaveOne() {
            this.activeOne = '';
            this.$refs.paintOne.style = '';
            this.$refs.contentOne.style.display = 'none';
        },

样式就不展开写了。

这样就利用mouseOver和mouseLeave实现了鼠标移入和移出时的不同效果。

以下是一个简单的评分组件,使用Vue实现了click、mouseovermouseleave功能: ```html <template> <div class="rating"> <span v-for="(star, index) in stars" :class="{ 'filled': index < rating }" @click="selectRating(index + 1)" @mouseover="hoverRating(index + 1)" @mouseleave="resetRating()"> <i class="fa fa-star"></i> </span> </div> </template> <script> export default { name: 'Rating', data() { return { rating: 0, stars: [1, 2, 3, 4, 5] } }, methods: { selectRating(rating) { this.rating = rating }, hoverRating(rating) { this.rating = rating }, resetRating() { if (this.rating === 0) { return } this.rating = 0 } } } </script> <style> .rating { display: inline-block; font-size: 1.5rem; } .fa-star { color: #ccc; cursor: pointer; } .filled { color: #ff9800; } </style> ``` 在上面的代码中,我们使用了一个stars数组来表示五颗星星。在模板中,我们使用了v-for指令来循环这个数组,并为每个星星添加了一个click、mouseovermouseleave事件监听器。 我们还定义了三个方法:selectRating、hoverRatingresetRating。selectRating方法用于在用户点击星星时设置评分,hoverRating方法用于在用户将鼠标移动到星星上时实时更新评分,resetRating方法用于在用户将鼠标移出星星时重置评分。 最后,我们还定义了一些样式来美化评分组件。在样式中,我们使用了FontAwesome图标库中的fa-star图标来表示星星。我们还使用了CSS伪类选择器来为已选中的星星添加填充颜色。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值