Mixitup插件带下拉菜单

时间:2013-09-28 23:47:28

标签: javascript jquery drop-down-menu

默认情况下,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>

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

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

2 个答案:

答案 0 :(得分:2)

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

1-您应该注意到您使用的下拉插件正在删除select和option标记,并将其替换为ul-li和隐藏的输入,name的{​​{1}}属性为与input标记的nameid相同。

所以如果你有: select

你得到这样的输入: <select id="cd-dropdown" name="cd-dropdown" class="cd-select">

2-你想要的是每当<input type="hidden" name="cd-dropdown" value=""></input>值改变时改变mixitup过滤器:

input

3-以上不会起作用,因为输入字段是$("[name=cd-dropdown]").on("change",function(){ item = $(this).val(); //console.log(item); $('#pub-grid').mixitup('filter',item); }); 并且没有hidden的监听器,也就是说,没有人正在监听隐藏字段以检测它是否被更改。因此,只要更改输入字段的值,就应该触发事件。

要执行此操作,请打开下拉插件并转到第147行并添加触发器:

代码的oroginal价格:

change

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();
    }
} );

答案 1 :(得分:0)

自问这个问题以来已经有一段时间了,但对于新访问者来说:

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

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

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

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

$( '#cd-dropdown' ).dropdown( {
 gutter : 4,
 onOptionSelect : function( opt ) {
    jQuery('#Grid').mixItUp('filter', opt.data( 'value' ));
 }
});
相关问题