Search code examples
javascriptjqueryhtmlcssowl-carousel

Horizontal slide pages needs to set position top when click on the next button


I have created horizontal scrolling pages using Owl Carousel. It contains page next & previous buttons at the bottom.

What I need is:

When we click on the button, the top of the page should be visible. Now it shows the bottom part of the next/prev page.

Here is my 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'
    });
.owl-carousel {
  position: relative;
}
.page {
    height: 10rem;
    padding: 1rem;
    height: 1500px;
}
.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;
}
<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">
    Button at the bottom!
    <a href="#page2" class="linkB">Page 2</a>
    </div>
    
    <div class="page two" data-hash="page2">
    Button at the bottom!
    <a href="#page1" class="linkA">Page 1</a>
    <a href="#page3" class="linkB">Page 3</a>
    </div>
    
    <div class="page three" data-hash="page3">
    Button at the bottom!
    <a href="#page2" class="linkA">Page 2</a>
    </div>
</div>

JSFiddle

Have a great day..!

StaySafe@Home


Solution

  • You can use the translated.owl.carousel event to call your function when the page changed:

    owl.on('translated.owl.carousel', function(event) {
        window.scrollTo(0, 0);
    })
    

    Check the following example:

    var owl = $('.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'
    });
    owl.on('translated.owl.carousel', function(event) {
        window.scrollTo(0, 0);
    })
    .owl-carousel {
      position: relative;
    }
    .page {
        height: 10rem;
        padding: 1rem;
        height: 1500px;
    }
    .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;
    }
    <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">
        Button at the bottom!
        <a href="#page2" class="linkB">Page 2</a>
        </div>
        
        <div class="page two" data-hash="page2">
        Button at the bottom!
        <a href="#page1" class="linkA">Page 1</a>
        <a href="#page3" class="linkB">Page 3</a>
        </div>
        
        <div class="page three" data-hash="page3">
        Button at the bottom!
        <a href="#page2" class="linkA">Page 2</a>
        </div>
    </div>