0

没有关于我想要实现的功能的文档,我无法在网上找到它,所以我在这里尝试。

当用户单击过滤器时,我想向 url 添加一个哈希。我正在使用MixItUp jQuery 插件。最好它需要记住带有 localstorage 的 url 哈希,因此如果用户关闭或刷新页面,用户最后单击的过滤器仍然处于活动状态。

Isotope有我想要实现的东西,但我无法让它在 MixItUp 中工作,尽管它似乎是一种类似的技术?也许这里有人已经能够让它工作。

我知道当用户单击过滤器时如何向 url 添加哈希,但刷新后活动状态重置为“全部”。

过滤器的 HTML:

<section id="menu">
   <a id="one" class="filter menu-button active" data-filter="all">One</a>
   <a id="two" class="filter menu-button" data-filter=".selected:not(.button-exclude)">Two</a>
   <a id="three" class="filter menu-button" data-filter=":not(.selected)">Three</a>
 </section>`

用于添加哈希的 JS:

$(document).ready(function() {
   $("#menu a").click(function(e) {
     window.location.hash = $(this).attr("id");
     e.preventDefault();
   });
});

先感谢您。

4

1 回答 1

1

我知道这有点延迟回复,但我现在才遇到这个问题。

为了基于散列初始化过滤器,我需要更多的 HTML 代码用于过滤区域。同时,我在这里复制我的解决方案,它使用 data-filter 属性工作。在我的例子中,我使用类将数据过滤器链接到可过滤元素。

HTML 如下所示:

<div class="filtering">
  <span><a data-filter="all"><span>Show all</span></a></span>
  <ul>
    <li><a data-filter="._training-videos">Training Videos</a></li>
    <li><a data-filter="._how-to-guides">How-To Guides</a></li>
    <li style="display: none;"><a data-filter="all">Show all</a></li>
  </ul>
</div>


<section class="filter">    
  <article class="video _training-videos">
    <a class="various fancybox.iframe" href="https://www.youtube.com/embed/CY1wYKq5sLU?autoplay=1&amp;rel=0">
        <img src="/images/videos/CY1wYKq5sLU.jpg" />
    </a>
    <h4>What you need to know about online marketing</h4>
  </article>

  <article class="video _how-to-guides">
    <a class="various fancybox.iframe" href="https://www.youtube.com/embed/L2QfjcgDjsY?autoplay=1&amp;rel=0">
        <img src="/images/videos/L2QfjcgDjsY.jpg" />
    </a>
    <h4>Handling, Cleaning, Playing your Vinyl</h4>
  </article>
</section>

顶部的块(div.filtering)是过滤器下拉列表(类别),而底部的块(section.filter)是链接到 YouTube 视频的图像区域。

为了预加载过滤器,而不是像初始化 MixItUp 代码

$('.filter').mixItUp();

您需要使用 { load: { filter: data_filter } } 设置。这显然是从 URL 获得散列值后。

这是整个代码片段:

//  MixItUp init script
$(function () {
    //  Get the hash value from the URL
    var hashedValue = window.location.hash;
    if (hashedValue.replace('#', '') == '') {
        hashedValue = 'all';            //  Use the default filter if there's no hash value
    } else {
        hashedValue = '._' + hashedValue.replace('#', '');      //  Try to figure out the filter class based on the hash value
    }

    //  Make sure the filter dropdown selects the hash item
    $('.filtering').find('[data-filter="' + hashedValue + '"]').click();

    //  Init MixItUp
    $('.filter').mixItUp({
        load: {
            filter: hashedValue
        }
    });
});

请注意,您需要分别设置下拉菜单 (.filtering) 和可过滤区域 (.filter)。

根据您的代码,我认为您需要进行的更改:

hashedValue = hashedValue.replace('#', '');

$('#menu').find('#' + hashedValue).click();

但我需要你的过滤区号来给你这方面的建议。

不过,希望这会有所帮助。:)

于 2016-11-17T09:13:03.927 回答