javascript - Owl Carousel 2 : Add title, 单击并移动到相应的项目

标签 javascript html jquery owl-carousel owl-carousel-2

enter image description here

我正在使用 Owl Carousel 2 , 中央。如图所示,我需要一些帮助。基本上我有 6 个项目,其中 2 个在同一类别下

  • 标题 1:1 和 1a
  • 标题 2:2 和 2a
  • 标题 3:3 和 3a

点击“Title 1”会移动到1,但不会移动到1a,点击“Title 2”会移动到2,以此类推。

希望你们中的一些人能给我一些建议。谢谢!

$(document).ready(function($) {
    $('.loop').owlCarousel({
      center: true,
      items: 2,
      loop: true,
      margin: 10,
      nav: true,
      responsive: {
        600: {
          items: 5
        }
      }
    });
 });
h4{
    border: 1px solid black;
    text-align: center;
}
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet"/>
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js"></script>
<a href="#" class="title1">Title 1</a>
<a href="#" class="title2">TItle 2</a>
<a href="#" class="title3">TItle 3</a>
<div class="loop owl-carousel owl-theme">
  <div class="item">
    <h4>1</h4>
  </div>
  <div class="item">
    <h4>1a</h4>
  </div>
  <div class="item">
    <h4>2</h4>
  </div>
  <div class="item">
    <h4>2a</h4>
  </div>
  <div class="item">
    <h4>3a</h4>
  </div>
  <div class="item">
    <h4>3</h4>
  </div>
</div>

最佳答案

以这种方式更改轮播中的项目代码:

<div class="loop owl-carousel owl-theme">
  <div class="item" data-hash="one">
    <h4>1</h4>
  </div>
  <div class="item">
    <h4>1a</h4>
  </div>
  <div class="item" data-hash="two">
    <h4>2</h4>
  </div>
  <div class="item">
    <h4>2a</h4>
  </div>
  <div class="item">
    <h4>3a</h4>
  </div>
  <div class="item" data-hash="three">
    <h4>3</h4>
  </div>
</div>

标题是这样变化的:

<a href="#one" class="title1">Title 1</a>
<a href="#two" class="title2">TItle 2</a>
<a href="#three" class="title3">TItle 3</a>

而Carousel的初始化是这样的:

$(document).ready(function($) {
    $('.loop').owlCarousel({
      center: true,
      items: 2,
      loop: true,
      margin: 10,
      nav: true,
      URLhashListener: true, //1. this string added
      startPosition: 'URLHash', //2. this string added
      responsive: {
        600: {
          items: 5
        }
      }
    });
 });

更多细节参见官方文档:https://owlcarousel2.github.io/OwlCarousel2/demos/urlhashnav

关于javascript - Owl Carousel 2 : Add title, 单击并移动到相应的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68725056/

相关文章:

html - 如何设置字段位置?

jquery - Google事件跟踪代码,对吗?

javascript - 给定一个字符串 S,返回 "reversed"字符串,其中所有不是字母的字符都保持在同一个位置,并且所有字母反转它们的位置

javascript - 当用户滚动到特定元素时触发事件 - 使用 jQuery

javascript - 为什么我没有收到序列化()的响应?

html - Linear Gradient SVG 3种颜色增加中间颜色的宽度

javascript - Click 事件是否穿透到 <html> 元素?

jQuery : How to find the url that the user has typed in the browsers address-bar during body-unload event?

jquery验证引擎日期格式函数

javascript - javascript 的 "on the fly"编译实际上是如何工作的?