简体   繁体   English

圆滑的旋转木马选择选项

[英]Slick carrousel select option

Hi i want to link a carrousel to a input value option how can i do that ? 嗨,我想将转盘链接到输入值选项,我该怎么做?

here is my carrousel : 这是我的旋转木马:

$('.slider-for').slick({
 slidesToShow: 1,
 slidesToScroll: 1,
 arrows: false,
 fade: true,
 asNavFor: '.slider-nav'
});

$('.slider-nav').slick({
slidesToShow: 3,
slidesToScroll: 1,
asNavFor: '.slider-for',
dots: true,
centerMode: true,
focusOnSelect: true
});

and my input value : 和我的输入值:

<input id="test" name="test" value="changevalue">

the slider-for will contain image i want to link with the input value 滑块将包含我要与输入值链接的图像

so i want my value to be change and synchronize with the "slide-for" 所以我想改变自己的价值,并与“幻灯片”同步

You have to use afterChange event, it will fire after any slide change, and then you can use currentSlide parameter to get the active slide (img) and then update your input value, here is a working example: 您必须使用afterChange事件,它会在任何幻灯片更改后触发,然后可以使用currentSlide参数获取活动幻灯片(img),然后更新您的输入值,这是一个工作示例:

 $(document).ready(function(){ $('.slider-for').slick({ slidesToShow: 1, slidesToScroll: 1, }); var $input = $('#test'); $('.slider-for').on('afterChange', function(event, slick, currentSlide) { // get the active slide var $activeImg = $("[data-slick-index='" + currentSlide + "']").children('img'); // update input value $input.val( $activeImg.attr('src') ); }); }); 
 .slider-for{ margin: 50px; } #test { width: 300px; } body { background: #3498db; } 
 <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick.css"/> <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick-theme.css"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick.min.js"></script> <div class="slider-for"> <div><img alt="" src="http://via.placeholder.com/350x100"></div> <div><img alt="" src="http://via.placeholder.com/400x100"></div> <div><img alt="" src="http://via.placeholder.com/450x100"></div> </div> <input id="test" name="test" value=""> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM