(共566篇)
全部分类

h5平滑滚动兼容安卓&ios
[ 未分类 ] 

普通vue项目,使用scrollTo方法进行滚动到指定位置

1
2
3
4
this.$refs.page.scrollTo({
   top: dom.offsetTop  // 需要滚动的距离
   behavior: 'smooth', // 平滑滚动到顶部
});

但是在ios环境中过渡动画会失效

解决方法:

1
2
3
yarn add smoothscroll-polyfill
import smoothscroll from 'smoothscroll-polyfill';
smoothscroll.polyfill();

uniapp 滚动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

// 该方法可以同时兼容安卓 ios
uni.pageScrollTo({
  duration: 500,
  scrollTop: 父元素.top - 子元素.top,
});

// 具体实现
query.select(".vip").boundingClientRect((res) => {
    uni.pageScrollTo({
        duration: 500,
        scrollTop: data.top - res.top,
    });
}).exec();