kylie2675647
kylie2675647

Reputation: 153

Popup modal and jump to anchor inside

I am trying to create a link that when clicked, a modal pops up, and also jumps to an anchor in it.

At the moment you will see i have some code for the pop up (which is configured for a set of nested modals) hence it is not the standard setup.

When you click on the links twice, they work. How can I get it to work as is, with one click only.

Here is a fiddle

      //popup nested modals
      $(function () {
    const openModals = [];
    $('.element-item').click(e => {
      e.preventDefault();
      $(e.currentTarget).closest('.modal').add('body').addClass('open');
      openModals.push($($(e.currentTarget).attr('href')).show());
    });
    $(window).add('.close').click(e => {
      e.stopPropagation();
      if ($(e.target).is('.modal, .close')) {
        const closing = openModals.pop().addClass('modal-content-active');
        setTimeout(() => {closing.hide().removeClass('modal-content-active')}, 0);
        if (openModals.length > 0) {
          openModals[openModals.length - 1].removeClass('open');
        } else $('body').removeClass('open');
      }
    });
  });
  
 //jump to anchor in modal
    $('.item1').on('click',function(){ $('#contributors').animate( { scrollTop: $('#item1').offset().top -40 }, 500); });
  $('.item2').on('click',function(){ $('#contributors').animate( { scrollTop: $('#item2').offset().top -40 }, 500); });
.modal{
    width: 300px;
    height: 200px;
    overflow: auto;
   display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#contributors" class="element-item item1">item1</a>
    <a href="#contributors" class="element-item item2">item2</a>
        
  <!-- The Modal -->
  <div id="contributors" class="modal">
        <header><span class="close">×</span></header>
   line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line
      <div class="item" id="item1"><h4>Item 1</h4></div>
<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line
<div class="item" id="item2"><h4>Item 2</h4></div>
<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line
  </div>

Upvotes: 0

Views: 824

Answers (2)

Diogo Peres
Diogo Peres

Reputation: 1372

That's because when you click on the item1, you are firing both click handlers at the same time, the one from $('.element-item') and the one from $('.item1'). And the $('#item1').offset().top will be 0 because the container it's with display: none. You need to fire it after the display has been set. Do as @marzelin has done, it's the easier way.

Upvotes: 0

marzelin
marzelin

Reputation: 11600

The problem is that the layout updates only after both click handlers are done. A quick fix is to wrap a handler with requestAnimationFrame:

$('.item1').on('click',function(){ requestAnimationFrame(() => 
    $('#contributors').animate( { scrollTop: $('#item1').offset().top -40 }, 500)
)});

https://jsfiddle.net/onbmh7ur/

Upvotes: 2

Related Questions