判断鼠标从div的哪一方向划入-------Day74

本文介绍了一种基于鼠标位置判断遮盖层显示方向的交互效果。通过计算鼠标相对于目标div四边的距离,确定遮盖层出现的方向,并利用JavaScript实现了这一效果。

发现一种很有意思的效果,感觉很漂亮很华丽,想实现一下,大体效果来介绍下:

鼠标从左端划入,则遮盖层从左边划入遮盖div,而从上方划入,则遮盖层从上方划入遮盖div,同理对于右方和下方,而同样从哪个方向鼠标移出div,遮盖层也就从哪个方向消失,你能想象的出么?如果还想象不出效果,可以看一下“拉勾网”,我就从上面发现的这个效果。

其实单纯说遮盖层划出的效果,我们大约知道好几种方法,如果遮盖层是需要新创建div,那么javascript完全可以实现,至于划出效果也可以实现,遮盖层的width从0不断增大到div的宽度即可,而如果遮盖层是之前隐藏的div,那么除了用javascript方法,我们还可以用css3的transition的width来实现效果,然后我们就遇到了一个当前的大问题,那就是判定鼠标从哪个方向划入,然后遮盖层从哪个方向划出。

而我想到的方法,就是鼠标划入的初始位置,距离div的哪一个边最近,我们就知道从哪一个方向划入的了,是不是这个道理呢,用代码来实现下:

html语言:

<div style="margin-left:300px;margin-top:300px;width:400px;height:400px;background:cyan;" id="test">
		<img src="11.jpg" width="300px" height="250px">
</div>
js部分:

