Navigate through FAQ items with sliders using Owl Carousel 2 - Easily switch between different chapters

Exploring the creation of a FAQ section with individual carousels for each item. My primary objective is for the carousel to transition to the next chapter when advancing beyond the last slide (backwards navigation would be a future enhancement).

I've been working on a solution, but there seems to be a persistent bug. When jumping directly to the final slide and then proceeding forward, it restarts at the initial slide rather than progressing to the next chapter.

If anyone could provide assistance or insight into this issue, it would be greatly appreciated.

Visit JSFiddle

$(document).ready(function() {
    //initialize the slider class
  $('.owl-carousel').owlCarousel({
      loop:true,
      margin:10,
      nav:true,
      autoHeight:true,
      responsive:{
          0:{
              items:1
          },
          600:{
              items:1
          },
          1000:{
              items:1
          }
      }
  })


var carousel;
var current;
var lastItem = 0;


//on click of a setup item do
$('.faq-item-title').click(function(){
  $(this).next().fadeToggle();
  lastItem = 0;
});



//open next chapter
$('.owl-carousel').on('changed.owl.carousel', function(e) {
  var carousel = e.relatedTarget,
  current = carousel.current();
  console.log('ist: ' + e.page.index);
  console.log('last: ' + lastItem);
  console.log('total: ' + (e.page.count + 1));
  console.log('--');
  if (e.page.index == 0 && lastItem == e.page.count + 1){
    $(this).parent().fadeOut();
 //   $(this).parent().prev().removeClass('faq_active').addClass('faq_inactive');
    $(this).parent().parent().next().children().next().fadeIn();
  //  $(this).parent().parent().parent().next().children().children().removeClass('faq_inactive').addClass('faq_active');
    lastItem = 0;
  }
  lastItem = current;
    });
});
body {
 background: linear-gradient(to right, #33ccff 0%, #ff99cc 100%);
}

.faq-item {
  border: 1px solid gray;
  background: rgba(255,200,255,0.2);
  font-family: 'Comic Sans MS';
}

.faq-item-title {
  cursor: pointer;
  padding-left: 10px;
  user-select: none;
}

.faq-item-content {
  display: none;
  text-align: center;
  margin-bottom: 20px;
}

.displayBlock {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
<div class="wrap">
  <div class="faq-item">
    <div class="faq-item-title">
      <p>FAQ Item 1</p>
    </div>
    <div class="faq-item-content displayBlock">
      <div class="owl-carousel owl-theme">
        <div class="item">Chapter 1: Text A</div>
        <div class="item">Chapter 1: Text B</div>
        <div class="item">Chapter 1: Text C</div>
        <div class="item">Chapter 1: Text D</div>
      </div>      
    </div>
  </div>
    <div class="faq-item">
    <div class="faq-item-title">
      <p>FAQ Item 2</p>
    </div>
    <div class="faq-item-content">
      <div class="owl-carousel owl-theme">
        <div class="item">Chapter 2: Text A</div>
        <div class="item">Chapter 2: Text B</div>
        <div class="item">Chapter 2: Text C</div>
        <div class="item">Chapter 2: Text D</div>
      </div>  
    </div>
  </div>
      <div class="faq-item">
    <div class="faq-item-title">
      <p>FAQ Item 3</p>
    </div>
    <div class="faq-item-content">
      <div class="owl-carousel owl-theme">
        <div class="item">Chapter 3: Text A</div>
        <div class="item">Chapter 3: Text B</div>
        <div class="item">Chapter 3: Text C</div>
        <div class="item">Chapter 3: Text D</div>
      </div>  
    </div>
  </div>
</div>

Answer №1

It took me a while to figure this out, but I believe this is the ultimate solution.

$(document).ready(function() {
    //initialize the slider class
  $('.owl-carousel').owlCarousel({
      loop:true,
      margin:10,
      nav:true,
      autoHeight:true,
      responsive:{
          0:{
              items:1
          },
          600:{
              items:1
          },
          1000:{
              items:1
          }
      }
  })


  var carousel = 0;
  var current = 0;
  var lastItem = 0;


//on click of a setup item do
$('.faq-item-title').click(function(){
  $(this).next().fadeToggle();
  lastItem = 0;
});



//open next chapter
$('.owl-carousel').on('changed.owl.carousel', function(e) {
  current = e.page.index;
  console.log('ist: ' + e.page.index);
  console.log('last: ' + lastItem);
  console.log('total: ' + (e.page.count + 1));
  console.log('--');
  if (e.page.index == 0 && lastItem  == e.page.count - 1){
    $(this).parent().fadeOut();
 //   $(this).parent().prev().removeClass('faq_active').addClass('faq_inactive');
    $(this).parent().parent().next().children().next().fadeIn();
  //  $(this).parent().parent().parent().next().children().children().removeClass('faq_inactive').addClass('faq_active');
    lastItem = 0;
  }
  lastItem = current;
    });
});
body {
 background: linear-gradient(to right, #33ccff 0%, #ff99cc 100%);
}

.faq-item {
  border: 1px solid gray;
  background: rgba(255,200,255,0.2);
  font-family: 'Comic Sans MS';
}

.faq-item-title {
  cursor: pointer;
  padding-left: 10px;
  user-select: none;
}

.faq-item-content {
  display: none;
  text-align: center;
  margin-bottom: 20px;
}

.displayBlock {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
<div class="wrap">
  <div class="faq-item">
    <div class="faq-item-title">
      <p>FAQ Item 1</p>
    </div>
    <div class="faq-item-content displayBlock">
      <div class="owl-carousel owl-theme">
        <div class="item">Chapter 1: Text A</div>
        <div class="item">Chapter 1: Text B</div>
        <div class="item">Chapter 1: Text C</div>
        <div class="item">Chapter 1: Text D</div>
      </div>      
    </div>
  </div>
    <div class="faq-item">
    <div class="faq-item-title">
      <p>FAQ Item 2</p>
    </div>
    <div class="faq-item-content">
      <div class="owl-carousel owl-theme">
        <div class="item">Chapter 2: Text A</div>
        <div class="item">Chapter 2: Text B</div>
        <div class="item">Chapter 2: Text C</div>
        <div class="item">Chapter 2: Text D</div>
      </div>  
    </div>
  </div>
      <div class="faq-item">
    <div class="faq-item-title">
      <p>FAQ Item 3</p>
    </div>
    <div class="faq-item-content">
      <div class="owl-carousel owl-theme">
        <div class="item">Chapter 3: Text A</div>
        <div class="item">Chapter 3: Text B</div>
        <div class="item">Chapter 3: Text C</div>
        <div class="item">Chapter 3: Text D</div>
      </div>  
    </div>
  </div>
</div>

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

Creating a redux store with an object using typescript: A step-by-step guide

Having recently started using Redux and Typescript, I'm encountering an error where the store is refusing to accept the reducer when working with objects. let store = createStore(counter); //error on counter Could this be due to an incorrect type set ...

Request successful but receiving no response text and content length is zero

let req = new XMLHttpRequest(); req.addEventListener('load', function (data) { console.log(data) }, false); req.open("get", "/bar.txt", true); req.send(); Feeling a bit perplexed at the moment, I can't seem to figure out what's going ...

Glitch found in Safari involving innerText of elements

Hey everyone, I posted this question not too long ago but now I have some images to share regarding the issue with Safari. When checking the console in Safari, the following text is displayed: <div id="rot3posDisp" class="rotDisp">C</div> Ho ...

React Native: Avoiding Infinite Loops in useEffect

I am facing an issue where my app goes into an infinite loop because pointsData is inside the useEffect function. How can I resolve this situation? function useGetPoints() { const [pointsData, setPointsData] = useState<PointTbleType[]>([]); ...

Tips on creating reusable scoped styles for 3 specific pages in Vue.js without impacting other pages

Our project includes global classes for components that are utilized throughout the entire system. <style lang="scss" scoped> .specialPages { padding: 0 10px 30px 10px; } /deep/ .SelectorLabel { white-space: nowrap; al ...

Error Alert: React Native object cannot be used as a React child within JSON, leading to an Invariant Violation

When using React-Native: To start, here is the example code of a json file: Any placeholders marked with "..." are for string values that are not relevant to the question. [ { "id": "question1" "label": "..." "option": [ { "order": 1, "name": "..."}, ...

Troubleshooting a navigation issue in Angular involving the mat-sidenav element of material UI while transitioning between components

I would greatly appreciate your assistance in resolving the issue I am facing. I have been trying to navigate from one view to another, but when I use an <a> tag nested within a <mat-sidenav>, I encounter the following error: "Uncaught (in prom ...

Retrieve all the keys from an array of objects that contain values in the form of arrays

Looking for an efficient way to extract all keys from an array of objects whose values are arrays, without any duplicates. I want to achieve this using pure JavaScript, without relying on libraries like lodash or underscore. Any suggestions on how to impro ...

Every time I attempt to send Ajax jQuery parameters in a POST request with MVC 5, I am met with receiving NULL values

When attempting to send the subscriberemail parameter to the action result in the controller using AJAX jQuery parameters, I am consistently receiving a null value. Here is the code snippet: $("#button-subscribe-newsletter").click(function () { debug ...

Using Jquery to run a jquery script stored in a variable within jQuery syntax

This question may seem a bit confusing, so allow me to explain further. I am trying to execute a jquery script that is written in a text file obtained through an ajax request. For example, the code I receive from the ajax request looks like this: ($($(" ...

Position the Bootstrap icon to float to the far right

My icon is always positioned to the right instead of beside the input field. Take a look at the image below: https://i.stack.imgur.com/Bb1eH.png The icon does not align next to the input field. This is my code snippet : <div class="form-horizontal"& ...

Is it possible to modify a JavaScript array variable without having to refresh the page by utilizing Ajax?

Is it possible to update a JavaScript array variable without having to reload or refresh the page? I have an existing list of data in an array, and I retrieve additional data using PHP and Ajax. Now I want to add the new data obtained from PHP Ajax to th ...

Every time I attempt to submit the login form on the Ionic and Angular page, instead of capturing the values of the form group, the page simply refreshes

Struggling with submitting the login form in Ionic and Angular? When attempting to submit, the page reloads instead of capturing the form group values. I am utilizing angular reactive forms and form builder within the ionic framework. Need assistance in id ...

Changes influencing independent div elements

My goal is to design a front-end screen with images labeled steps 1-4 lined up next to each other. The concept is that when the user hovers over one of these steps, the corresponding image should fade in below them in a separate div. Upon removing the curs ...

Can different classes be assigned as "dragenter" targets?

Is it possible to apply the Jquery "dragenter" event to multiple targets or classes simultaneously? I tried this approach, but it doesn't seem to be working: $('.list').on('dragenter','.class1, .class2', function(e) { ...

Is there a way to create an <a> element so that clicking on it does not update the URL in the address bar?

Within my JSP, there is an anchor tag that looks like this: <a href="patient/tools.do?Id=<%=mp.get("FROM_RANGE") %>"> <%= mp.get("DESCRITPION") %></a>. Whenever I click on the anchor tag, the URL appears in the Address bar. How can ...

Using jQuery to manipulate input arrays that are dynamically generated

My form is dynamic and consists of the following fields: <tr> <td><input name = "qty[]" /></td> <td><input name = "color[]" /></td> <td><input name = "price[]" /></td> <td><input nam ...

Encountering Issues with Accessing Property

Upon trying to run my code, the console is displaying an error that I am unable to resolve. The error specifically states: "TypeError: Cannot read property 'author' of undefined." View the StackBlitz project here The error seems to be coming fr ...

Exploring the world of interactive storytelling through a basic PHP text adventure

I recently completed coding a text adventure using functions like fgets(STDIN) that works in the command line. Now, I want to convert it to run on HTML (internet browser) but I'm facing a challenge. I am unsure how to transition from this: $choose=0; ...

Is there a way to transform NextJS typescript files into an intermediate machine-readable format without having to build the entire project?

I need to deliver a Next.js project to my client, but I want to modify the TypeScript files so they are not easily readable by humans. The client will then build and deploy these files to their production environment. How can I achieve this? In summary, C ...