打开折叠时移动页面焦点

时间:2018-11-10 13:25:32

标签: javascript html css3 bootstrap-4

我有一个包含3张图片的网站,点击图片会在下面打开一个collapse网站,其中包含一个iframe网站。我希望网站在单击图像时向下滚动到打开的iframe。我只能找到手风琴的答案。

框架将打开3d漫游,当另一个漫游打开时,该漫游应停止。我已经制作了一个基本的Codepen,它使用虚拟图像并且仅链接到一个自动取款机。 https://codepen.io/jvern22/pen/BGzgjw

我的代码。链接到Iframe的图像,其中有三个;

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

iframe的代码又是其中的三个;

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

我对此并不陌生,所以希望我以正确的方式提出要求。

1 个答案:

答案 0 :(得分:1)

也许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 slim 版本,而不是完整版本。 animate()不会被定义,这里您具有链接:

Codepen Example