开发者

How to solve - DIV element flashes/disappears immediately after it is rendered

开发者 https://www.devze.com 2022-12-13 07:30 出处:网络
Here\'s what is happening After the page loads, javascript reads an XML from the underlying code The XML contains a bunch of field-ids, and corresponding content to display in popups when the mouse

Here's what is happening

  • After the page loads, javascript reads an XML from the underlying code
  • The XML contains a bunch of field-ids, and corresponding content to display in popups when the mouse hovers over a field id listed

My code generates a bunch of popups as div-elements with the styles

.render{
 background-color:    #fffc80;
 border:             .1em solid rgb(200, 128, 0);
 padding-left:        2px;
 padding-right:       2px;
 z-index:             1000;
}

.hide{
 display:none;
}

All created popups are attached to the root element.

EDITED: Added script snippets

The event handlers are attached as below

// instantiate a div element

var myDiv = document.createElement('div');

// generate an ID 

myDiv.id = generatePopupId(getFieldId());

// attach the content from the XML into the new div element

myDiv.innerHTML = getPopupContent();

/开发者_如何学C/ apply mouseover/out handlers to the main element

document.getElementById(getFieldId()).onmouseover = function(){
  showPopup(generatePopupId(getFieldId()));
};

document.getElementById(getFieldId()).onmouseout = function(){
  hidePopup(generatePopupId(getFieldId()));
}; 


// read the X coordinate of the present position of the mouse

function getX(){
  var e = window.event;
  posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  return posX;
}

// read the Y coordinate of the present position of the mouse

function getY(){
  var e = window.event;
  posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  return posY;
}

// Show the popup element at the current mouse location

function showPopup(popupId){
  var posX = getX();
  var posY = getY();

  var poppyElement = document.getElementById(popupId);

  poppyElement.className = 'render';
  poppyElement.style.left = posX;
  poppyElement.style.top  = poxY;
  poppyElement.style.position = 'absolute';
  poppyElement.style.display = '';

}

// hide the popup element

function hidePopup(popupId){
  var poppyElement = document.getElementById(popupId);

  poppyElement.className = 'hide';

}

My question is - Why does the element flash, and disappear immediately instead of hanging around for the mouse-out event?

Regards, Abhishek


Changing the elements in the JavaScript may be modifying the element being hovered over, which may trigger the mouse out event by changing, rather than actually moving the mouse out of the co-ordinates.


Firstly, you need to be more careful with case sensitivity. It should be clientWidth (capital W) and top (small t). Secondly, when you set CSS left and top you must add a +'px' suffix to the value; an integer on its own is not valid.

Also if you want to know the height of the viewport, document.body is the wrong place to look. This will only work in IE Quirks Mode, which you generally want to avoid like the plague. Add a Standards Mode <!DOCTYPE declaration and you can use document.documentElement across browsers. (Or branch on window.innerHeight for non-IE browsers.)

In any case unless there is more CSS you aren't showing us, setting the left and top styles will have no effect at all because the .render divs are not position: absolute. You're not showing how exactly they are attached to the document, but since they are apparently not absolutely positioned, they're part of the document flow and may cause the page to shift/reflow when they're unhidden. If that causes the element being moused over to move, it will no longer be under the pointer and you'll get a mouseout event immediately.

(This would also happen with absolute positioning if the hovered item was underneath the place the pop-up appeared.)

(Also, the hiding/unhiding is a bit redundant. Leave style.display alone and just set className to either 'render' or 'render hide'.)

0

精彩评论

暂无评论...
验证码 换一张
取 消