使中间项目活跃在实现轮播中

时间:2019-02-10 14:32:41

标签: css carousel materialize

我有一个带有实现CSS的轮播。我想从中间项目开始轮播。但是轮播是从第二项开始的。 轮播的链接:link 我能做什么? 预先感谢!

1 个答案:

答案 0 :(得分:0)

请下次添加一段代码,以更好地了解...

顺便说一句...您可以使用instance.set(x)将轮播移动到第n张幻灯片(如文档here中所述)

这里是一个示例(只需注释instance.set(middle_slide);语句以查看区别):

document.addEventListener('DOMContentLoaded', function() {
  //find wich slide is the middle one
  var carousel_items = document.querySelectorAll('.carousel-item').length;
  var middle_slide = Math.round(carousel_items / 2) 
  
  console.log('The slide in the middle is the ' + middle_slide + ' out of ' + carousel_items)
  
  //Carousel initialization
  var options = {};
  var elems = document.querySelectorAll('.carousel');
  var instances = M.Carousel.init(elems, options);
  
  //Set the carousel to show the middle slide first
  var elem = document.querySelector('.carousel');
  var instance = M.Carousel.getInstance(elem);
  instance.set(middle_slide);
})
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<div class="carousel">
  <a class="carousel-item" href="#one!"><img src="https://lorempixel.com/250/250/nature/1"></a>
  <a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2"></a>
  <a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3"></a>
  <a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4"></a>
  <a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5"></a>
</div>

如果您愿意,这里是codepen:https://codepen.io/LukeSavefrogs/pen/WPYOGE?editors=1010