XML technologies / SVG / Interactive graphics / Manipulating graphic elements / Manipulating the visible property / Adding scripts

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

The remaining scripts must:

Several functions and global variables follow. First create a <script> tag for the functions, defining the global variables min and max as follows. min specifies the first rectangular box number, for K01, and max the last. Adjust them for your needs; the script adds leading zeros.

<script type="text/ecmascript"> <![CDATA[var min=1; var max=6;]]> </script>

Define functions inside the tag. The function hovern( ) runs when the pointer enters the close button (onmouseover) or leaves it (onmouseout). First, the variable button receives the triggering object, returned by the getTarget( ) method.

Note:
Instead of the method getTarget( ) the property evt.target returns the same object but does not work in Netscape Navigator.

Then retrieve the graphic’s svgdocument object with the method getOwnerDocument, which returns the button’s parent document, usually an svgDocument object used to access named elements. Calculating the tooltip’s name requires the element name defined by the id attribute.

Retrieve attributes with the element’s method getAttribute, passing the case-sensitive attribute name. Thus the function uses var bname=button.getAttribute("id"); to store the element’s id in the variable bname. Prefix it with "T" to calculate the tooltip name.

The method getElementById(tname) retrieves the tooltip object and stores it in ttipp. Then store the style object corresponding to the tag’s style attribute in the variable ttippStyle. This gives access to individual properties of the style attribute or its whole string. Test whether the style attribute equals the string visibility:inherit. If so, inside the if instruction use the setAttribute method to set the attribute style to the value visibility:hidden. Otherwise, when the tooltip is hidden (visibility:hidden), show it with visibility:inherit. Its parent group is visible, and the value inherit inherits the parent’s property value.

The if instruction thus toggles the tooltip depending on its current visibility.

function hovern(evt) 
{
  var button=evt.getTarget();
  var svgdok=button.getOwnerDocument();
  var bname=button.getAttribute("id");
  var tname="T"+bname;
  var ttipp=svgdok.getElementById(tname);
  var ttippStyle=ttipp.getAttribute("style");
  if (ttippStyle=="visibility:inherit")
  {
    ttipp.setAttribute("style","visibility:hidden");
  }
  else
  {
    ttipp.setAttribute("style","visibility:inherit");
  }
}

The function Einblenden shows boxes and takes several parameters. The parameter evt identifies the triggering element and is supplied by the interpreter. The parameter elemName names the element to show, different from the clicked element identified by evt. The parameters x and y set the target position. Again declare variables. The triggering hyperlink is assigned to the variable hlink. The instruction var Nstring=new String(elemName); creates from the parameter elemName a String object so its first character can be removed to calculate the tooltip name. This calculation uses var bname="TI" + Nstring.substr(1,Nstring.length-1);.

The substr method returns a substring starting at index 1, the second character since indexing starts at 0. Its length is calculated by Nstring.length-1. The length property returns string length; subtract 1 to remove the initial character. Prefix the substring with "TI" and retrieve the tooltip object with the getElementById method.

Next iterate over all boxes in a loop from min through max. Check visibility and hide visible boxes with elem.setAttribute("style", "visibility:hidden");, ensuring only one remains displayed. For correct names, check whether the counter I is below 10 and prefix 0 if so. The first if instruction inside the for loop does this. Then show or hide the required elements by setting the style attribute with the setAttribute method. Also reset the group’s transform attribute to position it, using the target parameters x and y.

function anzeigen(evt,elemName,x,y) 
{
  var hlink=evt.getTarget();
  var svgdok=hlink.getOwnerDocument();
  var Nstring=new String(elemName);
  var gruppe=svgdok.getElementById(elemName);
  var bname="TI" + Nstring.substr(1,Nstring.length-1);
  var ttipp=svgdok.getElementById(bname);
  var I=min;
  var elem;
  for (I=min;I<=max;I++)
  {
    if (I<10)
    {
      elem=svgdok.getElementById("K0"+I);
    }
    else
    {
      elem=svgdok.getElementById("K"+I);
    }
    if (elem.getStyle().getPropertyValue("visibility")=="visible")
    { 
      elem.setAttribute("style","visibility:hidden");
      if (I<10)
      {
        elem=svgdok.getElementById("TI0"+I);
      }
      else
      {
        elem=svgdok.getElementById("TI"+I);
      }
      elem.setAttribute("style","visibility:hidden");
      }
    }
    ttipp.setAttribute("style","visibility:hidden");
    gruppe.setAttribute("style","visibility:visible");
    gruppe.setAttribute("transform","translate("+x+","+y+")");
  }

Semicircular boxes also need a function. Hide all circles before showing others, reducing opacity so inactive ones remain faintly visible. This is done by the function aufklh. First the variable hlink receives the triggering element supplied as the parameter evt. Then retrieve the SVG document with the method getOwnerDocument( ) and assign it to the variable svgdok. Use it for all elements with the method setAttribute to set the property opacity. Finally the if instruction checks that the parameter strName differs from the empty string ''. If so, set the property opacity of the element named by strName to 0.5.

This also lets the function hide all circles: pass an empty string as the second parameter. The horizontal navigation button calls it this way.

function aufklh(evt,strName)
{
  var hlink=evt.getTarget();
  var svgdok=hlink.getOwnerDocument();
  var I;
  svgdok.getElementById("Kontakt").setAttribute("style","opacity:0.1");
  svgdok.getElementById("Impressum").setAttribute("style","opacity:0.1");
  svgdok.getElementById("Ziel").setAttribute("style","opacity:0.1");
  svgdok.getElementById("Projekte").setAttribute("style","opacity:0.1");
  if (strName!='') 
  {svgdok.getElementById(strName).setAttribute("style","opacity:0.5")};
}

   

 

 

 

back to top