JAVASCRIPT   68
dropdowncontent
Guest on 20th August 2022 12:50:19 AM


  1. //Drop Down/ Overlapping Content: http://www.dynamicdrive.com
  2. //**Updated: Added ability to dynamically populate a Drop Down content using an external file (Ajax feature)
  3.                                 //1) Added ability to reveal drop down content via "click" of anchor link (instead of default "mouseover")
  4.                                 //2) Added ability to disable drop down content from auto hiding when mouse rolls out of it
  5.                                 //3) Added hidediv(id) public function to directly hide drop down div dynamically
  6.  
  7. //**Updated: Fixed bug whereby drop down content isn't revealed onClick of anchor in Safari/ Google Chrome
  8.  
  9. var dropdowncontent={
  10.         disableanchorlink: true, //when user clicks on anchor link, should link itself be disabled (always true if "revealbehavior" above set to "click")
  11.  hidedivmouseout: [true, 200], //Set hiding behavior within Drop Down DIV itself: [hide_div_onmouseover?, miliseconds_before_hiding]
  12.         ajaxloadingmsg: "Loading content. Please wait...", //HTML to show while ajax page is being feched, if applicable
  13.         ajaxbustcache: true, //Bust cache when fetching Ajax pages?
  14.  
  15.         getposOffset:function(what, offsettype){
  16.                 return (what.offsetParent)? what[offsettype]+this.getposOffset(what.offsetParent, offsettype) : what[offsettype]
  17.         },
  18.  
  19.         isContained:function(m, e){
  20.                 var e=window.event || e
  21.                 var c=e.relatedTarget || ((e.type=="mouseover")? e.fromElement : e.toElement)
  22.                 while (c && c!=m)try {c=c.parentNode} catch(e){c=m}
  23.                 if (c==m)
  24.                         return true
  25.                 else
  26.                         return false
  27.         },
  28.  
  29.         show:function(anchorobj, subobj, e){
  30.                 if (!this.isContained(anchorobj, e) || (e && e.type=="click")){
  31.                         var e=window.event || e
  32.                         if (e.type=="click" && subobj.style.visibility=="visible"){
  33.                                 subobj.style.visibility="hidden"
  34.                                 return
  35.                         }
  36.                         var horizontaloffset=(subobj.dropposition[0]=="left")? -(subobj.offsetWidth-anchorobj.offsetWidth) : 0 //calculate user added horizontal offset
  37.                         var verticaloffset=(subobj.dropposition[1]=="top")? -subobj.offsetHeight : anchorobj.offsetHeight //calculate user added vertical offset
  38.                         subobj.style.left=this.getposOffset(anchorobj, "offsetLeft") + horizontaloffset + "px"
  39.                         subobj.style.top=this.getposOffset(anchorobj, "offsetTop")+verticaloffset+"px"
  40.                         subobj.style.clip=(subobj.dropposition[1]=="top")? "rect(auto auto auto 0)" : "rect(0 auto 0 0)" //hide drop down box initially via clipping
  41.                         subobj.style.visibility="visible"
  42.                         subobj.startTime=new Date().getTime()
  43.                         subobj.contentheight=parseInt(subobj.offsetHeight)
  44.                         if (typeof window["hidetimer_"+subobj.id]!="undefined") //clear timer that hides drop down box?
  45.                                 clearTimeout(window["hidetimer_"+subobj.id])
  46.                         this.slideengine(subobj, (subobj.dropposition[1]=="top")? "up" : "down")
  47.                 }
  48.         },
  49.  
  50.         curveincrement:function(percent){
  51.                 return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
  52.         },
  53.  
  54.         slideengine:function(obj, direction){
  55.                 var elapsed=new Date().getTime()-obj.startTime //get time animation has run
  56.                 if (elapsed<obj.glidetime){ //if time run is less than specified length
  57.                         var distancepercent=(direction=="down")? this.curveincrement(elapsed/obj.glidetime) : 1-this.curveincrement(elapsed/obj.glidetime)
  58.                         var currentclip=(distancepercent*obj.contentheight)+"px"
  59.                         obj.style.clip=(direction=="down")? "rect(0 auto "+currentclip+" 0)" : "rect("+currentclip+" auto auto 0)"
  60.                         window["glidetimer_"+obj.id]=setTimeout(function(){dropdowncontent.slideengine(obj, direction)}, 10)
  61.                 }
  62.                 else{ //if animation finished
  63.                         obj.style.clip="rect(0 auto auto 0)"
  64.                 }
  65.         },
  66.  
  67.         hide:function(activeobj, subobj, e){
  68.                 if (!dropdowncontent.isContained(activeobj, e)){
  69.                         window["hidetimer_"+subobj.id]=setTimeout(function(){
  70.                                 subobj.style.visibility="hidden"
  71.                                 subobj.style.left=subobj.style.top=0
  72.                                 clearTimeout(window["glidetimer_"+subobj.id])
  73.                         }, dropdowncontent.hidedivmouseout[1])
  74.                 }
  75.         },
  76.  
  77.         hidediv:function(subobjid){
  78.                 document.getElementById(subobjid).style.visibility="hidden"
  79.         },
  80.  
  81.         ajaxconnect:function(pageurl, divId){
  82.                 var page_request = false
  83.                 var bustcacheparameter=""
  84.                 if (window.XMLHttpRequest) // if Mozilla, IE7, Safari etc
  85.                         page_request = new XMLHttpRequest()
  86.                 else if (window.ActiveXObject){ // if IE6 or below
  87.                         try {
  88.                         page_request = new ActiveXObject("Msxml2.XMLHTTP")
  89.                         }
  90.                         catch (e){
  91.                                 try{
  92.                                 page_request = new ActiveXObject("Microsoft.XMLHTTP")
  93.                                 }
  94.                                 catch (e){}
  95.                         }
  96.                 }
  97.                 else
  98.                         return false
  99.                 document.getElementById(divId).innerHTML=this.ajaxloadingmsg //Display "fetching page message"
  100.                 page_request.onreadystatechange=function(){dropdowncontent.loadpage(page_request, divId)}
  101.                 if (this.ajaxbustcache) //if bust caching of external page
  102.                         bustcacheparameter=(pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
  103.                 page_request.open('GET.html', pageurl+bustcacheparameter, true)
  104.                 page_request.send(null)
  105.         },
  106.  
  107.         loadpage:function(page_request, divId){
  108.                 if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
  109.                         document.getElementById(divId).innerHTML=page_request.responseText
  110.                 }
  111.         },
  112.  
  113.  init:function(anchorid, pos, glidetime, revealbehavior){
  114.                 var anchorobj=document.getElementById(anchorid)
  115.                 var subobj=document.getElementById(anchorobj.getAttribute("rel"))
  116.                 var subobjsource=anchorobj.getAttribute("rev")
  117.                 if (subobjsource!=null && subobjsource!="")
  118.                         this.ajaxconnect(subobjsource, anchorobj.getAttribute("rel"))
  119.                 subobj.dropposition=pos.split("-")
  120.                 subobj.glidetime=glidetime || 1000
  121.                 subobj.style.left=subobj.style.top=0
  122.                 if (typeof revealbehavior=="undefined" || revealbehavior=="mouseover"){
  123.                         anchorobj.onmouseover=function(e){dropdowncontent.show(this, subobj, e)}
  124.                         anchorobj.onmouseout=function(e){dropdowncontent.hide(subobj, subobj, e)}
  125.                         if (this.disableanchorlink) anchorobj.onclick=function(){return false}
  126.                 }
  127.                 else
  128.                         anchorobj.onclick=function(e){dropdowncontent.show(this, subobj, e); return false}
  129.                 if (this.hidedivmouseout[0]==true) //hide drop down DIV when mouse rolls out of it?
  130.                         subobj.onmouseout=function(e){dropdowncontent.hide(this, subobj, e)}
  131.         }
  132. }

Raw Paste

Login or Register to edit or fork this paste. It's free.