jq弹窗(获取页面宽高,滚轮高度,始终居中)

jq写一个弹窗,效果如上图所示,

点击按钮弹窗弹出,右上角关闭。

弹窗始终显示在页面中间,无论放大缩小窗口,滚轮滚动。

代码如下:

html:

<br><br><br><br>
<button>弹出</button>
<div id="tanchuang">
	<span id="close">×</span>
</div>
<br><br><br>

 js:

$(function(){
	//定义页面宽度,页面高度,弹窗位置left,弹窗位置top,滚动条高度
	var screenWidth,screenHeight,tcleft,tctop,scrollTop;
	//计算弹窗位置的函数
	tanLocation();
	//按钮添加点击事件,调用方法show(),使弹窗div出现
	$('button').click(function(){
		$('#tanchuang').show();
	})
	//关闭按钮添加点击事件,调用方法hide(),使弹窗div消失
	$('#close').click(function(){
		$('#tanchuang').hide();
	})
	//窗口对象添加resize() 当浏览器窗口大小改变时执行。
	$(window).resize(function(){
		tanLocation();
	})
	//文档对象添加scroll() 当滚轮高度变化时执行
	$(document).scroll(function(){
		tanLocation();
	})
})
//计算弹窗位置的函数
function tanLocation(){
	//获取页面宽度
	screenWidth = $(window).width();
	//获取页面高度
	screenHeight = $(window).height();
	//计算left值
	tcleft = (screenWidth-100)/2;
	//计算top值
	tctop = (screenHeight-100)/2;
	//获取滚轮高度
	scrollTop = $(document).scrollTop();
	//弹窗的位置样式改变
	$('#tanchuang').css({'top':tctop+scrollTop,'left':tcleft});
}

 

转载于:https://www.cnblogs.com/SSs1995/p/9210533.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值