Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JavaScript Tooltip

Featured Replies

hi everyone!

 

I've got a JavaScript tool tip with the following code:

 

var tooltip=function(){
var id = 'tt';
var top = 3;
var left = 3;
var maxw = 300;
var speed = 10;
var timer = 20;
var endalpha = 95;
var alpha = 0;
var tt,t,c,b,h;

var ie = document.all ? true : false;
return{
 show:function(v,w){
  if(tt == null){
tt = document.createElement('div');
tt.setAttribute('id',id);
t = document.createElement('div');
t.setAttribute('id',id + 'top');
c = document.createElement('div');
c.setAttribute('id',id + 'cont');
b = document.createElement('div');
b.setAttribute('id',id + 'bot');
tt.appendChild(t);
tt.appendChild(c);
tt.appendChild(b);
document.body.appendChild(tt);
tt.style.opacity = 0;
tt.style.filter = 'alpha(opacity=0)';
document.onmousemove = this.pos;
  }
  tt.style.display = 'block';
  c.innerHTML = v;
  tt.style.width = w ? w + 'px' : 'auto';
  if(!w && ie){
t.style.display = 'none';
b.style.display = 'none';
tt.style.width = tt.offsetWidth;
t.style.display = 'block';
b.style.display = 'block';
  }
 if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
 h = parseInt(tt.offsetHeight) + top;
 clearInterval(tt.timer);
 tt.timer = setInterval(function(){tooltip.fade(1)},timer);
 },

pos:function(e){
  var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
  var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
  tt.style.top = (u - h) + 'px';
  tt.style.left = (l + left) + 'px'; 
  if (u < tt.width) {
  	tt.style.bottom = top + u;
  }

 },
 fade:function(d){
  var a = alpha;
  if((a != endalpha && d == 1) || (a != 0 && d == -1)){
var i = speed;
  if(endalpha - a < speed && d == 1){
i = endalpha - a;
  }else if(alpha < speed && d == -1){
 i = a;
  }
  alpha = a + (i * d);
  tt.style.opacity = alpha * .01;
  tt.style.filter = 'alpha(opacity=' + alpha + ')';
 }else{
clearInterval(tt.timer);
 if(d == -1){tt.style.display = 'none'}
 }
},
hide:function(){
 clearInterval(tt.timer);
  tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
 }
};
}();

$(document).ready(function(){
$.getJSON("fetcher.json", function (json){
	$.each(json.items, function (i, items) {

		$('li.pale' + (i+1)).each( function () {
			$(this).mouseover( function () {
				var imageURL = '../fetcher/' + items.image; 
				tooltip.show('<h1>' + items.heading + '</h1> <p>' + items.content + '<br /><img src="'+ items.image + '" alt="image" /><br />'+ items.image +'</p>');
			});

			$(this).mouseout(function(){
				tooltip.hide()
			});		
		});	

	});
});

});

 

and in my html, wherever I have labelled a list item: <li class="pale1">String</li>, a tooltip will appear corresponding to .pale1 .

 

Now what I need help with is the fact that some of the tooltip goes above the page (outside the document) so is not visibile. Can anyone help me with coming up with some JavaScript to detect how much space there is at the top and then make the tooltip shift accordingly. Here is where I got the code from (*** I then modified some of it to accommodate jquery).

 

 

I'd really appreciate the help, I'm on work experience here and want to make a good impression.

 

Cheers

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.