没有关于我想要实现的功能的文档,我无法在网上找到它,所以我在这里尝试。
当用户单击过滤器时,我想添加一个散列到url。我使用的是MixItUp jQuery插件。更可取的是,它需要使用localstorage记住url哈希值,这样如果用户关闭或刷新页面,用户上次单击的过滤器仍然有效。
同位素有我想要实现的东西,但我不能让它在MixItUp工作,虽然它似乎是一个类似的技术?也许这里有人能让它工作。
我知道如何添加哈希到url时,用户点击过滤器,但刷新后的活动状态重置为'all'。
过滤器:
<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 for add hash:
$(document).ready(function() {
$("#menu a").click(function(e) {
window.location.hash = $(this).attr("id");
e.preventDefault();
});
});
提前感谢。
我知道这是一个有点延迟的答复,但我现在才遇到这个问题。
为了基于散列初始化过滤器,我需要更多的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();
但是我需要你的过滤区号来给你这方面的建议。
希望这对你有帮助。:)