vue里实现鼠标点击哪里div定位在哪里

定义个定位的div,需要绑定style

	<div v-if='showBox'>
		<div class='show-parent' :style="{left: tranLeft, top: tranTop}">
			<div class="shard">
	          <div class="show-inner">
	            <div>申请次数</div>
	            <div>1天内</div>
	            <div>7天内</div>
	          </div>
	        </div>
		</div>
	</div>
	<span class='show' @click='show'></span>
	export default {
		data() {
			return {
				showBox: false,
				tranLeft: 0// 向左偏移
				tranTop: 0  // 向右偏移
			}
		},
		methods: {
			show(e) {
				// 根据事件处理参数e下的pageX,PageY来定位div的位置
				// 需要计算下div定位的差值 我这边是横轴是500左右纵轴是163左右
				this.tranLeft = (e.pageX - 500) + 'px'
				this.tranTop = (e.pageY - 163) + 'px'
				// 点击的时候显示模态框
				this.showBox = true	
			}	
		}
	}
	.show-parent {
    position: absolute;
    /*top: 587px;
    left: 933px;*/
    width: 300px;
    background-color: #ffffff;
    box-shadow: 0 0 2px #8c939d;
    z-index: 1;
  }
  
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以通过给input添加一个事件监听器,当鼠标进入input区域时,改变其后面的div的背景颜色,当鼠标移出input区域时,再将其背景颜色恢复原状。以下是一个示例代码: HTML部分: ```html <div id="app"> <div v-for="(item, index) in items" :key="index"> <input type="text" @mouseover="changeBgColor(index, true)" @mouseout="changeBgColor(index, false)"> <div :class="{ 'bg-color': showBg && showIndex === index }"> {{ item }} </div> </div> </div> ``` Vue部分: ```javascript new Vue({ el: '#app', data: { items: ['item1', 'item2', 'item3'], // 待循环的数据 showBg: false, // 是否显示背景颜色 showIndex: null // 当前鼠标所在的input的下标 }, methods: { // 改变背景颜色的方法,index表示当前input的下标,isShow表示是否显示背景颜色 changeBgColor(index, isShow) { this.showBg = isShow; this.showIndex = index; } } }) ``` CSS部分: ```css .bg-color { background-color: #ccc; } ``` 在上面的示例代码中,我们通过遍历数据循环出多个input和对应的div,给input添加了两个事件监听器,当鼠标进入时,将showBg标记为true,并将showIndex赋值为当前input的下标;当鼠标移出时,将showBg标记为false,这样就可以在Vue中根据showBg和showIndex的值来决定是否显示div的背景颜色。同时我们还通过:class绑定了背景颜色样式,并在CSS中设置了该样式的背景颜色。 希望这个回答可以帮您解决问题,如果还有其他问题可以继续提问哦~

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值