获取鼠标位置和区块在页面始终居中

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 2     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 <html>
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 6 <title>dom</title>
 7 <style>
 8 * {
 9     margin:0;
10     padding:0;
11 }
12 .toTop {
13     display:inline-block;
14     width:500px;
15     height:400px;
16     background-color:#969;
17     fond-size:52px;
18     line-height:400px;
19     text-align:center;
20     color:#fff;
21     cursor:pointer;
22 }
23 </style>
24 </head>
25 <body>
26     <div></div>
27     <a class="toTop">上</a>
28 <script src="../../../../js/jquery.min.js"></script>
29 <script>
30     $(function(){
31         $(document).mousemove(function(e){
32             $('div').html('X:'+e.pageX+' | Y:'+e.pageY);
33         });
34         jQuery.fn.center = function(){
35             $(this).css({
36                 'position':'absolute',
37                 'top':($(window).height()-$(this).height())/2+$(window).scrollTop()+'px',
38                 'left':($(window).width()-$(this).width())/2+$(window).scrollLeft()+'px',    
39             })    
40         };
41         $('a').center();
42     });
43 </script>
44 </body>
45 </html>

 

posted on 2016-05-29 01:30  jasonduanmu  阅读(292)  评论(0编辑  收藏  举报

导航