Jquery 更新滚动正在滚动中翻转

Jquery 更新滚动正在滚动中翻转,jquery,Jquery,我想知道滚动时如何更新URL中的哈希 我使用以下代码来更新单页设计中的当前状态 function setActiveListElements(){ // Get the offset of the window from the top of page var windowPos = $(window).scrollTop(); $('#primary-navwrapper').find('li a[href^="#"]').each(function () {

我想知道滚动时如何更新URL中的哈希

我使用以下代码来更新单页设计中的当前状态

function setActiveListElements(){

    // Get the offset of the window from the top of page
    var windowPos = $(window).scrollTop();

    $('#primary-navwrapper').find('li a[href^="#"]').each(function () { 
        var anchorId = $(this);
        var target = $(anchorId.attr("href"));
        //console.log(target);

        var offsetTop = target.position().top - offset;
        //var offsetBottom = offsetTop + target.height();
        //var top = sidebar.offset().top;

        if (target.length > 0) {
            //console.log(target.position().top + target.height());

            //if(windowPos >= offsetTop && windowPos <= offsetBottom) {
            if (target.position().top - offset <= windowPos && target.position().top + target.height() + offset > windowPos) {
                $('#primary-navwrapper li a').removeClass("current");
                anchorId.addClass("current");
            }

        }

    });

}
函数setActiveListElements(){
//获取窗口从页面顶部的偏移量
var windowPos=$(window.scrollTop();
$(“#主navwrapper”).find('LIA[href^=“#”]”)。每个(函数(){
var anchorId=$(本);
var target=$(anchorId.attr(“href”);
//控制台日志(目标);
var offsetTop=target.position().top-offset;
//var offsetBottom=offsetTop+target.height();
//var top=侧边栏.offset().top;
如果(target.length>0){
//console.log(target.position().top+target.height());
//如果(windowPos>=偏移量和windowPos 0){
//console.log(target.position().top+target.height());
//如果(windowPos>=offsetTop&&windowPos使用历史API:

history.pushState(null, null, '#myhash');
要处理那些不支持这一点的人:

if (history.pushState) {
    history.pushState(null, null, '#myhash');
} else {
    location.hash = '#myhash';
}

window.location.hash='myvalue';
?如何替换滚动部分的id的
#myhash
?当然,这是另一个问题:-)但是一旦你滚动,你需要做一个方法来检测你在哪里,然后从那里你将更新你的散列…用jQuery检查你在滚动中的位置。是的,但是我的代码已经检测到了。它会将我菜单的当前状态设置到视口的正确位置…所以我认为这只是添加了一点点code到我的代码。。。
if (history.pushState) {
    history.pushState(null, null, '#myhash');
} else {
    location.hash = '#myhash';
}