使用javascript添加Map弹窗

使用javascript添加弹窗

方式1:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="map"></div>
		<button onclick="clickStatus()">激活弹窗</button>
<script>
		
	let isInit=false;
	let addDiv = '';
	let addtitle = '';
	let addcontent = '';
	let addClose='';
	let flag=true;
	let initTool=function (frameDiv) {
		if (!isInit) {
			return 0;
		}
		//弹窗容器div
		const rightdiv = document.createElement('DIV');
		rightdiv.className = "tooltip-right";
		rightdiv.style = `
		position:absolute;
		min-width:380px;
		min-height:100px;
		max-height:400px;
		background:#f2f2f2;
		border-radius:10px;
		box-shadow: 2px 4px 5px #888888;
		`;

		//弹窗箭头div
		const arrow = document.createElement('DIV');
		arrow.className = "tooltip-arrow";
		arrow.style = `
		position:absolute;
		left:-24px;
		top:38px;
		width:0;
		height:0;
		border-top: 12px solid transparent;
		border-right: 12px solid #f2f2f2;
		border-bottom: 12px solid transparent;
		border-left: 12px solid transparent;`;
		rightdiv.appendChild(arrow);

		//标题div
		const title = document.createElement('DIV');
		title.className = "tooltip-title";
		title.style = `
		width:100%;
		height:30px;
		line-height:30px;
		font-size: small;
		text-align: left;
		border-bottom: 1px solid #777;
		padding: 0 0 0 0px;
		`
		rightdiv.appendChild(title);

		//close
		const close = document.createElement('DIV');
		close.className = "tooltip-close";
		close.style = `
		position:absolute;
		right:-5px;
		top:0px;
		width:30px;
		height:30px;
		line-height:30px;
		color: #4876ff;
		z-index:999;`
		rightdiv.appendChild(close);
		//内容div
		const content = document.createElement('DIV');
		content.className = "tooltip-content";
		content.style = `
		width:100%;
		box-sizing:border-box;
		padding:10px 0 10px 10px;
		word-break:break-all;
		`
		rightdiv.appendChild(content);

		addtitle = title;
		addcontent = content;
		addClose=close;
		addDiv = rightdiv;
		frameDiv.appendChild(rightdiv);
		isInit = true;

	}
	//控制弹窗显隐
	let setVisible=function (visible) {
		if (!isInit) {
			return 0;
		}
		addDiv.style.display = visible ? 'block' : 'none';
	}
	//控制弹窗定位
	let showAt=function (position, message, content) {
		if (!isInit) {
			return 0;
		}
		if (position && message && content) {
			setVisible(true);
			addtitle.innerHTML = message;
			addcontent.innerHTML = content;
			addClose.innerHTML = '&#10005';
			addDiv.style.left = position.x + 30 + "px";
			addDiv.style.top = (position.y - 50) + "px";
		}
	}
	let map=document.getElementById('map')
	let content=
		"<div><h5>管径:<span>" +
		'diameter' +"</span></h5></div>"+
		"<div><h5>起点号:<span>" +
		'startPoint'+"</span></h5></div>"+
		"<div><h5>起点埋深:<span>" +
		'startDeep'+"m</span></h5></div>" +
		"<div><h5>起点管线高程:<span>" +
		'starHeight'+"m</span></h5></div>"+
		"<div><h5>终点号:<span>" +
		'endPoint'+"</span></h5></div>"+
		"<div><h5>终点埋深:<span>" +
		'endDeep'+"m</span></h5></div>" +
		"<div><h5>终点管线高程:<span>" +
		'endHeight'+"m</span></h5></div>";
	let click=function(ev){
		console.log(ev);
		let position ={
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop - document.body.clientTop
		}
		showAt(position, '设施概况', content);
	}
	function clickStatus(){
		isInit = !isInit;
		console.log(isInit);
		if(isInit){
			initTool(map)
			// document.οnclick= click  //添加事件
			document.addEventListener('click',click) //添加事件
			addClose.addEventListener('click', function () {
				isInit = !isInit;
				document.removeEventListener('click', click);//移除事件
				addDiv.style.display = 'none';
			})
		}else{
			// document.onclick = null //移除事件
			document.removeEventListener('click', click);//移除事件
		}
	}

