开发者社区> 问答> 正文

谁能为我详细解释一下这段浮动的js代码?

function moveTips() {
        var tt = 50;
        if (window.innerHeight) {
            pos = window.pageYOffset
        }
        else if (document.documentElement && document.documentElement.scrollTop) {
            pos = document.documentElement.scrollTop
        }
        else if (document.body) {
            pos = document.body.scrollTop;
        }
        pos = pos - tips.offsetTop + theTop;
        pos = tips.offsetTop + pos / 10;
        if (pos < theTop) pos = theTop;
        if (pos != old) {
            tips.style.top = pos + "px";
            tt = 10;
        }
        old = pos;
        setTimeout(moveTips, tt);
    }

展开
收起
a123456678 2016-07-15 15:36:14 1474 0
1 条回答
写回答
取消 提交回答
  • function moveTips() {
            //设置自动执行的时间
            var tt = 50;
            //根据不同的浏览器,获取滚动的高度
            if (window.innerHeight) {
                pos = window.pageYOffset
            }
            else if (document.documentElement && document.documentElement.scrollTop) {
                pos = document.documentElement.scrollTop
            }
            else if (document.body) {
                pos = document.body.scrollTop;
            }
            //这里应该是一种算法,算出tips这个元素样式的top值
            pos = pos - tips.offsetTop + theTop;
            pos = tips.offsetTop + pos / 10;
            if (pos < theTop) pos = theTop;
            //如果pos值和原来不相同的话,将tips元素样式的top属性设置为pos,并将执行时间缩短为10毫秒
            if (pos != old) {
                tips.style.top = pos + "px";
                tt = 10;
            }
            //将pos赋给old,并等待tt毫秒后,再次执行这个函数
            old = pos;
            setTimeout(moveTips, tt);
        }
    2019-07-17 19:57:06
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载