0

我已经在我的 wordpress 主题中实现了一点,当我单击文件夹内的链接时,我试图弄清楚如何实现 hashchange。我尝试使用本教程来触发 hashchange。当我注释掉“加载永久链接到 .itemContent”时,hashchange 有效,但随后项目立即向下和向上滑动。当它没有被注释掉时,hashchange 不会触发。

另外,如果有人将http://www.pathtomydomain.com/#/item-name-1复制到地址栏(或添加书签),如何根据 url 打开文件夹?与 .trigger(click) 有什么关系?

HTML

<div id="container">    
<div class="item">
<a href="http://pathtomydomain.com/item-name-1">
<img src="an-image-1.jpg" />
</a>
</div>

<div class="item">
<a href="http://pathtomydomain.com/item-name-2">
<img src="an-image-2.jpg" />
</a>
</div>

<div class="item">
<a href="http://pathtomydomain.com/item-name-3">
<img src="an-image-3.jpg" />
</a>
</div>
</div>
<div class="itemContent"></div>

查询

$.ajaxSetup({cache:false});

$('.item').click(function(){
        if($('.itemContent').is(":visible")){
            $('.itemContent').slideUp("fast", function(){  
        });

    } else if ($('.itemContent').is(":hidden")) {
        $('.itemContent').empty();
        $('.itemContent').html('<img class="loading" src="ajax-loader.gif"/>');
        $('.itemContent').slideDown("fast", function(){
            var $dock = $('#navigation'),
                dockHeight = $dock.height();
            $("html, body").animate({
                            scrollTop: $('.itemContent').offset().top - ( $(window).height() - dockHeight - $(this).outerHeight(true) ) / 2 

                            });

        });

    /*
    Load permalink to .itemContent . 
    */
    var post_link = $(this).children("a").attr("href");
        $(".itemContent").load(post_link);
        return false;

      }

});

var $mainContent = $(".itemContent"),
    siteUrl = "http://" + top.location.host.toString(),
    url = ''; 
$(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function() {
    location.hash = this.pathname;
    return false;
}); 

$(window).bind('hashchange', function(){
    url = window.location.hash.substring(1); 
});
$(window).trigger('hashchange');
4

0 回答 0