我可以为光滑滑块的每个自定义点使用不同的图像吗?

我有一个问题要问光滑的滑块退伍军人:


我想将我的点的自定义图像放在光滑的滑块上,但我想为我的滑块点使用不同的图像。所以例如我有 5 张幻灯片,我希望我的 5 个点有单独的图像,所以 5 个不同的图像(如果我使用不同的图像用于活动和非活动状态,则为 10 个)


任何人都可以建议我如何处理这个问题吗?我想在这里尝试一下,但后来我意识到它只适用于每个点的相同图像:


 $('.productslider').slick({

                lazyLoad: 'ondemand',

                dots: true,

                customPaging : function(slider, i) {

                    return '<a href="#"><img src="images/realmix/products/freshness_energy_active.png" /><img src="images/realmix/products/freshness_energy_active.png" /></a>';

                },

                speed: 300,

                slidesToShow: 1,

                slidesToScroll: 1,

                adaptiveHeight: true,

                infinite: true,

                responsive: [

                    {

                        breakpoint: 400,

                        settings: {

                            mobileFirst: true,

                            centerMode: true,

                            centerPadding: '10px',

                            slidesToShow: 1,

                            slidesToScroll: 1,

                            infinite: true,

                            adaptiveHeight: true,

                            dots: true


                        }

                    }

                ]

              });  


ibeautiful
浏览 125回答 1
1回答

慕桂英3389331

您可以将自定义图像 url 作为数据属性分配给幻灯片 div,然后在 slick init 上循环遍历您的点,获取自定义点图像 url 并通过幻灯片 id 将其渲染到 slick dot 中。请注意,在我的示例中,我.slick-slide使用此数据属性在 div 中设置了一个点图像...data-dot-img="https://i.imgur.com/6X5GKWJ.png"这是一个小提琴......&nbsp;https://jsfiddle.net/joshmoto/fxy7gudv/请参阅下面的工作示例...// my slick slider optionsconst options = {&nbsp; slidesToShow: 1,&nbsp; slidesToScroll: 1,&nbsp; dots: true,&nbsp; arrows: false,&nbsp; adaptiveHeight: true,&nbsp; autoplay: true};// my slick slider as const objectconst mySlider = $('.slider').on('init', function(slick) {&nbsp; // set this slider as const for use in set time out&nbsp; const slider = this;&nbsp; &nbsp;&nbsp;&nbsp; // slight delay so init completes render&nbsp; setTimeout(function() {&nbsp; &nbsp; // dot buttons&nbsp; &nbsp; let dots = $('.slick-dots > LI > BUTTON', slider);&nbsp; &nbsp; // each dot button function&nbsp; &nbsp; $.each(dots, function(i,e) {&nbsp; &nbsp; &nbsp; // slide id&nbsp; &nbsp; &nbsp; let slide_id = $(this).attr('aria-controls');&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // custom dot image&nbsp; &nbsp; &nbsp; let dot_img = $('#'+slide_id).data('dot-img');&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $(this).html('<img src="' + dot_img + '" alt="" />');&nbsp; &nbsp; });&nbsp; }, 100);}).slick(options);body {&nbsp; margin: 0;&nbsp; padding: 20px;}.slider .slick-slide > A {&nbsp; display: block;&nbsp; position: relative;&nbsp; height: 132px;&nbsp; width: 100%;}.slider .slick-slide > A > IMG {&nbsp; position: absolute;&nbsp; width: 100%;&nbsp; display: block;&nbsp; top: 50%;&nbsp; left: 50%;&nbsp; transform: translate(-50%,-50%);}.slider .slick-dots {&nbsp; bottom: -37.5px;}.slider .slick-dots LI {&nbsp; width: 75px;&nbsp; height: 75px;&nbsp; opacity: .8;}.slider .slick-dots LI.slick-active {&nbsp; opacity: 1;}.slider .slick-dots LI.slick-active BUTTON,.slider .slick-dots LI:hover BUTTON {&nbsp; opacity: 1;&nbsp; transform: scale(1,1);}.slider .slick-dots LI BUTTON {&nbsp; display: block;&nbsp; overflow: hidden;&nbsp; position: relative;&nbsp; width: 100%;&nbsp; height: auto;&nbsp; padding: 0;&nbsp; transition: all .5s ease;&nbsp; transform: scale(.75,.75);}.slider .slick-dots LI BUTTON IMG {&nbsp; display: block;&nbsp; height: 100%;&nbsp; position: absolute;&nbsp; top: 50%;&nbsp; left: 50%;&nbsp; transform: translate(-50%,-50%);}.slider .slick-dots LI BUTTON:before {&nbsp; display: block;&nbsp; position: relative;&nbsp; content: '';&nbsp; width: 100%;&nbsp; padding-top: 100%;&nbsp; height: auto;}<div class="slider">&nbsp; <div data-dot-img="https://i.imgur.com/6JplNl6.png">&nbsp; &nbsp; <a href="https://i.imgur.com/q5Y5RCH.png">&nbsp; &nbsp; &nbsp; <img src="https://i.imgur.com/q5Y5RCH.png" alt="" />&nbsp; &nbsp; </a>&nbsp; </div>&nbsp; <div data-dot-img="https://i.imgur.com/6X5GKWJ.png">&nbsp; &nbsp; <a href="https://i.imgur.com/8HjXPXD.png">&nbsp; &nbsp; &nbsp; <img src="https://i.imgur.com/8HjXPXD.png" alt="" />&nbsp; &nbsp; </a>&nbsp; </div>&nbsp; <div data-dot-img="https://i.imgur.com/SefTwI1.png">&nbsp; &nbsp; <a href="https://i.imgur.com/vUDcfcy.png">&nbsp; &nbsp; &nbsp; <img src="https://i.imgur.com/vUDcfcy.png" alt="" />&nbsp; &nbsp; </a>&nbsp; </div>&nbsp; <div data-dot-img="https://i.imgur.com/IqLrd0o.png">&nbsp; &nbsp; <a href="https://i.imgur.com/okTDHas.png">&nbsp; &nbsp; &nbsp; <img src="https://i.imgur.com/okTDHas.png" alt="" />&nbsp; &nbsp; </a>&nbsp; </div></div><link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick-theme.min.css" rel="stylesheet"/><link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.css" rel="stylesheet"/><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript