关于窗口高度于浏览器高度问题

本文介绍了一种使用JavaScript实现的鼠标右键点击时显示弹窗的方法,并详细解释了如何根据不同的屏幕位置调整弹窗出现的位置以避免超出浏览器边界。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.shi{
width:200px;
height:200px;
background:yellow;
box-shadow: #ccc 0px 0px 10px;
position:absolute;
display: none;
}
</style>
</head>
<body>
<div class="shi">
<p>若不是你突然闯进 我生活</p>
<p>我怎会把死守的寂寞 放任了</p>
</div>
<script>
window.oncontextmenu = function ()
{
return false;
}
document.onmousedown = function(e){
let mouse = document.querySelector('.shi');
let winHeight = document.documentElement.clientHeight; //////窗口高度
let winWidth = document.documentElement.clientWidth; //////窗口宽度
let mouseHeight = e.clientY; ////鼠标距离浏览器高度
let mouseLeft = e.clientX; /////鼠标距离浏览器左侧宽度
if(e.button == '2'){
mouse.style.display = 'block';
if(winHeight - mouseHeight < 200){
mouse.style.left = mouseLeft - 200 + 'px';
mouse.style.top = mouseHeight-200 + 'px';
}else if(winWidth - mouseLeft < 200){
mouse.style.top = mouseHeight + 'px';
mouse.style.left = mouseLeft - 200 + 'px'
}else if(winWidth - mouseLeft > 200){
mouse.style.left = mouseLeft+'px';
mouse.style.top = mouseHeight+'px';
}else if(mouseHeight > 0 && mouseHeight < 200){
mouse.style.left = mouseLeft - 200 + 'px';
mouse.style.top = mouseHeight - 200 + 'px';
}
}else{
console.log(winHeight,mouseHeight);
}
}
</script>
</body>
</html>
上下左右都不会有bug的点击弹窗

 

转载于:https://www.cnblogs.com/MDGE/p/10639098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值