最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to apply different delays between each slide in bxslider - Stack Overflow

programmeradmin6浏览0评论

Is there a way to keep different delay time between each slide in a bxslider? Suppose I have 4 slides in the bxslider I want a delay of 1000ms between 1st and 2nd slide and 2000ms delay between third and 4th slide.

Is this possible?

Is there a way to keep different delay time between each slide in a bxslider? Suppose I have 4 slides in the bxslider I want a delay of 1000ms between 1st and 2nd slide and 2000ms delay between third and 4th slide.

Is this possible?

Share Improve this question edited Jun 8, 2013 at 12:30 Syed Hashim 1561 silver badge10 bronze badges asked Jun 8, 2013 at 12:13 ThejdeepThejdeep 2492 gold badges6 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

It's probably possible, but not pretty. Basically, insofar as I can tell from the documentatin for bxslider, you can't set times for specific, individual slide transition intervals. So, here's what you'd prob. need to do, in JavaScript, working with the event handlers:

Assuming the CSS selector .bxslider gets at the HTML element housing your slider content, and the slide transition you want to make slower (2000ms) is the third to fourth slide only... and assuming you've already set up and initialized your slider, your solution could be as follows (elipses to indicate the other options that you'll set up/you've already set up, that I'm glossing over):

var slider = $('.bxslider').bxSlider({
    // ...
    // other params here
    // ...
    speed: 1000,
    onSlideAfter: function (slide, oldIndex, newIndex) {
        if (newIndex === 2) { // remember, it's zero based indices with bxslider...
            slider.stopAuto();
            setTimeout(function () {
                slider.goToSlide(3);
                slider.startAuto();
            }, 2000);
        }
    }
});

Basically, we're stopping the autoplay when we hit the 3rd slide, so that we can manually control the slide "presence" duration. When the 2000 ms are up, we manually move the slider to the next frame, and restart the autoplay. If you had a larger slide show, and wanted to do this in a more organized way, it would/could look something like this:

var params = {
    defaultSpeed: 1000,
    speedOverrides: {
        "2": 2000
        // you can add other slides here,
        // with the slide number used being the
        // first slide of the slide transition,
        // i.e., modifying the speed of 3-4, like
        // above means you'd enter "2" as the
        // property name, b/c again we're using
        // 0-based indices.
    }
};
var slider = $('.bxslider').bxSlider({
    // ...
    // other params here
    // ...
    speed: params.defaultSpeed,
    onSlideAfter: function (slide, oldIndex, newIndex) {
        if (params.speedOverrides[newIndex]) {
            slider.stopAuto();
            setTimeout(function () {
                slider.goToSlide(newIndex + 1);
                slider.startAuto();
            }, params.speedOverrides[newIndex]);
        }
    }
});

I came up with a very flexible way to do this by making a small edit to the bxslider.js file. Here is the detail of my answer here: https://stackoverflow./a/17408119/700111

Use SlideBefore. The code in the answer above will stop at the last slide.

onSlideBefore: function (slide, oldIndex, newIndex) {
if (params.speedOverrides[newIndex]) {
    slider.stopAuto();
    setTimeout(function () {
        slider.goToSlide(newIndex);
        slider.startAuto();
    }, params.speedOverrides[newIndex]);
}

}

发布评论

评论列表(0)

  1. 暂无评论