Need help with closing toggle overlay by mouse click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sylo9020
    New Member
    • Jun 2014
    • 1

    Need help with closing toggle overlay by mouse click

    Ok I am trying to set up a webpage where the image overlay is closeable by a mouse click anywhere on the page as well as the "close" button I have already coded. This works already so I think I just need a function to make the image overlay close on a mouse click. I can't seem to find a specific answer online and it is driving me crazy. Any suggestions would greatly be appreciated.


    -------------------------------------------Here is the code I placed in between the <head> tags of my webpage.

    Code:
    <style type="text/css">
    div#overlay {
    display: none;
    z-index: 2;
    background: #000;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    text-align: center;
    }
    div#specialBox {
    display: none;
    position: relative;
    z-index: 3;
    margin: 0px auto 0px auto;
    width: 00px;
    height: 00px;
    background: #FFF;
    color: #000;
    }
    div#wrapper {
    position:absolute;
    top: 0px;
    left: 0px;
    padding-left:24px;
    }
    </style> <script type="text/javascript">
    function toggleOverlay(){
    var overlay = document.getElementById('overlay');
    var specialBox = document.getElementById('specialBox');
    overlay.style.opacity = .8;
    if(overlay.style.display == "block"){
    overlay.style.display = "none";
    specialBox.style.display = "none";
    } else {
    overlay.style.display = "block";
    specialBox.style.display = "block";
    }
    }
    </script>
    
    ----------------------------------------------------Here is the code I placed in the <body> tags. 
    
    <body onload = "toggleOverlay()" >  <div id="overlay"></div>   <div id="specialBox"> <p><img src="http://bytes.com/MY-IMAGE-IN-THE-ROOT-OF-WEBSITE" /> <button onmousedown="toggleOverlay()">Close</button></p> </div>
    -----------------------------------------------------------------------------------

    The owner of the website says the close button at the bottom of the overlay isn't enough, they also want it to close on mouse click. Is that even possible?

    Thanks
    Last edited by Rabbit; Jun 30 '14, 06:27 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code or formatted data.

    You're looking for the onmouseup event. You can read more about the event from this intro to javascript tutorial: http://www.w3schools.com/jsref/event_onmouseup.asp

    Comment

    Working...