js 滚动到指定位置的函数

很多时候我们会需要这样一个效果,点击某位置时,让页面滚动到某一个位置。下面我总结的一个方法。
ele:要跳到的元素节点。不传参为跳到最上边
speed:滚动到元素节点的时间,单位为毫秒,不传参为默认值

function scrollTo(ele, speed){
    //速度默认值
    if(!speed) speed = 300;
    if(!ele){
        $("html,body").animate({scrollTop:0},speed);
    }else{
        if(ele.length>0) $("html,body").animate({scrollTop:$(ele).offset().top},speed);
    }
    return false;
}
原文地址:https://www.cnblogs.com/zhaoqiming/p/8086658.html