Adjusting the page view when a collapse is opened

I am working on a website that features 3 images, each of which opens a collapse below with an embedded iframe website inside when clicked. I am trying to implement a function where the site automatically scrolls down to the opened iframe when an image is clicked. So far, I have only found solutions for accordions.

The iframes display a 3D tour, and I want to ensure that only one tour is visible at a time. To demonstrate this concept, I have created a basic demonstration using placeholder images and a single tour link: https://codepen.io/jvern22/pen/BGzgjw

Here is a snippet of my code depicting an image linking to an iframe; there are three instances of this structure:

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

Below is the code for the iframe element, replicated thrice to correspond with each image:

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

I am still relatively new to coding, so I hope I have articulated my query effectively.

Answer №1

Utilizing some JQuery code could assist in achieving your desired outcome.

To start, we can establish a listener for the click event on images that trigger a collapsible element to open. Subsequently, when one is clicked, it will close the currently visible collapsible element. This ensures only one collapsible element remains open at any given time. Here is an example of the code:

// Set up a click listener for images triggering collapsible elements.

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

Furthermore, upon a collapsible element becoming fully visible to the user, it triggers the shown.bs.collapse event. By listening to this event, we can automatically scroll down to the visible collapsible element using the following 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
    );
});

Feel free to interact with a functional demonstration (view in fullscreen mode):

This code snippet can also be accessed on Codepen. I replaced the slim version of JQuery with the full version to ensure the animate() function works correctly. Click on the link below:

Codepen Example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update my function to be asynchronous by changing async: false to async: true using JQuery and Ajax

I've created a function in a JavaScript class that accesses a PHP file to retrieve information from a database. Although this function works well, I attempted to set the property async: true, and it caused the function to stop working. (I received th ...

Secure an input field for exclusive attention. React

How can I lock the focus of my input field? I attempted using the following code: onBlur={this.click()} However, it was not successful. What is the correct way to accomplish this? ...

Approaching fashion from a new angle: incorporating perspective into

How can I style elements in the perspective to appear straight(3d) instead of flat due to perspective? I want to achieve a design where the alphabets and flag post image align perfectly. https://i.sstatic.net/HcHMC.jpg .container { perspective: 100p ...

"Encountering issues with $http.get() function in my controller while trying to

Currently, I am working on an Application that utilizes Angularjs. I am trying to call an API and retrieve data using $http.get(). However, I am facing an issue where my API url does not seem to work properly. Interestingly, when I use a URL from a tutoria ...

Determine the time difference between two dates, taking into account Daylight Saving Time adjustments

If a start date and number of days are given, I need to calculate the end date by adding the number of days to the start date. This is the code snippet I used: var endDate=new Date(startDate.getTime()+ONE_DAY); Everything was working fine until I notice ...

Re-establishing Scroll Functionality Post Ajax Request Disruption caused by prettyLoader

I have created a jQuery infinite scroll function that follows the infinite scroll design pattern. After the ajax server call is completed, I am trying to rebind the scroll event. Although everything works fine for the first ajax call, the scroll event is ...

Sending a URL through the $.post method to various websites

Trying to populate my address using JavaScript $.post('/Endereco/getEnderecos', { CardCode: dado }, function (data) { if (data) { (...) Encountering an issue: when I host my website on 'localhost', it works fine, but when host ...

Determining the identity of an HTML element using its surrounding values

I have a webpage with a table displaying various services and their corresponding download buttons. Here is an example of the content: nov. service 1 [ click to download pdf ] nov. service 5 [ click to download pdf ] nov. service 3 [ ...

Using Nodes to Populate an Array with References to Objects

How can I populate the courses of a Student in StudentSchema with courses (Object_id) from Course in CourseSchema that belong to the same major as the student? let StudentSchema = new Schema({ _id: new Schema.Types.ObjectId, emplId: { type: ...

The CSS scrollbar is visible on the screen, but for some reason,

I'm seeking a solution to enable scrolling within a div container that can scroll both horizontally and vertically. body { height: 100%; width: 100%; overflow: hidden; } .wrapper { width: 75%; padding: 0px 5px 0px 8px; top: 20px; whit ...

Enhance Data Filtering in Ag-Grid Using Vue.js

Seeking assistance with Ag Grid in Vue js. I have a scenario where I want to disable the checkbox in the filter upon initial load so that the grid does not display records initially. Is this achievable? For example, in the screenshot provided in the link ...

designing the border at the bottom of the existing menu selection

I am trying to enhance the look of my current-menu-item div by adding a border at the bottom. It seems simple enough to achieve this by simply including the border in the div. However, I'm facing an issue with the width and position of the border. Ide ...

Duplicate key error in Node.js/Express with req.param

Whenever my node.js/express server encounters an error due to the failure of saving an object (through a post request), I noticed that the req.param key ends up being duplicated. Take, for instance, my node.js code: User.create({ username: req.param("use ...

Execute JavaScript function with a delay once the webpage has finished loading

I am currently working on a basic function that runs when the page is loaded. However, even though the page has finished loading, there are times when it takes about half a second for the content to actually appear. While this typically wouldn't be an ...

Can someone assist me with updating the image source for it to display correctly within the div element?

I am currently working on a code that displays the URL (data-owner_logo) in my div after indexing JSON. Rather than just displaying the URL string, I want the actual logo image to appear in the div: $('a').on('click', function () { ...

Detecting collisions in JavaScript

Currently, I am in the process of reviving an old game reminiscent of Tron using HTML5 canvas and JavaScript. The unique twist in this version is that the snakes have the ability to move in curves rather than right angles (known as Achtung Die Kurve). The ...

What is the process for setting up a vertical carousel in Bootstrap 5 with a stationary previous image?

Looking for assistance with my vertical carousel project. Is there a way to create a vertical carousel in bootstrap 5 with the previous image fixed? I found a slider on this website: zara.com/jp/en/ I want to maintain the previous image in a fixed posit ...

Get a file from a distinct internal server than the one where the website is operating

I am trying to download a file from an internal server that is separate from the one where the website is hosted. I have attempted various methods such as... <a href="javascript:Start('file://servername/path/filename.txt')> <a href="// ...

Center align text in the uib-progressbar

Is it possible to center align text in the uib-progressbar? I attempted using position:absolute, but I would like to display a list of progress bars in a UI grid that includes scrollable content. The goal is for the text to remain fixed while the progress ...

When using VueJS checkboxes with object values bound to an array property, they do not get removed from the array when unchecked

Within my VueJS component, I am utilizing an array named selectedJobs to track checked checkboxes in an HTML table. The data displayed in the table is sourced from an array of objects called replenJobsList. /* My Component */ <template> ...