I was struggling with this one and couldn't quite get it to work out but, I'd wanted it to close the previous overlapping content, if any, as well - like the demo page from dyn-web.com. Using DD's code above as a jumping off point, I was able to do that. Here is my modification to that modification:
Code:
<script type="text/javascript">
/***********************************************
* Drop Down/ Overlapping Content- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}
function overlay(curobj, subobjstr, opt_position, e){
if(typeof document.onclick=='function')
document.onclick(e);
if (document.getElementById){
var subobj=document.getElementById(subobjstr)
document.onclick=function(e){
clicktohide(curobj, subobjstr, e)
}
subobj.style.display=(subobj.style.display!="block")? "block" : "none"
var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0)
var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
subobj.style.left=xpos+"px"
subobj.style.top=ypos+"px"
return false
}
else
return true
}
function overlayclose(subobj){
document.getElementById(subobj).style.display="none"
}
function clicktohide(linkobj, subobj, e){
if(!e && (!window.event||!window.event.srcElement))
return;
var currentnode=(window.event)? event.srcElement : e.target
var hidesubobj=true
while (currentnode.tagName!="BODY" && currentnode.tagName!="HTML"){
if (currentnode.id==subobj || currentnode==linkobj){
hidesubobj=false
break
}
currentnode=currentnode.parentNode
}
if (hidesubobj)
overlayclose(subobj)
}
</script>
However, this requires a slightly different syntax when calling the overlapping content. The optional position must now either be declared or be an empty value and either way, be followed by the unquoted word event (to pass the event to the script). To demonstrate, here are how the two example calls from Step 2 of the demo page here on dynamic drive would now look using this syntax (additions red):
Code:
<a href="search.htm" onClick="return overlay(this, 'subcontent', '', event)">Search DD</a>
and:
Code:
<a href="search.htm" onClick="return overlay(this, 'subcontent2', 'rightbottom', event)">Show Content</a>
Bookmarks