页面加载时jquery触发器锚点滚动?

时间:2010-12-20 04:06:41

标签: php jquery scroll anchor

我已经下载了jquery滚动脚本(http://www.position-absolute.com/articles/better-html-anchor-a-jquery-script-to-slide-the-scrollbar/),它将滚动到单击链接时的锚点。但是,我想知道是否有任何方法可以使用php / jquery从URL中获取项目ID,然后在页面加载时滚动到该项目ID?

这是滚动js:

$(document).ready(function() {
    $("a.anchorLink").anchorAnimate()
});

jQuery.fn.anchorAnimate = function(settings) {

    settings = jQuery.extend({
        speed : 1100
    }, settings);   

    return this.each(function(){
        var caller = this
        $(caller).click(function (event) {  
            event.preventDefault()
            var locationHref = window.location.href
            var elementClick = $(caller).attr("href")

            var destination = $(elementClick).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
                window.location.hash = elementClick
            });
            return false;
        })
    })
}

以下是包含itemid的示例网址:

http://www.somesite.com/index.php?view=list&itemid=3

^^所以基本上我需要抓取itemid(使用php我假设?)然后告诉jQuery滚动到那个锚点:

<a name="3" id="3"></a>

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:2)

根本不需要PHP!你很幸运!有一个jQuery插件可以抓取和解析查询字符串。 http://plugins.jquery.com/project/query-object。如果你想自己动手,只需在JavaScript中测试location.href属性,因为它包含查询字符串。 http://www.w3schools.com/jsref/prop_loc_href.asp