0

HTML:

<div class="promo_tumbs col_12">
    <div data-dir="prev" class="prev"></div>
    <div data-dir="next" class="next"></div>
    <div class="promo_tumbs_centar">
        <a href="#first"><div class="promo_tumb promo_tumb_current">Test</div></a>
        <a href="#second"><div class="promo_tumb">Test</div></a>
        <a href="#thrid"><div class="promo_tumb">Test</div></a>
        <a href="#fourh"><div class="promo_tumb">Test</div></a>
        <a href="#fifth"><div class="promo_tumb">Test</div></a>
        <a href="#fifth"><div class="promo_tumb">Test</div></a>
        <a href="#fifth"><div class="promo_tumb">Test</div></a>
    </div>
</div>

JavaScript:

function Slider(container, nav) {
    this.container = container;
    this.nav = nav;

    this.li = this.container.find('li');
    this.li_width = this.li.first().width();
    this.li_len = this.li.length;

    this.thumbs = this.nav.find('a');

    this.current = 0;
}

Slider.prototype.transition = function(coords) {
    this.container.stop().animate({
        'margin-left': coords || -(this.current * this.li_width)
    })
}

Slider.prototype.set_current = function(dir) {
    var pos = this.current;
    if (dir === 'next') {
        pos++
    }
    else if (dir === 'prev') {
        pos--
    }
    this.current = (pos < 0) ? this.li_len - 1 : pos % this.li_len;

    return pos;
}

var slider = new Slider($('div.promo_inner ul'), $('div.promo_tumbs'));
slider.nav.find('div').on('click', function() {
    if ($(this).attr("data-dir") === undefined) {
        var index = slider.thumbs.index($(this).parent('a'));

        console.log(index)
    } else {
        slider.set_current($(this).data('dir'));
    }
    slider.transition();
})​
​

小提琴链接

当我单击元素时,我得到两个值-单击元素的索引和-1。这里发生了什么?我怎样才能松开 -1 并只获得索引值?

4

3 回答 3

2

调用 event.stopPropagation(); 为了阻止事件Demo在JsFiddle上的传播

这将使您更多地了解导致双重事件的元素多重事件的原因 JsFiddle

于 2012-05-31T09:54:40.993 回答
1

nav.find()也匹配<div class="promo_tumbs_centar">。尝试find(".promo_tumb")

于 2012-05-31T09:42:09.573 回答
0

当点击 .promo_tumb div 时,您也点击了 .promo_tumbs_centar。

你应该使用这个:

slider.nav.find('.promo_tumb')

代替

slider.nav.find('div')
于 2012-05-31T09:44:12.537 回答