【问题标题】:Active class on page load of menu item菜单项页面加载上的活动类
【发布时间】:2010-08-09 16:03:06
【问题描述】:

我有一个硬编码的 li 菜单,它使用 filterable.js 根据哈希标签过滤项目列表。

如果项目被选中,我可以专注于工作,但是,我希望在页面加载时选择所有标签。我将“current”类添加到“all”菜单项,但这不起作用。

见:http://thinquetanque.com/portfolio

filterable.js 的代码:

/*
* Copyright (C) 2009 Joel Sutherland.
* Liscenced under the MIT liscense
*/

(function($) {
    $.fn.filterable = function(settings) {
        settings = $.extend({
            useHash: true,
            animationSpeed: 5000,
            show: { width: 'show', opacity: 'show' },
            hide: { width: 'hide', opacity: 'hide' },
            useTags: true,
            tagSelector: '#portfolio-filter a',
            selectedTagClass: 'current',
            allTag: 'all'
        }, settings);

        return $(this).each(function(){

            /* FILTER: select a tag and filter */
            $(this).bind("filter", function( e, tagToShow ){
                if(settings.useTags){
                    $(settings.tagSelector).removeClass(settings.selectedTagClass);
                    $(settings.tagSelector + '[href=' + tagToShow + ']').addClass(settings.selectedTagClass);
                }
                $(this).trigger("filterportfolio", [ tagToShow.substr(1) ]);
            });

            /* FILTERPORTFOLIO: pass in a class to show, all others will be hidden */
            $(this).bind("filterportfolio", function( e, classToShow ){
                if(classToShow == settings.allTag){
                    $(this).trigger("show");
                }else{
                    $(this).trigger("show", [ '.' + classToShow ] );
                    $(this).trigger("hide", [ ':not(.' + classToShow + ')' ] );
                }
                if(settings.useHash){
                    location.hash = '#' + classToShow;
                }
            });

            /* SHOW: show a single class*/
            $(this).bind("show", function( e, selectorToShow ){
                $(this).children(selectorToShow).fadeIn('slow');
            });

            /* SHOW: hide a single class*/
            $(this).bind("hide", function( e, selectorToHide ){
                $(this).children(selectorToHide).fadeOut('slow');
            });

            /* ============ Check URL Hash ====================*/
            if(settings.useHash){
                if(location.hash != '')
                    $(this).trigger("filter", [ location.hash ]);
                else
                    $(this).trigger("filter", [ '#' + settings.allTag ]);
            }

            /* ============ Setup Tags ====================*/
            if(settings.useTags){
                $(settings.tagSelector).click(function(){
                    $('#portfolio-list').trigger("filter", [ $(this).attr('href') ]);

                    $(settings.tagSelector).removeClass('current');
                    $(this).addClass('current');
                });
            }
        });
    }
})(jQuery);


$(document).ready(function(){

    $('#portfolio-list').filterable();

});

这是在我的投资组合项目列表中添加和删除“当前”类。我确定一定有jquery函数来加载一个类,但是我没有找到。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: jquery class menu filter focus


    【解决方案1】:

    你有这个:

    $(document).ready(function(){
    
        $('#portfolio-list').filterable();
    
    });
    

    试试这个:

    $(document).ready(function(){
    
        $('#portfolio-list').filterable();
        $('#portfolio-list').trigger('filter', [ '#all' ]);
    
    });
    

    或者也许(未经测试)...

    $(document).ready(function(){
    
        $('#portfolio-list').filterable();
        $("#portfolio-filter li:first a").addClass("current");
    
    });
    

    这将在页面加载时将当前类添加到第一个列表元素“All”。不太确定这是否有帮助。

    【讨论】:

    • 我最终用 css 解决了这个问题。我的列表都很不稳定,它干扰了正确的 css。但感谢您的意见。
    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多