我知道这有点延迟回复,但我现在才遇到这个问题。
为了基于散列初始化过滤器,我需要更多的 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&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&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();
但我需要你的过滤区号来给你这方面的建议。
不过,希望这会有所帮助。:)