On Nov 4, 9:32*am, Kim <kims...@gmail.com> wrote:
> Using the code below am I able to display/hide a tooltip without any
> problems, however once the tooltip is displayed its position is fixed
Without any problems that you can see.
> (based on where the mouse first hovered onto the object) and I would
> like the tooltip to follow the mouse instead.
That is so irritating and pointless.
> What must I change to do this ?
>
> HTML
> <a href="#" onMouseOver="showBox('help','text to display')"
> onMouseOut="hideBox('help')">text link</a>
Link goes nowhere? Might navigate to the top of the page in some
browsers.
>
> JS
> function hideLayer(strLayer) {
> * * * * if (document.getElementById) {
Do not test host object methods in this manner. Use the typeof
operator. Search the group for "isHostMethod".
> * * * * * * * * d = document.getElementById(strLayer);
> * * * * * * * * if (d) {
> * * * * * * * * * * * * d.style.visibility = 'hidden';
> * * * * * * * * * * * * d.style.display = 'none';
> * * * * * * * * }
> * * * * }
> * * * * else if (document.all) {
Here's where the wheels come off.
> * * * * * * * * d = eval('document.all[\''+strLayer+'\']');
> * * * * * * * * if (d) {
> * * * * * * * * * * * * eval('document.all[\''+strLayer+'\'].style.visibility = \'hidden
> \'');
> * * * * * * * * * * * * eval('document.all[\''+strLayer+'\'].style.display = \'none\'');
> * * * * * * * * }
> * * * * }}
Where did you pick that up? If you find yourself using eval, you are
likely off in the weeds.
>
> function showLayer(strLayer) {
> * * * * if (document.getElementById) {
> * * * * * * * * d = document.getElementById(strLayer);
> * * * * * * * * d.style.visibility = 'visible';
> * * * * * * * * d.style.display = 'block';
> * * * * }
> * * * * else if (document.all) {
> * * * * * * * * eval('document.all[\''+strLayer+'\'].style.visibility = \'visible
> \'');
> * * * * * * * * eval('document.all[\''+strLayer+'\'].style.display = \'block\'');
> * * * * }}
Same comments for this virtual duplication.
>
> function getObj(name) {
> * * * * if (document.getElementById) {
> * * * * * * * * this.obj = document.getElementById(name);
> * * * * * * * * this.style = document.getElementById(name).style;
> * * * * }
> * * * * else if (document.all) {
> * * * * * * * * this.obj = document.all[name];
> * * * * * * * * this.style = document.all[name].style;
> * * * * }
No eval this time?
> * * * * else if (document.layers) {
> * * * * * * * * this.obj = document.layers[name];
> * * * * * * * * this.style = document.layers[name];
> * * * * }
Clearly you can lose this branch. It is only for NN4 and makes no
sense in the context of this script.
> * * * * return this;}
>
> document.onmousemove = getMousePosition;
It is good practice to declare functions before their first use.
> if (!document.all) document.captureEvents(Event.MOUSEMOVE);
That might be the worst object inference (a form of browser sniffing)
I have ever seen. Where did you find this stuff?
> var mouse_x = 0;
> var mouse_y = 0;
> function getMousePosition(e) {
> * * * * if (!e) var e = window.event;
e = e || window.event;
> * * * * if (e.pageX || e.pageY) {
Wrong. What if both are 0?
> * * * * * * * * mouse_x = e.pageX;
> * * * * * * * * mouse_y = e.pageY;
> * * * * }
> * * * * else if (e.clientX || e.clientY) {
Same here.
> * * * * * * * * mouse_x = e.clientX + (document.documentElement ?
> document.documentElement.scrollLeft : document.body.scrollLeft);
Wrong. The documentElement property is defined in quirks mode. You
should be testing the scrollLeft/Top properties.
> * * * * * * * * mouse_y = e.clientY + (document.documentElement ?
> document.documentElement.scrollTop : document.body.scrollTop);
> * * * * }}
Same here.
>
> function showBox(name, msg) {
> * * * * var x = (mouse_x + 20) + 'px';
> * * * * var y = (mouse_y + 0) + 'px';
> * * * * var box = getObj(name);
>
> * * * * box.obj.innerHTML = msg;
> * * * * box.style.position = 'absolute';
> * * * * box.style.top = y;
> * * * * box.style.left = x;
> * * * * showLayer(name);}
>
> function hideBox(name) {
> * * * * hideLayer(name);
>
> }
>
>
I advise you to look into the title attribute. Most browsers will
turn it into a tooltip. Furthermore, search engines, screen readers,
text browsers, etc. will be able to see the content. No script
required. That should save you a lot of trouble.
|