window.onload=function(){
                var left=0;
                var top=0;
		var right=0;
		var bottom=0;
                var arr=new Array();
                test.addEventListener("mouseover",function(event){
			var test=document.getElementById("test");//获得test对象
			if(check){
				check=false;
				var x=event.clientX;//鼠标的位置
				var y=event.clientY;
				left=x-test.offsetLeft;//鼠标相对于div左方的边的距离
				top=y-test.offsetTop;//相对于div上方的边的距离
				right=test.offsetLeft+test.offsetWidth-x;//相对于div右方的边的距离
				bottom=test.offsetTop+test.offsetHeight-y;//相对于div下方的边的距离
				arr.push(top);
				arr.push(right);
				arr.push(bottom);
				arr.push(left);
				var least=findLeast(arr);
				alert(least);//获取最小是数组的第几位,1、2、3、4位分别是左、上、右、下,就能判定是哪个方向了
}
	var findLeast=function([a1,a2,a3,a4]){
			var a;
			var n;
			a=a1>a2?a2:a1;
			n=a==a2?2:1;
			a=a3>a?a:a3;
			n=a==a3?3:n;
			a=a4>a?a:a4;
			n=a==a4?4:n;
			return n;
		}
}
这样是不是就达到目的了呢,再来想想该怎么继续实现这个效果呢,多写几个if应该可以把,今天先到这里吧,明天试试看




<template> <div> <baidu-map id="allmap" :center="center" :zoom="zoom" @ready="handler" style="height:9.45rem;width: 100%;" @click="getClickInfo" :scroll-wheel-zoom='true'> <div class="time-filter"> <el-date-picker v-model="startTime" type="datetime" placeholder="开始时间" format="yyyy-MM-DD HH:mm:ss" value-format="yyyy-MM-DD HH:mm:ss" /> <el-date-picker v-model="endTime" type="datetime" placeholder="结束时间" format="yyyy-MM-DD HH:mm:ss" value-format="yyyy-MM-DD HH:mm:ss" /> <el-button type="primary" @click="searchConfirm">查询</el-button> </div> <div class="drawing-panel" style="z-index: 99;"> <button class = "btn" @click='play(1)' size="small">播放</button> <button class = "btn" @click='play(2)' size="small">暂停</button> <button class = "btn" @click='play(3)' size="small">停止</button> </div> </baidu-map> </div> </template> <script> export default { name: 'BtAddrBaiDu', data () { return { isMapReady: false, BMapObj: null, mapObj: null, startTime: null, endTime: null, center: {lng: 114.085947, lat: 22.547}, zoom: 15, point:"", gpsArr:[ { lng:114.000069, lat:22.500069 }, ], speed:500, lushu:{}, batteryId:"电池编号:"+this.$parent.$children[0].hwid, myMap: {} } }, mounted(){ }, methods: { searchConfirm() { this.loadMapData(); }, handler({BMap, map}) { this.BMapObj = BMap; this.mapObj = map; this.isMapReady = true; this.loadMapData(); }, loadMapData() { if (!this.isMapReady) return; this.$http({ url: this.$http.adornUrl('/mqtt/mtgpslocationinfo/queryGpsLocationInfos'), method: 'get', params: this.$http.adornParams({ 'thingid': this.$parent.$children[0].hwid, 'startTime': this.startTime, 'endTime': this.endTime }) }).then(({data}) => { const BMap = this.BMapObj; const map = this.mapObj; map.enableScrollWheelZoom(new BMap.Point(114.085947, 22.547), 15); map.centerAndZoom(); var arrPois = []; if (data && data.code === 0) { const socitem = data.data if (socitem && socitem.length > 0) { let clng = socitem[socitem.length-1].longitude; let clat = socitem[socitem.length-1].latitude; this.center = {lng: clng, lat: clat}; var myIcon = new BMap.Icon( require("../../../assets/img/markers.png"), new BMap.Size(23, 25), { offset: new BMap.Size(10, 25), imageOffset: new BMap.Size(0, 0 - 11 * 25) }); for (let i = 0; i < socitem.length; i++) { var p0 = socitem[i].longitude; var p1 = socitem[i].latitude; var point = new BMap.Point(p0, p1); arrPois.push(point); var distance = this.getDistance(clat,clng, p1,p0); var content = "<table style='color: #FFF;'>"; content = content + "<tr><td> 电池编号:"+socitem[i].thingId+"</td></tr>"; content = content + "<tr><td> 经纬度:"+socitem[i].longitudedirectionValue+socitem[i].longitude+","+socitem[i].latitudedirectionValue+socitem[i].latitude+"</td></tr>"; content = content + "<tr><td> 与最新点距离:"+distance.toFixed(2)+"m</td></tr>"; content = content + "<tr><td> 时间:"+socitem[i].uploadTime+"</td></tr>"; content += "</table>"; var marker = this.createMarker( point, content, socitem[i].thingId, i, myIcon ); map.addOverlay(marker) var circle = new BMap.Circle(point, 6, { strokeColor: 'Red', strokeWeight: 8, strokeOpacity: 2, Color: 'Blue', fillColor: '#f03' }) map.addOverlay(circle) } } else { console.log('没有数据的显示') this.soctitle = '暂无数据显示' } } else { console.log('没有数据的显示') this.noneTitle = '没有数据~' } var sy = new BMap.Symbol(BMap_Symbol_SHAPE_BACKWARD_OPEN_ARROW, { scale: 0.3, strokeColor:'#fff', strokeWeight: 2 }); var icons = new BMap.IconSequence(sy, '100%', '5%',false); var polyLine = new BMap.Polyline(arrPois, { strokeWeight: 2, strokeOpacity: 0.5, strokeColor:"#303133", enableEditing: false, enableClicking: false, icons:[icons] }); map.addOverlay(polyLine); BMapLib.LuShu.prototype._move = function(initPos, targetPos, effect) { var pointsArr = [initPos, targetPos]; //点数组 var me = this, currentCount = 0, timer = 10, step = this._opts.speed / (1000 / timer), init_pos = this._projection.lngLatToPoint(initPos), target_pos = this._projection.lngLatToPoint(targetPos), count = Math.round(me._getDistance(init_pos, target_pos) / step); this._map.addOverlay( new BMap.Polyline(pointsArr, { strokeColor: "#111", strokeWeight: 3, strokeOpacity: 0.5 }) ); if (count < 1) { me._moveNext(++me.i); return; } me._intervalFlag = setInterval(function() { if (currentCount >= count) { clearInterval(me._intervalFlag); if (me.i > me._path.length) { return; } me._moveNext(++me.i); } else { currentCount++; var x = effect(init_pos.x, target_pos.x, currentCount, count), y = effect(init_pos.y, target_pos.y, currentCount, count), pos = me._projection.pointToLngLat(new BMap.Pixel(x, y)); if (currentCount == 1) { var proPos = null; if (me.i - 1 >= 0) { proPos = me._path[me.i - 1]; } if (me._opts.enableRotation == true) { me.setRotation(proPos, initPos, targetPos); } if (me._opts.autoView) { if (!me._map.getBounds().containsPoint(pos)) { me._map.setCenter(pos); } } } me._marker.setPosition(pos); me._setInfoWin(pos); } }, timer); }; map.addControl(new BMap.OverviewMapControl({isOpen:true,anchor:BMAP_ANCHOR_BOTTOM_RIGHT})); map.addControl(new BMap.NavigationControl()); map.addControl(new BMap.ScaleControl({anchor:BMAP_ANCHOR_TOP_LEFT})); map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_SATELLITE_MAP],anchor: BMAP_ANCHOR_TOP_RIGHT})); //负责切换地图类型的控件 this.initLushu(map, arrPois, this.batteryId); }) }, getDistance(lat1,lng1,lat2,lng2) { let EARTH_RADIUS = 6378.137; let radLat1 = this.rad(lat1); let radLat2 = this.rad(lat2); let a = radLat1 - radLat2; let b = this.rad(lng1) - this.rad(lng2); let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.round(s * 10000) / 10000; s = s*1000; //乘以1000是换算成米 return s; }, rad(d){ return d * Math.PI / 180.0; }, initLushu(map, arrPois, batteryId) { if (typeof BMapLib === 'undefined') { console.error('BMapLib not loaded'); return; } this.lushu = new BMapLib.LuShu(map, arrPois, { defaultContent: batteryId, // autoView: true, //是否开启自动视野调整,如果开启那么路书在运动过程中会根据视野自动调整 icon: new BMap.Icon( require("../../../assets/img/mybike2.png"), new BMap.Size(52, 26), { anchor: new BMap.Size(27, 13) } ), speed: this.speed, enableRotation: true, //是否设置marker随着道路的走向进行旋转 landmarkPois: [ { lng:114.52785300795644, lat:38.14758574827796, html: "加油站", pauseTime: 2 } ] }); }, play(val) { // 动图播放 if (this.arrIndex >= this.gpsArr.length - 3) { this.arrIndex = 0; } switch (val) { case 1: this.lushu.start(); break; case 2: this.lushu.pause(); break; case 3: this.lushu.stop(); this.arrIndex = 0; this.percentage = 0; break; } }, //创建maeker和infowindow //传入的参数为:坐标,弹框中要显示的信息,厂区号 createMarker(point, iw, paramNumber,i,myIcon) { var markerx = new BMap.Marker(point); //序号为1.2.3.4.....,根据需求更改 var label = new BMap.Label(i+1, { offset:new BMap.Size(-3,-5)}); label.setStyle({ background:'url()', color:'#fff', border:'none', fontSize:'12px', textAlign:'center', width:'25px', height:'25px', lineHeight:'25px' }); markerx.setLabel(label) //将序号放入标记中 markerx.setZIndex(9999) //在路线之上 覆盖路线 var opts = { width: 300, // 信息窗口宽度 height: 180, // 信息窗口高度 title: '<h4>坐标信息'+'</h4>', // 信息窗口标题 enableMessage: true, //设置允许信息窗发送短息 message: "" } //鼠标停留发生的事件 markerx.addEventListener("mouseover", function(e) { this.openInfoWindow(new BMap.InfoWindow(iw,opts)); }); //鼠标划出发生的事件 markerx.addEventListener("mouseout", function(e) { this.closeInfoWindow(new BMap.InfoWindow(iw)); }); //点击标记发生的事件,暂不做特殊处理 var that = this; markerx.addEventListener("click", function(e) { }); return markerx; } }} </script> <style > /* 弹出框中标题样式 */ .title { color: darkgreen; text-align: center; height: 0.125rem; } /* 弹出框中提示“无数据”的样式 */ .nodate { color: dimgrey; text-align: center; } /* 弹出框底部的点击提示样式 */ .reminder { font-size: 0.15rem; color: darkcyan; margin-left: 1.25rem; } .btn { width: 1.125rem; height: 0.375rem; float: left; background-color: #fff; color: rgba(27, 142, 236, 1); font-size: 0.175rem; border:0.0125rem solid rgba(27, 142, 236, 1); border-radius: 0.0625rem; margin: 0 0.0625rem; text-align: center; line-height: 0.375rem; } .btn:hover { background-color: rgba(27, 142, 236, 0.8); color: #fff; } .drawing-panel { z-index: 999; position: fixed; bottom: 1.3rem; margin-left: 0.3rem; padding: 0.12rem 0.12rem; border-radius: .05rem; background-color: #fff; box-shadow: 0 0.025rem 0.075rem 0 rgba(27, 142, 236, 0.5); } .time-filter{ z-index: 999; position: fixed; top: 1.8rem; margin-left: 7rem; padding: 0.12rem 0.12rem; border-radius: .05rem; } .BMap_bubble_title { color: white; font-size: 0.1625rem; font-weight: bold; text-align: left; padding-left: 0.0625rem; padding-top: 0.0625rem; border-bottom: 0.0125rem solid gray; background-color: #409EFF; } /* 消息内容 */ .BMap_bubble_content { background-color: rgba(40, 40, 40, 0.8); padding-left: 0.0625rem; padding-top: 0.0625rem; padding-bottom: 0.125rem; border-bottom-left-radius: 0.1rem; border-bottom-right-radius: 0.1rem; } /* 内容 */ .BMap_pop div:nth-child(9) { top: 0.4375rem !important; border-radius: 0.0625rem; } .BMap_pop div:nth-child(5) { display: none; } /* 左上角删除按键 */ .BMap_pop img { display: none; } .BMap_top { display: none; } .BMap_bottom { display: none; } .BMap_center { display: none; } /* 隐藏边角 */ .BMap_pop div:nth-child(1) div { display: none; } .BMap_pop div:nth-child(3) { display: none; } .BMap_pop div:nth-child(7) { display: none; } </style>为什么第次加载后端接口没有带上时间字段
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值