</script>
</body>
	</body>
</html>

方式2

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.ol-popup-facility {
				position: absolute;
				background-color: rgba( 255,255,255,.8 );
				-webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
				filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
				padding: 15px;
				border-radius: 10px;
				border: 1px solid #cccccc;
				bottom: 12px;
				left: -50px;
				width: 420px;
				height: 300px;
				color: #7E8A9E;
			}
			.ol-popup-facility .popup-title{
				color: #7E8A9E;
				display: inline-block;
				font:15px arial,sans-serif;
				font-weight: bolder;
				height: 26px;
				font-size: 16px;
				letter-spacing: 1px;
				line-height: 26px;
			}
	
			.ol-popup-facility .popup-close{
				position:absolute;
				right:0px;
				top:10px;
				width:30px;
				height:30px;
				line-height:30px;
				color: #4876ff;
				z-index:200;
			}
			.ol-popup-facility:after, .ol-popup-facility:before {
				top: 100%;
				border: solid transparent;
				content: " ";
				height: 0;
				width: 0;
				position: absolute;
				pointer-events: none;
			}
	
			.ol-popup-facility:after {
				border-top-color: white;
				border-width: 10px;
				left: 48px;
				margin-left: -10px;
			}
	
			.ol-popup-facility:before {
				border-top-color: #cccccc;
				border-width: 11px;
				left: 48px;
				margin-left: -11px;
			}
	
		</style>
	</head>
	<body>
		<div id="map">
			<div id="popup" class="ol-popup-facility">
				<div id="popup-close" class="popup-close">&#10005;</div>
				<div id="popup-title" class="popup-title"></div>
				<div id="popup-content" class="popup-content"></div>
			</div>
		</div>
		<button onclick="clickStatus()">激活弹窗</button>
<script>
	let map=document.getElementById('map')
	let isInit=false;
	let addDiv = document.getElementById('popup')
	let addtitle = document.getElementById('popup-title')
	let addcontent = document.getElementById('popup-content')
	let addClose=document.getElementById('popup-close')
	addDiv.style.display = 'none';
	//控制弹窗显隐
	let setVisible=function (visible) {
		if (!isInit) {
			return 0;
		}
		addDiv.style.display = visible ? 'block' : 'none';
	}
	//控制弹窗定位
	let showAt=function (position, message, content) {
		if (!isInit) {
			return 0;
		}
		if (position && message && content) {
			setVisible(true);
			addtitle.innerHTML = message;
			addcontent.innerHTML = content;
			addClose.innerHTML = '&#10005';
			addDiv.style.left = position.x + 30 + "px";
			addDiv.style.top = (position.y - 50) + "px";
		}
	}
	let content=
		"<div><h5>管径:<span>" +
		'diameter' +"</span></h5></div>"+
		"<div><h5>起点号:<span>" +
		'startPoint'+"</span></h5></div>"+
		"<div><h5>起点埋深:<span>" +
		'startDeep'+"m</span></h5></div>" +
		"<div><h5>起点管线高程:<span>" +
		'starHeight'+"m</span></h5></div>"+
		"<div><h5>终点号:<span>" +
		'endPoint'+"</span></h5></div>"+
		"<div><h5>终点埋深:<span>" +
		'endDeep'+"m</span></h5></div>" +
		"<div><h5>终点管线高程:<span>" +
		'endHeight'+"m</span></h5></div>";
	let click=function(ev){
		console.log(ev);
		let position ={
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop - document.body.clientTop
		}
		showAt(position, '设施概况', content);
	}
	function clickStatus(){
		isInit = !isInit;
		console.log(isInit);
		if(isInit){
			// document.οnclick= click  //添加事件
			document.addEventListener('click',click) //添加事件
			addClose.addEventListener('click', function () {
				isInit = !isInit;
				document.removeEventListener('click', click);//移除事件
				addDiv.style.display = 'none';
			})
		}else{
			addDiv.style.display = 'none';
			// document.onclick = null //移除事件
			document.removeEventListener('click', click);//移除事件
		}
	}

</script>
</body>
	</body>
</html>

方式1效果

在这里插入图片描述

方式2效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不秃头的LT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值