在一页上制作几个滑块/转盘

时间:2017-03-20 19:18:40

标签: javascript jquery html css slider

我正在使用“Datalife Engine”CMS和“Entreprise”模板。有一个滑块,可以像这样添加页面

<div id="slider-wrapper">
<div class="slider">
    <div class="slide" style="background-image: url(/uploads/fotos/lager.jpg);">
        <div class="cnt">
            <div class="slide-text">
                <h2>Duis felis tortor, consequat</h2>
                <p>Duis felis tortor, consequat In sed elementum risus. Phasellus venenatis hendrerit pharetra. Suspendisse ultricies malesuada metus, non placerat lectus pellentesque at. Vivamus ut molestie lorem, non bibendum neque. Nam sagittis luctus egestas. Donec libero turpis, faucibus vitae iaculis vel, mattis at tortor.</p>
            </div>
        </div>
    </div>
    <div class="slide" style="background-image: url(/uploads/fotos/day_lager.jpg);">
        <div class="cnt">
            <div class="slide-text right-text">
                <h2>Phasellus venenatis hendrerit pharetra</h2>
                <p>Duis felis tortor, consequat In sed elementum risus. Phasellus venenatis hendrerit pharetra. Suspendisse ultricies malesuada metus, non placerat lectus pellentesque at. Vivamus ut molestie lorem, non bibendum neque. Nam sagittis luctus egestas. Donec libero turpis, faucibus vitae iaculis vel, mattis at tortor.</p>
            </div>
        </div>
    </div>
</div>
<span class="slider-button slider-button-prev"><i class="fa fa-angle-left"></i></span>
<span class="slider-button slider-button-next"><i class="fa fa-angle-right"></i></span>

滑块运行此JS(我认为)

$(".slider").aSlider({
    prevBtn: '.slider-button-prev',
    nextBtn: '.slider-button-next',
    fadeSpeed: 500, // скорость затухания/появления слайда
    autoPlay: true, // автоперелистывание слайдов (true/false)
    autoPlayDelay: 5000, // время показа в слайдах в миллисекундах
    slideDelay: 500
});

这是一个滑动功能

/* jQuery aSlider v1.2 */
;
! function (e) {
    jQuery.fn.aSlider = function (a) {
        var a = e.extend({
                nextBtn: ".aSliderNext",
                prevBtn: ".aSliderPrev",
                fadeSpeed: 300,
                autoPlay: !1,
                autoPlayDelay: 3e3,
                slideDelay: 0
            }, a),
            d = function () {
                function d() {
                    e(a.nextBtn).click(), r = setTimeout(d, a.autoPlayDelay)
                }
                var t = e(this),
                    n = t.children(".slide:first-child"),
                    l = t.children(".slide:last-child"),
                    i = n.index(),
                    f = l.index(),
                    u = n;
                if (t.css("overflow", "hidden"), t.find(".slide").fadeOut(0), n.fadeIn(0), e(a.nextBtn).click(function (e) {
                        return e.preventDefault(), u.index() != f ? (u.fadeOut(a.fadeSpeed), u = u.next().delay(a.slideDelay).fadeIn(a.fadeSpeed)) : (u.fadeOut(a.fadeSpeed), n.delay(a.slideDelay).fadeIn(a.fadeSpeed), u = n), !1
                    }), e(a.prevBtn).click(function (e) {
                        return e.preventDefault(), u.index() != i ? (u.fadeOut(a.fadeSpeed), u = u.prev().delay(a.slideDelay).fadeIn(a.fadeSpeed)) : (u.fadeOut(a.fadeSpeed), l.delay(a.slideDelay).fadeIn(a.fadeSpeed), u = l), !1
                    }), a.autoPlay) {
                    var r = setTimeout(d, a.autoPlayDelay);
                    t.parent().hover(function () {
                        clearTimeout(r)
                    }, function () {
                        r = setTimeout(d, a.autoPlayDelay)
                    })
                }
            };
        return this.each(d)
    }
}(jQuery);

// SmoothScroll for websites v1.2.1
// Licensed under the terms of the MIT license.
// People involved
//  - Balazs Galambosi (maintainer)  
//  - Michael Herf     (Pulse Algorithm)
;

我的问题是 - 如何在一个页面上添加几个(4个确切的)滑块?因为,如果我只是克隆html代码,所有滑块都在同时调用该函数并且刷新循环。试图用其他变量克隆函数,但失败了。请帮助。

1 个答案:

答案 0 :(得分:1)

您需要在实例化时将每个实例放入一个数组中,然后您可以通过其数组位置独立引用每个实例。 即。

 var sliderArr = [];

 var _slider = $(".slider").aSlider({
     prevBtn: '.slider-button-prev',
     nextBtn: '.slider-button-next',
     fadeSpeed: 500, // скорость затухания/появления слайда
     autoPlay: true, // автоперелистывание слайдов (true/false)
     autoPlayDelay: 5000, // время показа в слайдах в миллисекундах
     slideDelay: 500
 });

sliderArr.push(_slider);

然后访问该实例的方法和道具:

sliderArr[0].yourMethod();

如果你有第二个实例,你可以用同样的方式访问它:

即。

 sliderArr[1].yourMethod();