2

我正在使用同位素,这是 jsfiddle:http: //jsfiddle.net/desandro/DA3wF/

或者,或者,我设置了一个演示:http: //nerdi.net/isotope-test.html

有一个选项filter: selector

我怎么能通过一个过滤器,例如:index.html#green在页面加载时,会过滤到.green类?这可能吗?

4

2 回答 2

3
filter: (window.location.hash != '') ? '.' + window.location.hash.replace('#', '') : 'default'

这是一个if/then检查当前哈希是否为空字符串的语句。如果是filter则将设置为default否则将设置为该window.location.hash值(减去#)。

像这样的 URLindex.htmlfilter被设置为default,像这样的 URLindex.html#my-selectorfilter被设置为.my-selector

更新

您的代码不起作用的原因实际上与我的代码没有任何关系,但这是我在您的网站上测试的代码的更新版本:

$filterLinks.click(function(){
    var $this = $(this);
    
    // don't proceed if already selected
    if ( $this.hasClass('selected') ) {
        return;
    }
    
    $filterLinks.filter('.selected').removeClass('selected');
    $this.addClass('selected');
    
    // get selector from data-filter attribute
    var selector = $this.data('option-value');
    
    $container'.isotope({
        filter: selector
    });
});
于 2012-01-30T20:37:34.427 回答
1
filter: window.location.hash.replace('#', '.')

location.hash 将返回“#green”,我们只需将其替换为一个点即可获得“.green”。

jsfiddle示例如何根据哈希更改所选链接。

于 2012-01-30T20:48:50.873 回答