简体   繁体   English

自定义上下滑动按钮

[英]Custom previous & next buttons for slick slider

I am trying to customize my slick slider to match my design. 我正在尝试自定义光滑的滑块以匹配我的设计。 Now what I need for the prev and next button is the actual previous and next image of the slider. 现在,我需要上一个和下一个按钮是滑块的实际上一个和下一个图像。

And when you click the previous image you go 1 slide back and when you click the next image you go 1 slide forward, obviously. 而且,当您单击上一张图像时,您可以向前滑动1张,而当您单击下一张图像时,您可以向前滑动1张。

This is how my slider looks like in html 这是我的滑块在html中的样子

<div class="col-md-1 text-left">
    <p class="left darkerfontcolor">prev</p>
</div>
<div class="col-md-10 text-center nopadding white-text">
    <div class="slider">
        <div class="slider-image" id="theslider1" style=
        "background-image: url('img/bg/bg-1.jpg'); height: 600px; background-size: cover; 
         background-position: center center;">
        <h3>Street life of Antwerp</h3>
            <p class="largefontsize somemargin">Hello there!</p>
            <div class="slider-buttons">
                <a class="smallfontsize slider-button somemarginright"
                href="#">More info</a> <a class=
                "smallfontsize slider-button somemarginleft" href="#">Book
                this tour</a>
            </div>
        </div>
        <div class="slider-image" id="theslider2" style=
        "background-image: url('img/bg/bg-2.jpg'); height: 600px; background-size: cover;
         background-position: center center;">
        <h3>Hey there, I have no text</h3>
            <p class="largefontsize somemargin">Will you be my text?</p>
            <div class="slider-buttons">
                <a class="smallfontsize slider-button somemarginright"
                href="#">More info</a> <a class=
                "smallfontsize slider-button somemarginleft" href="#">Book
                this tour</a>
            </div>
        </div>
        <div class="slider-image" id="theslider3" style=
        "background-image: url('img/bg/bg-3.jpg'); height: 600px; background-size: cover; 
        background-position: center center;">
        <h3>Please...</h3>
            <p class="largefontsize somemargin">It'll be fun!</p>
            <div class="slider-buttons">
                <a class="smallfontsize slider-button somemarginright"
                href="#">More info</a> <a class=
                "smallfontsize slider-button somemarginleft" href="#">Book
                this tour</a>
            </div>
        </div>
    </div>
</div>
<div class="col-md-1 text-right">
    <p class="right darkerfontcolor">next</p>
</div>

So the goals is to get the previous and next image as prev and next button. 因此,目标是将上一张和下一张图片作为上一张和下一张按钮。 I have grabbed the index of the current slide and want to grab the images of the next and previous slide from my slider container to inject into the html this is how I've done it 我已经获取了当前幻灯片的索引,并希望从我的滑块容器中获取下一张和上一张幻灯片的图像,以注入到html中,这就是我的操作方式

jQuery('.slider').on('afterChange', function(event, slick){
            var activeSlide = jQuery('.slider').find('.slick-active');
            var index = activeSlide.data('slick-index');
            console.log(index);
});

The problem is I have no clue how I can grab the images and inject them accordingly into my html 问题是我不知道如何获取图像并将其相应地注入到我的html中

If I understand your question correctly, you want to show a thumbnail preview of the next and previous slides in your buttons, right? 如果我正确理解了您的问题,您想在按钮中显示下一张和上一张幻灯片的缩略图预览,对吗?

I'm not familiar with the slick slider library you're using, but looking at the code you provided, this is how you could do it: 我不熟悉您正在使用的滑动式滑块库,但是查看您提供的代码,这是您可以做到的:

1. Find active slide: You've already managed to find the (jQuery) element that points to the currently active slide 1.查找活动幻灯片:您已经设法找到指向当前活动幻灯片的(jQuery)元素

var activeSlide = jQuery('.slider').find('.slick-active');`

2. Find the next and previous slides: All your slides are in the same parent element. 2.查找下一张和上一张幻灯片:您所有的幻灯片都在同一父元素中。 jQuery (which you're already using) has a handy prev and next method. jQuery(您已经在使用)有一个方便的prevnext方法。

var prevSlide = activeSlide.prev('.slider-image');
var nextSlide = activeSlide.next('.slider-image');

If I'm not mistaken, your slider plugin makes sure the slides are reordered when you reach the end/start of your slideshow. 如果我没记错的话,您的滑块插件可确保您在幻灯片放映结束/开始时对幻灯片进行重新排序。

3. Find your next and previous buttons: 3.找到您的下一个和上一个按钮:

var prevButton = $('.text-left');
var nextButton = $('.text-right');

4. Copy the background image to your buttons: 4.将背景图像复制到您的按钮:

prevButton.css({
  'background-image': prevSlide.css('background-image')
});
nextButton.css({
  'background-image': nextSlide.css('background-image')
});

Note that you'll need some additional CSS to make the background image in the buttons look nice. 请注意,您将需要一些其他CSS来使按钮中的背景图像看起来不错。

It is hard to say if this works without a working example, that's my attempt: 很难说,如果没有一个有效的例子,这是否可行,那是我的尝试:

jQuery('.slider').on('afterChange', function(event, slick){
        var activeSlide = jQuery('.slider').find('.slick-active');
        var index = activeSlide.data('slick-index');
        console.log(index);

         //find previous slide and pick background image. output:  "none" or url("...urlhere..")
        var prev_row = jQuery('.slider').find("[slick-index='" + (index + 1) + "']").css('background-image');


                // If matched, retrieve url, otherwise ""
                prev_row = /^url\((['"]?)(.*)\1\)$/.exec(prev_row);
                prev_row = prev_row ? prev_row[2] : ""; 

           //same as above, but done from next slide
         var next_row = jQuery('.slider').find("[slick-index='" + (index + 1) + "']").css('background-image');

        // ^ Either "none" or url("...urlhere..")
                next_row = /^url\((['"]?)(.*)\1\)$/.exec(next_row);
                next_row = next_row ? next_row[2] : ""; // If matched, retrieve url, otherwise ""

                 //append a tag image with the correct url of the images retrieved above
                    $('.left').append('<img src=" '+ prev_row + '" />');
        $('.right').append('<img src=" '+ next_row + ' " />');
});

jsfiddle jsfiddle

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

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