JavaScript
  Home arrow JavaScript arrow Page 3 - Creating Pop-Up Notes with CSS and JavaScr...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Creating Pop-Up Notes with CSS and JavaScript Part I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 46
    2005-01-19

    Table of Contents:
  • Creating Pop-Up Notes with CSS and JavaScript Part I
  • Creating static pop-up notes
  • Creating dynamic pop-up notes
  • The HTML markup

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating Pop-Up Notes with CSS and JavaScript Part I - Creating dynamic pop-up notes


    (Page 3 of 4 )

    As we can see, the current script is a little weak and inefficient, because we're using absolute coordinates to position our pop-up note on the web document. Let's say visitors have changed their browser settings to use several font sizes, or set different screen resolutions. This might easily break down our note layout, since the text or element which the note is applied to would get expanded or contracted along with the Web page, and our pop-up note wouldn't be located in the right spot.

    The solution for this issue is to rewrite our script in a way that detects the mouse coordinates when users pass over the link, and then position the note at those coordinates. To do this, let's modify our JavaScript code in order to increase its capabilities:

    <script language="javascript">

    createNotes=function(){
                showNote=function(){
                            // gets note1 element
                            var note1=document.getElementById('note1');
                            // assigns X,Y mouse coordinates to 'note1' element\
                            note1.style.left=event.clientX;
                            note1.style.top=event.clientY;\
                            // makes note1 element visible
                            note1.style.visibility='visible';
                }

                hideNote=function(){
                            // gets note1 element
                            var note1=document.getElementById('note1');
                            // hides note1 element
                            note1.style.visibility='hidden';
                }
                var a1=document.getElementById('a1');
                // shows note1 element when mouse is over
                a1.onmouseover=showNote;
                // hides note1 element when mouse is out 
                a1.onmouseout=hideNote;
    }
    // execute code once page is loaded
    window.onload=createNotes;
    </script>

    Here, the script remains mostly the same, except that we have modified the showNote() function, which now is taking the x, y mouse coordinates, assigning them to the left and top properties of the pop-up note:

    showNote=function(){
                // gets note1 element
                var note1=document.getElementById('note1');
                // assigns X,Y mouse coordinates to 'note1' element
                note1.style.left=event.clientX;
                note1.style.top=event.clientY;
                // makes note1 element visible
                note1.style.visibility='visible';
    }

    We're making use of the powerful JavaScript Event Object. Since we're showing the pop-up note through the mouseover event handler, each time the visitor passes the mouse over the link, this object is available within the script, and therefore we are able to get the mouse coordinates. We do that with the following lines:

    note1.style.left=event.clientX;

    note1.style.top=event.clientY;

    clientX and clientY are the object properties to obtain the X,Y mouse coordinates, which are assigned to the left and top positions of our note. Doing so, we've generated a pop-up note that will appear positioned at the corresponding mouse position when the visitor hovers over the link. This is a big improvement compared to the first example above, since it was only static.

    The CSS code is very similar to the first case, and is shown below:

    <style type="text/css">

    p {
                font: normal 11px "Verdana", Arial, Helvetica, sans-serif;
                color: #000;
    }

    a.special:link,a.special:visited {
                font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
                color: #00f;
                text-decoration: underline;
    }

    a.special:hover {
                color: #f00;
    }

    #note1 {
                position: absolute;
                top: 0px;
                left: 0px;
                background: #ffc;
                padding: 10px;
                border: 1px solid #000;
                z-index: 1;
                visibility: hidden;
                font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
                color: #000;
    }

    </style>

    As we can appreciate, the only noticeable differences are that we've styled the link applying to it the class "special," just to differentiate from the other links present on the page. Again, feel free to add your own CSS rules and naming techniques. I have taken this approach for the sake of the example.

    Next, the "#note1" selector is styled similarly, but the top and left properties are set to 0px. Since this box will be initially hidden from view, and then displayed according to the user's action, these values are not critical.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Hello,The article explains in a detailed way, how to build pop-up notes using...
       · It's very useful for all to make web!Thank you!I will hope u post PartII.
       · Hi,Thank you for your feedback. Very much appreciatedAlejandro...
       · However the code is rather sloppy. You don't check for any availability of objects...
       · Thank you for the comment. You point is right, since the title attribute is good...
       · That might be the case, but where is the logical connection between the element and...
       · Yep, I've read carefully your above reasons, and they're pretty good. The whole...
       · This method is very bad from a semantics point of view. If you use a screen reader,...
       ·  Hi there, Thank you for your comments. Of course I agree that the title...
       · Hey,I would just look at the successful exposure, in a pedagogical sense.I...
       · Sure, it could work inserting images inside divs. Jut give a try.Thank you for...
       · There are plenty of occasions where this pop-up note could be useful and...
       · Hello friend,The suggested example is something that I haven't been thinking of,...
       · If you cut and paste the "here's the full code" bit you will find it works fine in...
       · Was it even mentioned? I didn't see anything discussing the compatibility issues. ...
       · Thank you for the good contributions here. As far I know, the script works in...
       · ok the scripts all good and all but what if you are new to css like me how and where...
       · Thank you for commenting on my article. Concerning your question, the tutorial...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway