Issue with fixed positioning in Owl carousel (unable to fixed position prev and next buttons on carousel)

Is there a way to keep the <div class="btn"> in a fixed position at the center of the screen, especially on small devices, despite conflicting styles from classes within the owl carousel?

I attempted to solve this by using jQuery to append the button div element to the body:

jQuery('.btn').appendTo("body");

However, this caused conflicts with other buttons, displaying all buttons on the first page instead.

How can I resolve this issue?

Code:

$('.owl-carousel').owlCarousel({
  items: 1,
  autoplay: false,
  loop: false,
  nav: false,
  dots: false,
  center: true,
  mouseDrag: false,
  touchDrag: false,
  autoHeight: true,
  margin: 10,
  smartSpeed: 500,
  URLhashListener: true,
  autoplayHoverPause: true,
  startPosition: 'URLHash'
});
body {
  position: relative;
}
.owl-carousel {
  position: relative;
}

.page {
  height: 10rem;
  padding: 1rem;
  height: 2000px;
}

.one {
  background: #bec4bd;
}

.two {
  background: green;
}

.three {
  background: #41253c;
}

.linkA {
  position: absolute;
  bottom: 10px;
  left: 10px;
  text-decoration: none;
  background: red;
  color: #fff;
  padding: 5px 15px;
  margin-right: 5px;
  font-family: sans-serif;
  font-size: 14px;
}

.linkB {
  position: absolute;
  bottom: 10px;
  right: 10px;
  text-decoration: none;
  background: red;
  color: #fff;
  padding: 5px 15px;
  margin-right: 5px;
  font-family: sans-serif;
  font-size: 14px;
}
.btn {
  position: fixed;
  top: 20%;
  left: 10px;
  right: 10px;
  width: 100%;
  z-index: 100;
}
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet" />
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet" />
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/vendors/jquery.min.js"></script>
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js"></script>
<div class="owl-carousel owl-theme">
  <div class="page one" data-hash="page1">
    Please see the button position!
    <div class="btn">
    <a href="#page2" class="linkB">Page 2</a>
    </div>  
  </div>

  <div class="page two" data-hash="page2">
    Please see the button position!
    <div class="btn">
    <a href="#page1" class="linkA">Page 1</a>
    <a href="#page3" class="linkB">Page 3</a>
    </div>  
  </div>

  <div class="page three" data-hash="page3" style="color:#fff;">
    Please see the button position!
    <div class="btn">
    <a href="#page2" class="linkA">Page 2</a>
    </div> 
  </div>
</div>

JsFiddle

Answer №1

One solution to your issue is to utilize the scroll feature within the carousel and apply a position: sticky property.

$('.owl-carousel').owlCarousel({
  items: 1,
  autoplay: false,
  loop: false,
  nav: false,
  dots: false,
  center: true,
  mouseDrag: false,
  touchDrag: false,
  autoHeight: true,
  margin: 0,
  smartSpeed: 500,
  URLhashListener: true,
  autoplayHoverPause: true,
  startPosition: 'URLHash'
});
body {
  position: relative;
  margin:0;
}
.owl-carousel {
  position: relative;
}

.page {
  height: 10rem;
  padding: 1rem;
  height: 2000px;
}

.one {
  background: #bec4bd;
}

.two {
  background: green;
}

.three {
  background: #41253c;
}

.linkA {
  position: absolute;
  bottom: 10px;
  left: 10px;
  text-decoration: none;
  background: red;
  color: #fff;
  padding: 5px 15px;
  margin-right: 5px;
  font-family: sans-serif;
  font-size: 14px;
}

.linkB {
  position: absolute;
  bottom: 10px;
  right: 10px;
  text-decoration: none;
  background: red;
  color: #fff;
  padding: 5px 15px;
  margin-right: 5px;
  font-family: sans-serif;
  font-size: 14px;
}
.btn {
 position: sticky;
  top: 50%;
  left: 10px;
  right: 10px;
  width: 100%;
  z-index: 100;
}
.owl-carousel .owl-item {
    overflow-y: scroll;
    height: 100vh;
}
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet" />
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet" />
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/vendors/jquery.min.js"></script>
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js"></script>
<div class="owl-carousel owl-theme">
  <div class="page one" data-hash="page1">
    Please see the button position!
    <div class="btn">
    <a href="#page2" class="linkB">Page 2</a>
    </div>  
  </div>

  <div class="page two" data-hash="page2">
    Please see the button position!
    <div class="btn">
    <a href="#page1" class="linkA">Page 1</a>
    <a href="#page3" class="linkB">Page 3</a>
    </div>  
  </div>

  <div class="page three" data-hash="page3" style="color:#fff;">
    Please see the button position!
    <div class="btn">
    <a href="#page2" class="linkA">Page 2</a>
    </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

PHP not receiving posted value from $.ajax() function

My Ajax post request seems to be causing an issue with the PHP script as the post data is not being received correctly. Whenever I try to execute it, I receive a 500 internal server error along with "XHR failed loading: POST". This is a snippet of my Java ...

JavaScript, the art of escaping strings

I am dealing with the following string: 'You've just created' When I store this string in a JavaScript variable, it is seen as 2 separate strings due to the presence of a ' character. Is there a way to properly escape it? ...

Allow the json array to be transformed into a format suitable for input

Hey there, I've received a JSON array as the response of my ajax request: {"chart":{"2016-03-08":"1","2016-03-07":"4","2016-03-06":0,"2016-03-05" :0,"2016-03-04":0,"2016-03-03":"145","2016-03-02":0}} Now, I'm looking to create a chart using the ...

Having trouble utilizing JQuery to toggle the visibility of the div closest to a checkbox

Currently, I am utilizing Ruby on Rails along with the Cocoon Gem for generating dynamic forms. Specifically, users are able to add or remove their Education details using this form. Here is an example of how my form appears: <div class='nested-fi ...

A helpful guide on displaying array elements using the map method

I'm struggling to display all the elements from the 'monday' array in the paragraph. How can I modify the code to achieve this and could you please explain how to combine these methods? Thanks! ` let mondayList = document.querySelector(&qu ...

Ways to streamline your Jquery/Ajax code: Eliminate repetitive sections

Seeking guidance on optimizing my Jquery/Ajax code that involves sending GET requests. I've noticed there's quite a bit of repetition. How can I streamline this code effectively? $(".add_to_cart").click(function() { product_slug = $(this).at ...

Is there a way to retrieve all the selected values from multiple select inputs when a button is clicked in React?

I am currently working on developing a questionnaire where the select inputs' values are collected and averaged to generate a final score. Although it may sound simple, I'm facing difficulties in retrieving these values. At this point, my primary ...

Is it permissible to utilize multiple ":root" selectors in a CSS file?

One of the features in TWBS 4 is the presence of color variables within the :root selector in bootstrap.css. Is it possible for my child stylesheet to also include a :root selector, allowing me to define my own variables? I assume that :root {} can be ov ...

Is there a way to retrieve the controller instance linked to a directive within the link function?

Is there a way to retrieve the controller instance connected with a directive within the link function? return { template: template, controller: controller, controllerAs: 'myCtrl', // What is the method for ac ...

What's causing my inability to establish a connection with MongoDB Atlas?

After using MySQL for my entire life, I am now diving into MongoDB. I have set up an account in Atlas, configured the IP to match mine, created a user, and saved the password. Why isn't my code working? Here's my app.js: const express = r ...

The function of AJAX is to send and receive data asynchronously without

Recently, I was experimenting with AJAX. When I use echo "hello" in my PHP file, everything works perfectly. However, if I try something like echo "<script language=Javascript> alert('hi');</script>"; in the PHP file, the alert ...

Simple steps to change the appearance of the delete button from an ajax button to an html button

I need help transitioning the delete button from an ajax button to an html button in my code. Currently, the delete button functions using ajax/javascript and when clicked, a modal window pops up asking for confirmation before deleting the vote. However, ...

Customizing CSS to differentiate the color of active card headers in a Bootstrap accordion

I currently have a basic bootstrap accordion set up, as shown below. My goal is to style the .card-header element of only the expanded .card section without impacting the other .card-header elements. Is there a way to specifically target the expanded ite ...

What could be the reason behind the lack of vertical centering for the Spartan

I can't seem to figure out why my font isn't aligning vertically correctly. I'm using the Spartan MB font from Google, but it just doesn't look right, you can see here. https://i.stack.imgur.com/wbLc2.png This is my HTML code: <htm ...

Is the WordPress error message "wp_register_style was improperly called" showing up on

I am facing an issue while trying to incorporate this code into my initial Wordpress template. It seems that the libraries for Bootstrap and my custom styles are not functioning as expected. Here is the code snippet in question. Any insights would be great ...

Ensuring Compatibility of jQuery with Internet Explorer

Attempting to display a long and intricate source code, I have decided to share a link to the problematic page instead. In Google Chrome or newer Firefox versions (5 or 6), the jQuery in this script functions as intended. However, issues have arisen in IE ...

Effortless scrolling implementation

How can I make the scroll smooth and animated with this code structure? Check out my JSFiddle here: https://jsfiddle.net/v7qhzo8q/ The code structure is as follows: <nav id="nav"> <ul id="top"> <li><a href="#">Home</a> ...

In the realm of Typescript Angular, transferring the value of an object's property to another property within the

I'm working with a large TypeScript object and I am hoping to automate certain parts of it to streamline my workflow. myObject = [ { id: 0, price: 100, isBought: false, click: () => this.buyItem(100, 0) } buyItem (it ...

Combining numerous draggable and droppable functionalities onto a single element

I attempted to include two draggable stop event handlers in a paragraph. However, only the second one seems to fire. How can I ensure that both of them trigger? <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jq ...

The <img> tag needs to dynamically adjust its size based on the width of its parent element, while ensuring it does not exceed its original dimensions

I spent hours working on my tribute page project on codepen and managed to pass 9 test cases. However, no matter what I try, I just can't seem to pass the last test case. My CSS code: #img-div { display: block; filter: grayscale(100%); width: 1 ...