Shifting page focus when opening a collapse

Viewed 854

I have a website with 3 images, on click the image opens a collapse below with an iframe website inside. I want the site to scroll down to the opened iframe when the image is clicked. I can only find an answer for accordions.

The frames open a 3d tour, which should stop when another is opened. I've made a basic codepen, it uses dummy images and only links to one tour atm. https://codepen.io/jvern22/pen/BGzgjw

My code. Image with link to Iframe, there are three of these;

<div class="col-md-4 port-item">
    <a href="" class="" data-toggle="collapse" data-target="#tour-duqesa">              
        <img src="assets/img/duqesa.jpg" alt="">
    </a>
    <h2 class="section-title-nomargin features-box">Duquesa</h2>
    <h5 class="options-text"><a href="assets/tours/duquesashorttour/index.html" class="" target="_blank">Open in new window</h5></a>              
</div>

The code for the iframe, again, there are three of these;

  <div id="tour-duqesa" class="collapse">
      <iframe height="600px" width="100%" src="assets/tours/duquesashorttour/index.html" name="iframe"></iframe>
  </div>

I'm new to this so I hope that I have asked in the correct way.

1 Answers

Maybe some code on Jquery can help to approach what you need.

First, we can register a listener on the click event for the images that open a collapsible element, and then when some of they are clicked, close the current one that is visible to the user. This will help to only have one collapsible element open at a time. A code for this would be the next:

// Register a click listener on the images that opens a collapsible element.

$("a[data-toggle='collapse']").click(function()
{
    // Close all visibile collapse elements.
    $(".collapse:visible").collapse('hide');
});

Second, when a collapsible element is fully visible to the user, it triggers the next event: shown.bs.collapse. So we can put a listener on that event, and scroll down to the current visible collapsible with the next code:

$(".collapse").on("shown.bs.collapse", function()
{
    var current = $(this);

    // Scroll down to the current opened collapse element.

    $([document.documentElement, document.body]).animate(
        {scrollTop: current.offset().top},
        1000
    );
});

Here you have a working example you can play with it (check on fullscreen mode):

$(document).ready(function()
{
    // Register a click listener on the images that opens a collapsible element.

    $("a[data-toggle='collapse']").click(function()
    {
        // Close all visibile collapse elements.
        $(".collapse:visible").collapse('hide');
    });

    // Register a listener for when a collapse element is made
    // visible to the user (wait for animations transitions).

    $(".collapse").on("shown.bs.collapse", function()
    {
        var current = $(this);

        // Scroll down to the current opened collapse element.

        $([document.documentElement, document.body]).animate(
            {scrollTop: current.offset().top},
            1000
        );
    });
});
box-sizing: border-box;

.display-iframe {
  border: none;
}

.options-text {
  font-size: 14px;
  font-family: "Roboto Condensed", sans-serif;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: #000;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<section id="tour-gallery">
<div class="container">

  <div class="row">
    <div class="col-sm-12 section-description wow fadeIn">
      <h2 class="section-title">Have a look at what can be done</h2>        
    </div>

    <div class="col-md-4 port-item">
      <a href="#tour-duqesa" class="" data-toggle="collapse" data-target="#tour-duqesa">              
        <img src="https://dummyimage.com/350x186/000/fff" alt="">
      </a>
      <h2 class="section-title-nomargin features-box">Duquesa</h2>                           
    </div>

    <div class="col-md-4 port-item">
      <a href="" class="" data-toggle="collapse" data-target="#tour-aldeahills1">
        <img src="https://dummyimage.com/350x186/000/fff" alt="">
      </a>
      <h2 class="section-title-nomargin features-box">Aldea Hills</h2>              
    </div>

    <div class="col-md-4 port-item">
      <a href="" class="" data-toggle="collapse" data-target="#tour-aldeahills2">
        <img src="https://dummyimage.com/350x186/000/fff" alt="">
      </a>
      <h2 class="section-title-nomargin features-box">Aldea Hills 2</h2>
    </div>
  </div>

  <div id="tour-duqesa" class="collapse">
    <iframe height="600px" width="100%" src="https://your360tours.com/test/assets/tours/duquesashorttour/index.html" name="iframe"></iframe>
  </div>

  <div id="tour-aldeahills1" class="collapse">
    <iframe height="600px" width="100%" src="https://your360tours.com/test/assets/tours/duquesashorttour/index.html" name="iframe"></iframe>
  </div>

  <div id="tour-aldeahills2" class="collapse">
    <iframe height="600px" width="100%" src="https://your360tours.com/test/assets/tours/duquesashorttour/index.html" name="iframe"></iframe>
  </div>

</div>
</section>

Also, I push the code on codepen too, but had to replace the slim version of JQuery for the full version, otherwise the animate() won't be defined, here you have the link:

Codepen Example

Related