Datacrawler
Datacrawler

Reputation: 2876

Avoid redirecting after closing pop up window

My purpose was to show a pop up window after clicking a specific link on my page. After the pop up window appears, if I click somewhere else it closes (as it should) but I get redirected at the top of my website.

In order to avoid redirecting, is it possible to alter the CSS code a bit?
Or do I need a JS code?

Another option is to set anchors within the link. I will test that only if there is no other way to do it via the CSS.

My CSS code is here :

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.5);
  transition: opacity 200ms;
  visibility: hidden;
  opacity: 0;
}
.overlay.light {
  background: rgba(255, 255, 255, 0.5);
}
.overlay .cancel {
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: default;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 75px auto;
  padding: 20px;
  background: rgb(37, 183, 211);
  border: 1px solid #666;
  width: 300px;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
  position: relative;
}
.light .popup {
  border-color: #aaa;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
}
.popup .close {
  position: absolute;
  width: 20px;
  height: 20px;
  top: 20px;
  right: 20px;
  opacity: 0.8;
  transition: all 200ms;
  font-size: 24px;
  font-weight: bold;
  text-decoration: none;
  color: #666;
}
.popup .close:hover {
  opacity: 1;
}
.popup .content {
  max-height: 400px;
  overflow: auto;
}

And the HTML link is here :

LINK and more specifically please scroll to the "Favourites" tab and click the film icon to see the pop up window.

Upvotes: 1

Views: 857

Answers (3)

Datacrawler
Datacrawler

Reputation: 2876

After searching many options on codepen I made it work with a JS :

HTML :

            <div id="ModalContentWrap" class="modal-wrap">
              <div class="modal-content mix">
                    <section id="stigma-box">
                        <header>
                            <h1>21 Grams</h1>
                            <span class="avatar"><img src="image.jpg" alt="" /></span>
                            <h3><a href=" " target="blank"> ...</a></h3> 
                        </header>
                    </section>
              </div>
            </div>

CSS :

.modal-trigger {
  cursor: pointer;
  outline: 0 none;
}

.modal-wrap,
.modal-bg,
.modal-content {
  display: none;
  position: fixed;
}

.modal-wrap,
.modal-bg {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.modal-wrap {
  -webkit-backface-visibility: hidden;
  -webkit-transform: translateZ(0);
}

.modal-bg {
  background: rgba(0, 0, 0, .6);
}

.modal-content {
    z-index: 1;
    top: 50%;
    left: 50%;
    margin: -100px 0 0 -150px;
    padding: 20px;
    background: rgb(37, 183, 211);
    border: 1px solid #666;
    width: 300px;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    position: absolute;
}

.modal-close {
  display: inline-block;
  padding: 10px;
  cursor: pointer;
}

JS :

var mixModal = {
  $bg: null,
  $content: null,
  init: function() {

    // Instantiate MixItUp on background wrapper

    this.$bg.mixItUp({
      controls: {
        enable: false
      },
      load: {
        filter: 'none'
      },
      animation: {
        effects: 'fade',
        duration: 400,
      }
    });

    // Instantiate MixItUp on content wrapper

    this.$content.mixItUp({
      controls: {
        enable: false
      },
      animation: {
        effects: 'fade translateZ(-300px) translateY(5%)',
        duration: 300,
        easing: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)'
      },
      load: {
        filter: 'none'
      }
    });
  },

  // Create a "show" method

  show: function() {
    this.$bg.show().mixItUp('filter', 'all');
    this.$content.show().mixItUp('filter', 'all');
  },

  // Create a "hide" method

  hide: function() {
    this.$bg.mixItUp('filter', 'none', function() {
      $(this).hide();
    });
    this.$content.mixItUp('filter', 'none', function() {
      $(this).hide();
    });
  }
};

// On document ready:

$(function() {

  // Assign elements to modal properties

  mixModal.$bg = $('#ModalBgWrap');
  mixModal.$content = $('#ModalContentWrap');

  // Initialize modal

  mixModal.init();

  // Bind click handlers

  $('.modal-trigger').on('click', function() {
    mixModal.show();
  });

  $('.modal-close, .modal-wrap').on('click', function(e) {
    if (e.target === this) {
      mixModal.hide();
    }
  });

});

Upvotes: 0

paolobasso
paolobasso

Reputation: 2018

My advice is: DO NOT use :target pseudo-element to show pop-up.

The best way to show pop-up is use javascript or better JQuery and then develop a simple script or use a plugin.

This is an example that you could edit and adapt for your case: FIDDLE

BUT if you want to keep :target you can use href="#fakelink" for example and not href="#". See Demo here: DEMO

Upvotes: 1

Alekos Dordas
Alekos Dordas

Reputation: 800

In your pure-CSS approach to popups, you trigger visibility by using the :target pseudo-class. In order to close the popup, you navigate to "#" and this is why you are "redirected" to the top of your page.

There is no CSS-only way to hide the popup by clicking somewhere else. If you have to use the same method of CSS-only popups, you can either navigate to an existing anchor of your page OR navigate to a non-existing anchor of your page <a class="cancel" href="#popup2closed"></a> and avoid any "redirection". It will just make the popup invisible again.

Upvotes: 0

Related Questions