Bug Tracker

Changes between Initial Version and Version 4 of Ticket #7774


Ignore:
Timestamp:
Dec 14, 2010, 7:18:52 AM (9 years ago)
Author:
jitter
Comment:

Thanks for reporting back with and providing a test case!

This isn't a bug in jQuery. The offset() documentation states

jQuery does not support getting the offset coordinates of hidden elements.

which explains your problems. That it works the first time is only a "coincidence" because at that time top/left are 0. Just change .ContactPanel top/left to something different from 0px and then it also won't work on the first click.

The correct way to go about this is to use .css()

working test case based on your code

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #7774

    • Property Owner set to mrbbljc@…
    • Property Status changed from new to closed
    • Property Resolution changed from to invalid
  • Ticket #7774 – Description

    initial v4  
    1 $("#ContactBtn").bind("click",function(evt){       
    2         var offset = $(this).offset();
    3         var newOff = {};
    4         newOff.left = offset.left - 250;
    5         newOff.top = offset.top - 350;//
    6         $("#ContactPanel").offset(newOff).show();
    7 }); 
    8 
    9 when I click ContactBtn,first it show,but when I use        $("#ContactBtn").hide();
     1{{{
     2$( "#ContactBtn" ).bind( "click", function(evt) {
     3    var offset = $(this).offset(),
     4        newOff = {};
     5    newOff.left = offset.left - 250;
     6    newOff.top = offset.top - 350;//
     7    $( "#ContactPanel" ).offset( newOff ).show();
     8});
     9}}}
     10when I click ContactBtn,first it show,but when I use `("#ContactBtn").hide()`
    1011It must click twice to show the div panel;
    1112I'm not sure it's a bug;
    1213But,
    13     $("#ContactBtn").bind("click",function(evt){       
    14         var x,y;       
    15         if(typeof window.event != 'undefined'){
    16             x = evt.clientX;
    17             y = evt.clientY;
    18         }else{
    19             x = evt.pageX;
    20             y = evt.pageY;
    21         }
    22        
    23         $("#ContactPanel").css("top",y-350+"px")
    24                           .css("left",x-250+"px")
    25                           .show();
    26      });   
     14{{{
     15$( "#ContactBtn" ).bind( "click", function(evt) {
     16    var x, y;
     17    if( typeof window.event != 'undefined' ){
     18        x = evt.clientX;
     19        y = evt.clientY;
     20    } else {
     21        x = evt.pageX;
     22        y = evt.pageY;
     23    }
     24
     25    $( "#ContactPanel" ).css( "top", y - 350 + "px" )
     26        .css( "left", x - 250 + "px" )
     27        .show();
     28});
     29}}}
    2730It's work;
    2831I'm sorry my poor English;