点击位置打开弹出窗口

时间:2012-05-08 05:13:06

标签: javascript jquery html popup mouse-position

您好,

  我做了一个弹出窗口,默认情况下隐藏并在窗口触发点击时打开。必须在触发事件的任何地方显示弹出窗口。但是有一些限制:

  1. 弹出窗口必须显示在当前可见窗口。意思是,如果我点击窗口的最右边部分,则弹出窗口必须显示在点击位置的右侧以避免滚动。

  2. 如果窗口有滚动,无论滚动如何,它都应显示在窗口的可见部分。

  3. 在我的现有代码中,一切正常,除非在窗口滚动的情况下。 如果向下滚动并单击窗口中间,则会在窗口当前显示区域外显示弹出窗口.........................

    <!DOCTYPE HTML PUBLIC>
    <html>
     <head>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
     <style>
       div{
         border:1px solid;
         background:#ff9999;
         width:500px;
         height:500px;
         display:none;
         position:absolute;
       }
     </style>
      <script>
       var mouseX,mouseY,windowWidth,windowHeight;
       var  popupLeft,popupTop;
     $(document).ready(function(){
    
       $(document).mousemove(function(e){
               mouseX = e.pageX;
               mouseY = e.pageY;
               //To Get the relative position
               if( this.offsetLeft !=undefined)
                 mouseX = e.pageX - this.offsetLeft;
               if( this.offsetTop != undefined)
                 mouseY = e.pageY; - this.offsetTop;
    
               if(mouseX < 0)
                    mouseX =0;
               if(mouseY < 0)
                   mouseY = 0;
    
               windowWidth  = $(window).width();
               windowHeight = $(window).height();
       });
    
         $('html').click(function(){
           $('div').show();
          var popupWidth  = $('div').outerWidth();
          var popupHeight =  $('div').outerHeight();
    
          if(mouseX+popupWidth > windowWidth)
            popupLeft = mouseX-popupWidth;
          else
           popupLeft = mouseX;
    
          if(mouseY+popupHeight > windowHeight)
            popupTop = mouseY-popupHeight;
          else
            popupTop = mouseY; 
          if(popupLeft < 0)
              popupLeft = 0;
          if(popupTop < 0)
              popupTop = 0;
    
          $('div').offset({top:popupTop,left:popupLeft});
         });
     });
      </script>
     </head>
    
     <body>
            <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>
             s dflasld fsadf
             sdfas dfsadf
            </div>
    
    </body>
    
    </html>
    

    请你检查一下.......

2 个答案:

答案 0 :(得分:8)

也许你可以在初始时加载你的函数中的windowW / H.

这个概念是使用鼠标滚动的高,因为mouseY与body相关。所以使用这个:

 $(document).ready(function(){

     $('html').click(function(e){
      mouseX=e.pageX;
      mouseY=e.pageY;
      var bodyTop = document.documentElement.scrollTop + document.body.scrollTop;
      ..
      //window.outerWidth is not working in IE
      var windowWidth  = $(window).outerWidth();
      var windowHeight = $(window).outerHeight();
      ..
      if(mouseY-bodyTop+popupHeight > windowHeight)
        ...
        ...
      //set the position first. remove the position attr in css   
      $('div').css({position:"absolute",top:popupTop,left:popupLeft});
      $('div').show();
     });
 });

答案 1 :(得分:6)

最后,我可以通过稍微更改来完成...这是一段可以正常工作的代码...

<html>
 <head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
 <style>
   div{
     border:1px solid;
     background:#ff9999;
     width:500px;
     height:500px;
     display:none;
     position:absolute;
   }
 </style>
  <script>
   var mouseX,mouseY,windowWidth,windowHeight;
   var  popupLeft,popupTop;
 $(document).ready(function(){

   $(document).mousemove(function(e){
           mouseX = e.pageX;
           mouseY = e.pageY;
           //To Get the relative position
           if( this.offsetLeft !=undefined)
             mouseX = e.pageX - this.offsetLeft;
           if( this.offsetTop != undefined)
             mouseY = e.pageY; - this.offsetTop;

           if(mouseX < 0)
                mouseX =0;
           if(mouseY < 0)
               mouseY = 0;

           windowWidth  = $(window).width()+$(window).scrollLeft();
           windowHeight = $(window).height()+$(window).scrollTop();
   });

     $('html').click(function(){
       $('div').show();
      var popupWidth  = $('div').outerWidth();
      var popupHeight =  $('div').outerHeight();

      if(mouseX+popupWidth > windowWidth)
        popupLeft = mouseX-popupWidth;
      else
       popupLeft = mouseX;

      if(mouseY+popupHeight > windowHeight)
        popupTop = mouseY-popupHeight;
      else
        popupTop = mouseY; 

    if( popupLeft < $(window).scrollLeft()){
     popupLeft = $(window).scrollLeft();
    }

    if( popupTop < $(window).scrollTop()){
     popupTop = $(window).scrollTop();
    }

     if(popupLeft < 0 || popupLeft == undefined)
           popupLeft = 0;
      if(popupTop < 0 || popupTop == undefined)
           popupTop = 0;

      $('div').offset({top:popupTop,left:popupLeft});
     });
 });
  </script>
 </head>

 <body>
        <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/><br/><br/><br/><br/>        

        <div>
         s dflasld fsadf
         sdfas dfsadf
        </div>

</body>

</html>