gpt4 book ai didi

javascript - 水平滑动页面需要在点击下一个按钮时设置位置顶部

转载 作者:行者123 更新时间:2023-12-01 08:30:23 24 4
gpt4 key购买 nike

我使用Owl Carousel创建了水平滚动页面。它包含底部的下一页和上一页按钮。

我需要的是:

当我们单击按钮时,页面顶部应该可见。现在它显示下一页/上一页的底部。

这是我的代码:

$('.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

祝你有美好的一天..!

保持安全@Home

最佳答案

您可以使用 translated.owl.carousel 事件在页面更改时调用您的函数:

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

检查以下示例:

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>

关于javascript - 水平滑动页面需要在点击下一个按钮时设置位置顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61119482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com