Vue 滚动条定位问题

作者:matrix 被围观: 9,498 次 发布时间:2018-06-28 分类:零零星星 | 3 条评论 »

这是一个创建于 2130 天前的主题,其中的信息可能已经有所发展或是发生改变。

使用jquery的时候可以直接animate方法完成滑动滚动条的动作,但是在vue项目中会很蛋疼。

$('.findcar_brand_ul').animate({scrollTop: liheight}, 300);

Vue中又没有相关的组件可以调用,载入JQuery麻烦,要是需要临时解决的话可以使用js的scrollTop
看了网上各路大神的文章才恍悟。要做到渐缓动画 只是需要setTimeOut执行step操作就好,频率快,人眼也就察觉不出。先取使用js操作获取dom的scrollTop取出需要让滚动条定位的目标位置,之后递增/减setTimeout操作。

//方法2。js操作 
                var jump = document.querySelector('#postion');
                var total = jump.offsetTop;//要定位的目标滚动条位置
                console.log('纵坐标', total)
                // var distance = document.documentElement.scrollTop || document.body.scrollTop

                 var distanceDom = document.querySelector('.findcar_brand_ul');//滚动条所在的容器
                var distance = distanceDom.scrollTop;//容器的滚动条照度


                // 平滑滚动,每1ms一跳,共20跳
                var time = 15;//毫秒
                var stepnum = 20;//共20跳
                //时长(time*stepnum) ms


                var step = total / stepnum //因为除法会出现余数情况 导致最终定位有偏差
                if (total > distance) {
                    // console.log('smoothDown')
                    // console.log('distance:'+distance)
                    // console.log('total:'+total)
                    smoothDown()
                } else {
                    // var newTotal = distance - total + 100
                    var newTotal = distance - total
                    step = newTotal / stepnum
                    // console.log('smoothUp')
                    smoothUp()
                }
                function smoothDown () {
                    if (distance < total) {
                        // console.log('distance < total')
                        distance += step
                        // Firefox
                        // document.documentElement.scrollTop = distance
                        // // Chrome
                        // document.body.scrollTop = distance

                        distanceDom.scrollTop = distance;
                        setTimeout(smoothDown, time)
                    } else {
                        // console.log('distance < total: else ')
                        // document.documentElement.scrollTop = total
                        // // document.body.scrollTop = total
                        // window.pageYOffset = total

                        // console.log('standard:'+standard)
                        distance = total;//将最后一小步的滚动操作的偏移量还原到目标位置。若不加此操作 会导致定位偏移不准的情况出现。因为step = newTotal / stepnum 的值会有除不尽的情况  !!important
                        // console.log('distance end:'+distance)
                        distanceDom.scrollTop = distance;
                    }
                }
                function smoothUp () {
                    if (distance > total) {
                        distance -= step

                        // // Firefox
                        // document.documentElement.scrollTop = distance
                        // // Chrome
                        // document.body.scrollTop = distance

                        distanceDom.scrollTop = distance;
                        setTimeout(smoothUp, time)
                    } else {
                        // document.documentElement.scrollTop = total
                        // document.body.scrollTop = total
                        // window.pageYOffset = total

                        distance = total;
                        distanceDom.scrollTop = distance;
                    }
                }

说明:
demo环境:Vue.version >> "2.2.6"
修改代码使用div中滚动条,取消浏览器窗口滚动条的获取。
代码简单修改了下最终的定位偏移问题。因为step = newTotal / stepnum
至少这能解决问题,还需后面完善。

代码来自@wisewrong

PEACE~

图片4005-Vue 滚动条定位问题

参考:
https://www.cnblogs.com/wisewrong/p/6495726.html

其他文章:
本文固定链接:https://www.hhtjim.com/vue-scroll-bar-positioning-problem.html
matrix
本文章由 matrix 于2018年06月28日发布在零零星星分类下,目前没有通告,你可以至底部留下评论。
转载请注明:Vue 滚动条定位问题-HHTjim'S 部落格
关键字:,

有3 条评论 »

  1. 七夜 七夜 2018-9-3 23:49:21 +0800#3

    😀 😀 😛 很不错~!!

  2. 彩博888 彩博888 2018-7-8 19:36:36 +0800#2

    👿 👿 拦着成长的不是幼稚,而是自以为成熟。

  3. 彩博888 彩博888 2018-7-8 16:56:32 +0800#1

    多一分为自己,少一分为别人,人生在世,何必为难自己

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!