Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How would I close this Modal by outside the Modal popup and also the close button?

Here's the code:

<html>

  <head>
    <title>JavaScript &amp; jQuery - Chapter 11: Content Panels - Modal Window</title>
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/modal-window.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="{{assetURL}}/js/modal.js"></script>
 </head>

  <style>

                .modal {
                  position: absolute;
                  z-index: 1000;
                  padding: 50px;
                  background-color: #555;}

                .modal-close {
                  cursor: pointer;}

                #window {
                  background: #fff;
                  text-align: center;}

                .social {
                  color: #858585;}

                section img {
                  display: block;
                  margin: 0 auto;}

                #share {
                  position: absolute;
                  top: 10px;
                  left: 10px;}

                #share-options {
                  background-color: #fff;
                  text-align: center;
                  clear: both;
                  padding: 15px;
                  border: 10px solid #e2e2e2;}

                #message {
                  display: inline-block;
                  margin: 15px 0 15px 0;} 

                #links a {
                  display: inline-block;
                  margin: 0 12px 8px 12px !important;} 

                #window a {
                  margin: 3px;}

                button.modal-close {
                  background-color: #ed8e6c;
                  color: #ffffff;
                  border: none;
                  border-radius: 4px;
                  padding: 7px 10px 9px 10px;
                  margin: 10px 0 0 0;
                  float: right;
                  letter-spacing: 0.1em;
                  text-transform: uppercase;}

                button.modal-close:hover {background-color: #ed612f;}
                      .share{
                        width:30; height:30;
                        background-color: black;
                      }
  </style>

<body style="">

    <header><h1></h1></header>

    <div id="share">

      <a href='#' onclick='overlay()'>  Log In </a>




    </div>

                 <script type="text/javascript">
              (function(){
                var $content = $('#share-options').detach();   // Remove modal from page

                $('#share').on('click', function() {           // Click handler to open modal
                  modal.open({content: $content, width:340, height:300});
                });
              }());




                 </script>


</body>

</html>
modal.js
var modal = (function() {                         // Declare modal object

  var $window = $(window);                        // Store the window
  var $modal = $('<div class="modal"/>');         // Create markup for modal
  var $content = $('<div class="modal-content"/>');
  var $close = $('<button role="button" class="modal-close">close</button>');

  $modal.append($content, $close);                // Add close button to modal

  $close.on('click', function(e){                 // If user clicks on close
    e.preventDefault();                           // Prevent link behavior
    modal.close();                                // Close the modal window
  });

  return {                                        // Add code to modal
    center: function() {                          // Define center() method
      // Calculate distance from top and left of window to center the modal
      var top = Math.max($window.height() - $modal.outerHeight(), 0) / 2;
      var left = Math.max($window.width() - $modal.outerWidth(), 0) / 2;
      $modal.css({                                // Set CSS for the modal
        top:top + $window.scrollTop(),            // Center vertically
        left:left + $window.scrollLeft()          // Center horizontally
      });
    },
    open: function(settings) {                     // Define open() method
      $content.empty().append(settings.content);   // Set new content of modal

      $modal.css({                                 // Set modal dimensions
        width: settings.width || 'auto',           // Set width
        height: settings.height || 'auto'          // Set height
      }).appendTo('body');                         // Add it to the page

      modal.center();                              // Call center() method
      $(window).on('resize', modal.center);        // Call it if window resized
    },
    close: function() {                            // Define close() method
      $content.empty();                            // Remove content from modal
      $modal.detach();                             // Remove modal from page
      $(window).off('resize', modal.center);       // Remove event handler
    }
  };
}());