jquery实现居中、左下角、右下角窗口效果

jquery_windows.html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>弹出窗口</title>
<link type="text/css" rel="stylesheet" href="css.css">
<script src="../../jquery/jquery-1.7.1.js"></script>
<script src="window.js"></script>
<script>
$(document).ready(function (){
//利用toggle方法实现显示和隐藏
$('#btn_center').click(function (){

	$(window).scroll(function (){
		popCenterWindow();
			});
	});
	
$('#btn_left').click(function (){
	$(window).scroll(function (){
		popLeftButtomWindow();
			});
	});
$('#btn_right').click(function (){
	$(window).scroll(function (){
		popRightButtomWindow();
		});
		
	});

});
</script>

</head>

<body>
<input type="button" value="弹出中间窗口" id="btn_center"/>
<input type="button" value="弹出居左下角窗口" id="btn_left"/>
<input type="button" value="弹出居右下角窗口" id="btn_right"/>
<br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br>
<div id="center" class="window">
        <div class="title">csdn欢迎你<img width="15px" height="15px" src="../../images/clone.png"></div>
        <div class="content">费水电费水电费你</div>
</div>
<div  id="left" class="window">
        <div class="title">csdn欢迎你<img width="15px" height="15px" src="../../images/clone.png"></div>
        <div class="content">费水电费水电费你</div>
</div>
<div  id="right" class="window">
        <div class="title">csdn欢迎你<img width="15px" height="15px" src="../../images/clone.png"></div>
        <div class="content">费水电费水电费你</div>
</div>
</body>
</html>

window.js文件

// JavaScript Document

//定义一些变量
//获取窗口的高度
var windowHeight;
//获得窗口的宽度
var windowWidth;
//获得弹窗的高度
var popHeight;
//获得弹窗的宽度
var popWidth;
//获取滚动条的高度
var scrollTop;
var scrollLeft;
//延迟时间
var timeout;
function init(){

 windowHeight=$(window).height();

 windowWidth=$(window).width();

 popHeight=$(".window").height();

 popWidth=$(".window").width();

 scrollTop=$(window).scrollTop();
 scrollLeft=$(window).scrollLeft();
}

//关闭窗口的方法
function closeWindow(){
	//根据div找到x号的图片,加单击事件,并且关闭窗口
	$('.title img').click(function (){
		$(this).parent().parent().hide('slow');
		});
	
	}


//定义弹出居中窗口的方法
function popCenterWindow(){
    clearTimeout(timeout);
	timeout=setTimeout(function (){
	init();
	//计算弹出窗口的左上角y的偏移量
	var popY=(windowHeight-popHeight)/2+scrollTop;
	var popX=(windowWidth-popWidth)/2+scrollLeft;
	//设定窗口的位置
	$('#center').animate({top:popY,left:popX},300).show('slow');
	},300);
	//调用关闭窗口
	closeWindow();
	
	}
//定义弹出左下角的效果
function popLeftButtomWindow(){
	clearTimeout(timeout);
	timeout=setTimeout(function (){
	//首先要初始化参数
	init();
	var popY=(windowHeight+scrollTop-popHeight);
	var popX=scrollLeft;
	//设定窗口的位置
	$('#left').animate({top:popY,left:popX},300).show('slow');
	},300);
	//调用关闭窗口
	closeWindow();

	}


//定义弹出右下角窗口的效果
function popRightButtomWindow(){
	//先清空上一次延迟
	clearTimeout(timeout);
	timeout=setTimeout(function (){
	//首先要初始化参数
	init();
	var popY=windowHeight+scrollTop-popHeight-10;
	var popX=windowWidth+scrollLeft-popWidth-10;
	//设定窗口的位置
	//$('#center').css('top',popY).css('left',popX).show('slow');
	
	$('#right').animate({top:popY,left:popX},300).show('slow');
		},300);
	//调用关闭窗口
	closeWindow();

	}


css.css文件

@charset "utf-8";
/* CSS Document */

.window{
	width:250px;
	background-color:#6FF;
	padding:2px;
	margin:5px;
	position:absolute;
	display:none;}
.content{
		height:150px;
		background-color:#FFF;
		padding:2px;
		overflow:auto;
		}
.title{
	padding:2px;
	color:#699;
	font-size:14px;
	}
.title img{
	cursor:pointer;
	float:right;
	}


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值