如何快速获取当前链接?后面的内容,location.search、页面滚动

function request()
{
    var urlStr = location.search;
    if (urlStr.indexOf("?") == -1) {
        theRequest={};
        return;
    }
    urlStr = urlStr.substring(1);
    var strs = urlStr.split("&");
    for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[0]);
        theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
    }
}

  window.scrollTo(0,0); 

如1设置的话,浏览器会在瞬间改变滚动条的位置,没有滚动的效果。如果要滚动的话,我们就需要慢慢的改变scrollTop的值就好。那么如何来做呢?

依照上面的经验,我们可能这么写:

var T=$(window).scrollTop();
var t=setInterval(function()
{
    if(T==0)
    {
        clearInterval(t);
    }
    else
    {
        T-=10;
        $(window).scrollTop(T);
    }
},10);

这么写,完全没有错误,把设置为固定值,就可以定点滚动了。这个例子里写的是匀速滚动,当然如果为了效果好,你可以修改下T值的变动情况,使之成为变减速运动。

有没有更好的办法来简化这个过程呢?我们知道,jQuery的animation是非常著名的,可以把这个运动放在animate里来执行呢?答案是肯定的:

 $(document.body).animate({'scrollTop':0},1000); 

通过阅读animate的API知道,animate的其中两个参数为属性和时间(如上代码),其中属性是这么描述的“An object of CSS properties and values that the animation will move toward.”(参考:http://api.jquery.com/animate/),意思指的是对象的css属性和值。但我们从第一点就知道,scrollTop并不是一个css属性,这又作如何解释呢?

可以解释的是,animate的第一个参数包括的不仅仅是CSS,它涵盖了所有可以用数值来表示的window属性值(scrollTop、scrollLeft)以及dom的CSS属性值(width、top、margin-left)。

原文地址:https://www.cnblogs.com/pengchengzhong/p/6027590.html