0

默认情况下,MixItUp 会将过滤点击处理程序应用于任何具有“过滤器”类的元素,如下所示:

<ul>
     <li class="filter" data-filter="dogs"> </ li>
     <li class="filter" data-filter="cats"> </ li>
     <li class="filter" data-filter="krakens"> </ li>
     <li class="filter" data-filter="dogs cats"> </ li>
</ul>

或者,过滤的元素可以直接通过 javascript 使用 'filter' 方法。

Syntax: $ ('# Grid') mixitup ('filter', 'dogs').

但我希望我的控件使用标签:

`<select id="cd-dropdown" name="cd-dropdown" class="cd-select">
<option value=""> SELECT CATEGORY </ option>
<option value="mix"> ALL </ option>
<option value="cat0"> CAT 0 </ option>
<option value="cat1"> CAT 1 </ option>
<option value="cat2"> CAT 2 </ option>
<option value="cat3"> CAT 3 </ option>
</ select>`

因为我正在尝试实现在 codrop 上找到的这个 jquery 下拉列表:http: //tympanus.net/codrops/2012/11/29/simple-effects-for-drop-down-lists/

我将把这个插件的脚本放在查询中:

 ;( function( $, window, undefined ) {

    'use strict';

    $.DropDown = function( options, element ) {
        this.$el = $( element );
        this._init( options );
    };

    // the options
    $.DropDown.defaults = {
        speed : 300,
        easing : 'ease',
        gutter : 0,
        // initial stack effect
        stack : true,
        // delay between each option animation
        delay : 0,
        // random angle and positions for the options
        random : false,
        // rotated [right||left||false] : the options will be rotated to thr right side or left side.
        // make sure to tune the transform origin in the stylesheet
        rotated : false,
        // effect to slide in the options. value is the margin to start with
        slidingIn : false,
        onOptionSelect : function(opt) { return false; }
    };

    $.DropDown.prototype = {

        _init : function( options ) {

            // options
            this.options = $.extend( true, {}, $.DropDown.defaults, options );
            this._layout();
            this._initEvents();

        },
        _layout : function() {

            var self = this;
            this.minZIndex = 1000;
            var value = this._transformSelect();
            this.opts = this.listopts.children( 'li' );
            this.optsCount = this.opts.length;
            this.size = { width : this.dd.width(), height : this.dd.height() };

            var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ),
                inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime();

            this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel );

            this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount );
            this._positionOpts();
            if( Modernizr.csstransitions ) {
                setTimeout( function() { self.opts.css( 'transition', 'all ' + self.options.speed + 'ms ' + self.options.easing ); }, 25 );
            }

        },
        _transformSelect : function() {

            var optshtml = '', selectlabel = '', value = 1;
            this.$el.children( 'option' ).each( function() {

                var $this = $( this ),
                    val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) ,
                    classes = $this.attr( 'class' ),
                    selected = $this.attr( 'selected' ),
                    label = $this.text();

                if( val !== 1 ) {
                    optshtml += 
                        classes !== undefined ? 
                            '<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
                            '<li data-value="' + val + '"><span>' + label + '</span></li>';
                }

                if( selected ) {
                    selectlabel = label;
                    value = val;
                }

            } );

            this.listopts = $( '<ul/>' ).append( optshtml );
            this.selectlabel = $( '<span/>' ).append( selectlabel );
            this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el );
            this.$el.remove();

            return value;

        },
        _positionOpts : function( anim ) {

            var self = this;

            this.listopts.css( 'height', 'auto' );
            this.opts
                .each( function( i ) {
                    $( this ).css( {
                        zIndex : self.minZIndex + self.optsCount - 1 - i,
                        top : self.options.slidingIn ? ( i + 1 ) * ( self.size.height + self.options.gutter ) : 0,
                        left : 0,
                        marginLeft : self.options.slidingIn ? i % 2 === 0 ? self.options.slidingIn : - self.options.slidingIn : 0,
                        opacity : self.options.slidingIn ? 0 : 1,
                        transform : 'none'
                    } );
                } );

            if( !this.options.slidingIn ) {
                this.opts
                    .eq( this.optsCount - 1 )
                    .css( { top : this.options.stack ? 9 : 0, left : this.options.stack ? 4 : 0, width : this.options.stack ? this.size.width - 8 : this.size.width, transform : 'none' } )
                    .end()
                    .eq( this.optsCount - 2 )
                    .css( { top : this.options.stack ? 6 : 0, left : this.options.stack ? 2 : 0, width : this.options.stack ? this.size.width - 4 : this.size.width, transform : 'none' } )
                    .end()
                    .eq( this.optsCount - 3 )
                    .css( { top : this.options.stack ? 3 : 0, left : 0, transform : 'none' } );
            }

        },
        _initEvents : function() {

            var self = this;

            this.selectlabel.on( 'mousedown.dropdown', function( event ) {
                self.opened ? self.close() : self.open();
                return false;

            } );

            this.opts.on( 'click.dropdown', function() {
                if( self.opened ) {
                    var opt = $( this );
                    self.options.onOptionSelect( opt );
                    self.inputEl.val( opt.data( 'value' ) );
                    self.selectlabel.html( opt.html() );
                    self.close();
                }
            } );

        },
        open : function() {
            var self = this;
            this.dd.toggleClass( 'cd-active' );
            this.listopts.css( 'height', ( this.optsCount + 1 ) * ( this.size.height + this.options.gutter ) );
            this.opts.each( function( i ) {

                $( this ).css( {
                    opacity : 1,
                    top : self.options.rotated ? self.size.height + self.options.gutter : ( i + 1 ) * ( self.size.height + self.options.gutter ),
                    left : self.options.random ? Math.floor( Math.random() * 11 - 5 ) : 0,
                    width : self.size.width,
                    marginLeft : 0,
                    transform : self.options.random ?
                        'rotate(' + Math.floor( Math.random() * 11 - 5 ) + 'deg)' :
                        self.options.rotated ?
                            self.options.rotated === 'right' ?
                                'rotate(-' + ( i * 5 ) + 'deg)' :
                                'rotate(' + ( i * 5 ) + 'deg)'
                            : 'none',
                    transitionDelay : self.options.delay && Modernizr.csstransitions ? self.options.slidingIn ? ( i * self.options.delay ) + 'ms' : ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : 0
                } );

            } );
            this.opened = true;

        },
        close : function() {

            var self = this;
            this.dd.toggleClass( 'cd-active' );
            if( this.options.delay && Modernizr.csstransitions ) {
                this.opts.each( function( i ) {
                    $( this ).css( { 'transition-delay' : self.options.slidingIn ? ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : ( i * self.options.delay ) + 'ms' } );
                } );
            }
            this._positionOpts( true );
            this.opened = false;

        }

    }

    $.fn.dropdown = function( options ) {
        var instance = $.data( this, 'dropdown' );
        if ( typeof options === 'string' ) {
            var args = Array.prototype.slice.call( arguments, 1 );
            this.each(function() {
                instance[ options ].apply( instance, args );
            });
        }
        else {
            this.each(function() {
                instance ? instance._init() : instance = $.data( this, 'dropdown', new $.DropDown( options, this ) );
            });
        }
        return instance;
    };

    } )( jQuery, window );

