Stephen Daly - ASP.net/C# developer & technology enthusiast

Popup div that disables the background & uses mouse position

I needed to develop a rollover popup div for one of my projects. When you roll over a gig item on the home page, the full details of the gig pops up which is placed according to the mouse position. I did this for 2 reasons:

  • To make the site more interactive/web 2.0
  • To decrease the amount of clicks for a user to view the data.

 
In this tutorial you will be showing the following:

  • How to get the mouse position
  • How to display a popup
  • How to disable/block/hide the background by using a transparent div

 
You can view the online demo/example here : Popup Demo 

You can download the files here : Popup Files

Code

JavaScript 

// To get the mouse position:

// Checks if the browsers is IE or another.

// document.all will return true or false depending if its IE

// If its not IE then it adds the mouse event

if (!document.all)

document.captureEvents(Event.MOUSEMOVE)

// On the move of the mouse, it will call the function getPosition

document.onmousemove = getPosition; 

// These varibles will be used to store the position of the mouse

var X = 0

var Y = 0 

// This is the function that will set the position in the above varibles

function getPosition(args)

{

  // Gets IE browser position

  if (document.all)

  {

    X = event.clientX + document.body.scrollLeft

    Y = event.clientY + document.body.scrollTop

  }

  // Gets position for other browsers

  else

  { 

    X = args.pageX

    Y = args.pageY

  } 

}

 // To enable/disable the background: 

function backgroundFilter()

{

    var div;

    if(document.getElementById)

    // Standard way to get element

    div = document.getElementById('backgroundFilter');

    else if(document.all)

    // Get the element in old IE's

    div = document.all['backgroundFilter'];   

    // if the style.display value is blank we try to check it out here

    if(div.style.display== '' && div.offsetWidth != undefined&&div.offsetHeight != undefined)

    {

        div.style.display = (div.offsetWidth!=0 && div.offsetHeight!=0)?'block':'none';

    }

    // If the background is hidden ('none') then it will display it ('block').

    // If the background is displayed ('block') then it will hide it ('none').

    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';

}

 // To display/hide the popup: 

function popUp()

{

    var div;

    if(document.getElementById)

    // Standard way to get element

    div = document.getElementById('popupWindow');

    else if(document.all)

    // Get the element in old IE's

    div = document.all['popupWindow'];   

    // if the style.display value is blank we try to check it out here

    if(div.style.display== '' && div.offsetWidth != undefined && div.offsetHeight != undefined)

    {

        div.style.display = (div.offsetWidth!=0 && elem.offsetHeight!=0)?'block':'none';

    }

    // If the PopUp is hidden ('none') then it will display it ('block').

    // If the PopUp is displayed ('block') then it will hide it ('none').

    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';

    // Off-sets the X position by 15px

    X = X + 15;   

    // Sets the position of the DIV

    div.style.left = X+'px';

    div.style.top = Y+'px';

}

 

CSS

The main thing to note about the CSS is z-index, display, filter & position

  • Z-Index : This is the way you can layer div's on top of each other. In my example i set the z-index of the div that will disable the background to 1000. And because i wanted the popup to be in front of that div, i set the z-index of the popup to 1005.
  • Display:  When i want a div to be displayed, is set it to "Block" and i set the display to "None" to hide it. You can see in the javascript code above where this is been done.
  • Filter: I used filter + opacity to make the disabled div transparent so the user can still view the background.
  • Position: This is got from the javascript function and it then sets the position of the popup div 

To understand these terms much better and to improve on your css/javascript and many more skills then please visit: www.w3schools.com for lots of great tutorials.

To view the css code: Click Here 


Posted by: Admin
Posted on: 11/03/2008 at 4:22 PM
Tags: , , , , , , , ,
Actions: E-mail | Digg it! | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (4) | Post RSSRSS comment feed

Comments

Patrick Canada

Sunday, April 12, 2009 10:33 AM

Patrick

Hi,

Thanks for these div tips.

I'm trying to use these popus on a site multiples time on one page with different info in each popup. The problem is that I can't get the popups to display what in the individul divs (it alway displays the 1st div).

Any ideas?

Newbie web designer

Patrick Canada

Sunday, April 12, 2009 11:04 AM

Patrick

sorted my multiple div issue.

Newbie web designer.

Camaro parts United States

Wednesday, June 03, 2009 11:26 PM

Camaro parts

great post, thanks for the code snips

Aby Lex Japan

Tuesday, June 08, 2010 10:26 AM

Aby Lex

Hi Stephen,

Thanks for your post. I found it very useful and clean way to demonstrate and understand.

Smile

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading