首页 > 解决方案 > Shifting page focus when opening a collapse

问题描述

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.

标签: javascripthtmlcssbootstrap-4

解决方案


也许 Jquery 上的一些代码可以帮助满足您的需求。

首先,我们可以为打开可折叠元素的图像注册一个事件监听click器,然后当其中一些被点击时,关闭当前对用户可见的图像。这将有助于一次只打开一个可折叠元素。下一个代码将是:

// 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');
});

其次,当一个可折叠元素对用户完全可见时,它会触发下一个事件:shown.bs.collapse. 所以我们可以在那个事件上放置一个监听器,并使用下面的代码向下滚动到当前可见的可折叠项:

$(".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
    );
});

在这里,您有一个可以使用的示例(检查全屏模式):

$(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>

另外,我也将代码推送到 codepen,但必须将JQuery的精简版替换为完整版,否则将无法定义,这里有链接:animate()

代码笔示例


推荐阅读