简体   繁体   English

jQuery:如何实现滑块(轮播)控制器?

[英]jQuery: how to implement controller for slider (carousel)?

I have build a slider using jQuery which works fine. 我已经使用jQuery构建了一个效果很好的滑块。 It was a quick development so that didn't get time to add controller. 这是一个快速的开发过程,因此没有时间添加控制器。 Right now it is getting hard to fix controller for the carousel. 目前,为轮播固定控制器变得越来越困难。

在此处输入图片说明

Does any one have solution or alternative to fix this? 有没有人有解决方案或替代方案来解决此问题?

Demo http://jsfiddle.net/sweetmaanu/Pn2UB/16/ 演示http://jsfiddle.net/sweetmaanu/Pn2UB/16/

$.fx.speeds._default = 1000;
function slider(container) {
    var currPg = null,
        firstPg = null;
    container.find('> .pg').each(function (idx, pg) {
        pg = $(pg);
        var o = {
            testimonial: pg.find('> .testimonial'),
            thumb: pg.find('> .testimonial-thumb'),
            pg: pg
        };
        o.pg.css({
            position: 'absolute',
            width: '100%',
            height: '100%',
        });

        if (idx > 0) {
            o.pg.css({
                opacity: 0,
                    'z-index': -1
            });
            o.testimonial.css({
                'margin-left': '100%'
            });
            o.thumb.css({
                'bottom': '-100%'
            });
        } else {
            firstPg = o;
        }
        o.prev = currPg;
        if (currPg) {
            currPg.next = o;
        }
        currPg = o;
    });
    firstPg.prev = currPg;
    currPg.next = firstPg;
    currPg = firstPg;

    this.advance = function advance(duration) {
        console.log("advance!", this);
        var dur = duration || $.fx.speeds._default;
        var dur2 = Math.ceil(dur / 2);

        var dh = container.height();
        var dw = container.width();
        var nextPg = currPg.next;

        nextPg.pg.css({
            opacity: 1,
                'z-index': null
        });

        var _pg = currPg;
        currPg.testimonial.stop().animate({
            'margin-left': -dw
        }, dur, function () {
            _pg.pg.css({
                opacity: 0,
                    'z-index': -1
            });
            _pg = null;
        });
        nextPg.testimonial.stop()
            .css({
            'margin-left': dw
        })
            .animate({
            'margin-left': 0
        }, dur);
        currPg.thumb.stop().animate({
            'bottom': -dh
        }, dur2, function () {
            nextPg.thumb.stop()
                .css({
                'bottom': -dh
            })
                .animate({
                'bottom': 0
            }, dur2);
            nextPg = null;
        });
        currPg = nextPg;
    }
}
var s = new slider($('#banner'));

function scheduleNext() {
    setTimeout(function () {
        s.advance();
        scheduleNext();
    }, 5000);
}
scheduleNext();

For next slider you need: 对于下一个滑块,您需要:

$('#next').click(function(){
s.advance();
});

But anyway you have to construct universal animations methods with parameters. 但是无论如何,您都必须构造带有参数的通用动画方法。 Check this examples: http://jsfiddle.net/lalatino/pjTU2/ and http://sorgalla.com/projects/jcarousel/examples/static_controls.html 检查以下示例: http : //jsfiddle.net/lalatino/pjTU2/http://sorgalla.com/projects/jcarousel/examples/static_controls.html

You just want to add a variable direction and change that on click of both prev and next 您只想添加一个可变的方向并在单击上一个和下一个时都进行更改

var direction = 'left';



$('#next').click(function(event) {
    direction = 'right';
    s.advance(1000,direction);
});
$('#prev').click(function(event) {
    direction = 'left';
    s.advance(1000,direction);
});

then add a line where it checks the direction variable 然后在其中检查方向变量的位置添加一行

    if(direction == 'left')
        var dw = container.width();
    else if(direction =='right')
        var dw = - container.width();
    else{
        console.log('Wrong direction')
        return;
    }

Carosal fixed 骨固定

Don't forget to add argument on advanced function 不要忘记在高级功能上添加参数

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

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