我测试了技巧,但我没有得到任何结论!

在这里,我发现有人实现了 fancyselect 和 mixitup 脚本,但我不能对下拉菜单做同样的事情。我给你链接! https://github.com/hasinhayder/ResponsiveGalleryWithFiltering

4

2 回答 2

2

同样的问题在这里,我解决了这个问题。

1-您应该注意到您正在使用的下拉插件正在删除 select 和 option 标签并将它们替换为 ul-li 和隐藏的输入字段,其name属性与标签的orinput相同。nameidselect

所以如果你有: <select id="cd-dropdown" name="cd-dropdown" class="cd-select">

你得到这样的输入: <input type="hidden" name="cd-dropdown" value=""></input>

2-您想要的是mixitup在值更改时更改过滤器,input如下所示:

$("[name=cd-dropdown]").on("change",function(){

        item = $(this).val();
        //console.log(item);

        $('#pub-grid').mixitup('filter',item);
    });

3-以上行不通,因为输入字段是hidden并且没有侦听器,change也就是说,没有人在侦听隐藏字段以检测它是否已更改。因此,每当您更改输入字段的值时,您都应该触发该事件。

为此,请打开下拉插件并转到第 147 行并添加您的触发器:

代码原价:

this.opts.on( 'click.dropdown', function() {
    if( self.opened ) {
        var opt = $( this );
        self.options.onOptionSelect( opt );
        self.inputEl.val( opt.data( 'value' ) );    
        self.selectlabel.html( opt.html() );
        self.close();
    }
} );

添加触发器change

this.opts.on( 'click.dropdown', function() {
    if( self.opened ) {
        var opt = $( this );
        self.options.onOptionSelect( opt );
        self.inputEl.val( opt.data( 'value' ) ).trigger('change');
        self.selectlabel.html( opt.html() );
        self.close();
    }
} );

就是这样,您的最终代码将如下所示:

$('#Grid').mixitup();
$( '#cd-dropdown' ).dropdown( {
    gutter : 4
} );

$("[name=cd-dropdown]").on("change",function(){
    item = $(this).val();   
    $('#Grid').mixitup('filter',item);
});
于 2013-11-02T08:14:17.817 回答
0

自从提出这个问题以来已经有一段时间了,但是对于新访客:

您可以在不更改插件代码的情况下执行此操作。

codrops 下拉插件提供了传递选项值的选项,例如

onOptionSelect : function( opt ) {
   window.location = opt.data( 'value' );
}

在这里,您可以调用 mixitUp 插件并传递过滤器或排序数据,因此您的总代码如下所示:

$( '#cd-dropdown' ).dropdown( {
 gutter : 4,
 onOptionSelect : function( opt ) {
    jQuery('#Grid').mixItUp('filter', opt.data( 'value' ));
 }
});
于 2014-09-26T18:00:11.417 回答