Javascript 如何在 Slick Carousel 中的事件中获取幻灯片元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29128331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a slide element in an event, in Slick Carousel?
提问by Yuval A.
Slick Carousel: http://kenwheeler.github.io/slick/
光滑的旋转木马:http: //kenwheeler.github.io/slick/
How can I get the element of the slide in an event? For example:
如何在事件中获取幻灯片的元素?例如:
.on('afterChange', function (slick, currentSlide)
{
var currentSlideElement = //Get current slide element here
});
The first argument seems to be the eventobject, but its target(or currentTarget) is always the slides container, and second argument seems to be the slick object....
第一个参数似乎是event对象,但它的target(或currentTarget)始终是幻灯片容器,第二个参数似乎是光滑的对象....
回答by Abraham Uribe
you need to include the first argument "event" like this
您需要像这样包含第一个参数“事件”
$('.your-element').on('afterChange', function(event, slick, currentSlide){
console.log(currentSlide);
});
or else your "currentSlide" argument will be "slick"
否则你的“currentSlide”参数将是“slick”
回答by Vénizia
To get the DOM element on 'afterChange' event:
在 'afterChange' 事件上获取 DOM 元素:
$('.your-element').on('afterChange', function (event, slick, currentSlide) {
var elt = slick.$slides.get(currentSlide);
});
回答by Leonid Usachov
// Get the current slide
var currentSlide = $('.your-element').slick('slickCurrentSlide');
回答by anotheruser1488182
The 'currentSlide' parameter does notwork as expected if you are displaying multiple slides at once.
该“currentSlide”参数确实没有工作,如果你是在一次显示多张幻灯片预期。
The way I have found in this instance is with the 'slick-current' class that gets added to the current slide.
我在这个实例中找到的方法是将“slick-current”类添加到当前幻灯片中。
e.g. where slideris the HTML element wrapper for you carousel:
例如,slider您轮播的 HTML 元素包装器在哪里:
$('.your-element').on('afterChange', function(event, slick, currentPage){
var currentIndex = parseInt(slider.querySelector('.slick-current').getAttribute('data-slick-index'));
});

