我在让 hashchange 函数按我想要的方式工作时遇到了一些麻烦。我目前有一个 div 的砌体网格,每行(4 个)之后都有一个清除 div,它是隐藏的。单击其中一个 div 会显示最近的清除 div,在其中附加相关内容并重新加载其周围的砌体。
这一切都很好,但是当我想链接到从其他页面显示的这些 div 时,我想将哈希附加到函数中。哈希值被添加到 URL,但如果您加载 URL,它将无法运行该函数并显示相关的 div。
这是一个 jsfiddle:http: //jsfiddle.net/EV7yg/1/
jQuery:
$(document).ready(function () {
$(window).hashchange( function(){
$(".content-block-footer").click(function () {
$('.content-block-preview').hide();
var previewForThis = $(this).parent(".content-block-small").nextAll('.content-preview:first');
var otherPreviews = $(this).parent(".content-block-small").siblings('.content-preview').not(previewForThis);
otherPreviews
.addClass('excluded') // exclude other previews from masonry layout
.hide();
previewForThis
.removeClass('excluded')
.append($('#content-preview' + $(this).attr('hook')).show())
.hide()
.delay(400)
.fadeIn("slow");
$('html, body').animate({ scrollTop: $(this).offset().top-95 }, 'slow');
$('#block-wrap').masonry('reload');
});
});
$(window).hashchange();
});
HTML:
<div id="block-wrap">
<div class="content-block-small" style="background-color: white;">
<div class="content-block-footer" hook="01"><a href="#test01">READ MORE</a>
</div>
</div>
<div class="content-block-small" style="background-color: white;">
<div class="content-block-footer" hook="02"><a href="#test02">READ MORE</a>
</div>
</div>
<div class="content-block-small" style="background-color: white;">
<div class="content-block-footer" hook="03"><a href="#test03">READ MORE</a>
</div>
</div>
<div class="content-block-small" style="background-color: white;">
<div class="content-block-footer" hook="04"><a href="#test04">READ MORE</a>
</div>
</div>
<div class="content-preview excluded">
</div>
<div id="content-preview01" class="content-block-preview">
CONTENT GOES HERE
</div>
<div id="content-preview02" class="content-block-preview">
CONTENT GOES HERE
</div>
<div id="content-preview03" class="content-block-preview">
CONTENT GOES HERE
</div>
<div id="content-preview04" class="content-block-preview">
CONTENT GOES HERE
</div>
</div>
是否可以将哈希附加到此类点击功能并从外部页面链接到这些哈希(显示相关内容)?