JavaScript 跳转到指定元素位置的简单示例

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
JS代码如下:

/**
* 跳转到指定元素位置
* @param 
* @arrange (编程之家) jb51.cc
* 使用方法
* scroller(el,duration)
* el : 目标锚点 ID
* duration : 持续时间,以毫秒为单位,越小越快
*/
var flutil = {
intval: function(v) {
    v = parseInt(v);
    return isNaN(v) ? 0 : v;
},getPos: function(e) { // 获取元素信息 
    var l = 0;
    var t = 0;
    var w = this.intval(e.style.width);
    var h = this.intval(e.style.height);
    var wb = e.offsetWidth;
    var hb = e.offsetHeight;
    while (e.offsetParent) {
        l += e.offsetLeft + (e.currentStyle ? this.intval(e.currentStyle.borderLeftWidth) : 0);
        t += e.offsetTop + (e.currentStyle ? this.intval(e.currentStyle.borderTopWidth) : 0);
        e = e.offsetParent;
    }
    l += e.offsetLeft + (e.currentStyle ? this.intval(e.currentStyle.borderLeftWidth) : 0);
    t += e.offsetTop + (e.currentStyle ? this.intval(e.currentStyle.borderTopWidth) : 0);
    return {
        x: l,        y: t,        w: w,        h: h,        wb: wb,        hb: hb
    };
},getScroll: function() { // 获取滚动条信息 
    var t,l,w,h;
    if (document.documentElement && document.documentElement.scrollTop) {
        t = document.documentElement.scrollTop;
        l = document.documentElement.scrollLeft;
        w = document.documentElement.scrollWidth;
        h = document.documentElement.scrollHeight;
    } else if (document.body) {
        t = document.body.scrollTop;
        l = document.body.scrollLeft;
        w = document.body.scrollWidth;
        h = document.body.scrollHeight;
    }
    return {
        t: t,        l: l,        h: h
    };
},scroller: function(el,duration) { // 锚点(Anchor)间平滑跳转 
    if (typeof el != 'object') {
        el = document.getElementById(el);
    }
    if (!el) return;
    var z = this;
    z.el = el;
    z.p = this.getPos(el);
    z.s = this.getScroll();
    z.clear = function() {
        window.clearInterval(z.timer);
        z.timer = null
    };
    z.t = (new Date).getTime();
    z.step = function() {
        var t = (new Date).getTime();
        var p = (t - z.t) / duration;
        if (t >= duration + z.t) {
            z.clear();
            window.setTimeout(function() {
                z.scroll(z.p.y,z.p.x)
            },13);
        } else {
            st = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.y - z.s.t) + z.s.t;
            sl = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.x - z.s.l) + z.s.l;
            z.scroll(st,sl);
        }
    };
    z.scroll = function(t,l) {
        window.scrollTo(l,t)
    };
    z.timer = window.setInterval(function() {
        z.step();
    },13);
}
}

// 来自:编程之家 jb51.cc(jb51.cc)

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小