jquery让弹出层居中

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'move.jsp' starting page</title>
   
 <script type="text/javascript" src="script/jquery-1.7.1.js"></script>
<script type="text/javascript">
 $(function(){
 $("#show1").click(function(){
      $("#showdiv1").css({top:($(window).height()-100)/2+$(window).scrollTop()+'px',left:($(window).width()-300)/2+document.documentElement.scrollLeft+'px'});
      $("#showdiv1").show();
      $("#showdiv2").hide();
      return false;
    });
  
 var menuYloc = $("#showdiv1").offset().top; 
        $(window).scroll(function (){ 
            var offsetTop = menuYloc + $(window).scrollTop(); 
            $("#showdiv1").animate({top : (offsetTop+($(window).height()-100)/2 )+"px"},{ duration:600 , queue:false }); 
        });
        //
        $("#show2").click(function(){
      $("#showdiv2").css({top:($(window).height()-100)/2+$(window).scrollTop()+'px',left:($(window).width()-300)/2+document.documentElement.scrollLeft+'px'});
      $("#showdiv2").show();
      $("#showdiv1").hide();
      return false;
    });
        $("#showdiv2").mousedown(function(e)//e鼠标事件 
        { 
            $(this).css("cursor","move");//改变鼠标指针的形状 为移动
             
            var offset = $(this).offset();//DIV在页面的位置 
            var x = e.pageX - offset.left;//获得鼠标指针离DIV元素左边界的距离 
            var y = e.pageY - offset.top;//获得鼠标指针离DIV元素上边界的距离 
            $(document).bind("mousemove",function(ev)//绑定鼠标的移动事件,因为光标在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件 
            { 
                $("#showdiv2").stop();//加上这个之后 
                 
                var _x = ev.pageX - x;//获得层X方向的值ev.pageX为鼠标的坐标
                var _y = ev.pageY - y;//获得层Y方向的值
                 
                $("#showdiv2").animate({left:_x+"px",top:_y+"px"},10); 
            }); 
             
        }); 
         
        $(document).mouseup(function() 
        { 
            $("#showdiv2").css("cursor","default"); 
            $(this).unbind("mousemove"); 
        }) 
       
       
 });
 
 
</script>
  </head>
 
  <body>
  <h3><a href="kuang.jsp">go</a></h3>
   <h3><a href="#" id="show1"> 弹出居中的自动Move的层</a></h3>
   <h3><a href="#" id="show2"> 弹出居中的手动Move的层</a></h3>
     <!-- 显现层 -->
     <div id="showdiv1" style="z-index: 1000;width: 300px;height: 100px;background-color: blue;position: absolute;display: none "><iframe  name="test"  src="/index.jsp"></iframe></div>
     <div id="showdiv2" style="z-index: 1000;width: 300px;height: 100px;background-color: blue;position: absolute;display: none "></div>
    <div style="z-index: 100;width: 900px;height: 900px; "></div>
  </body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值