【The Bug of Vue】Scroll等移动效果的 behavior: 'smooth' 在IOS端不生效?

518 阅读1分钟

在使用到scrollTo()等函数的时候,选择了smooth变换,在电脑端,电脑的手机模拟端,安卓端,都可以完美的展现,但是唯独ios端不行,所以记录一下。

第一步:安装插件

npm i smoothscroll-polyfill

第二步:在对应页面引入

import smoothscroll from 'smoothscroll-polyfill';

第三步:执行这个方法(在需要使用behavior:'smooth'的事件里面写入)

smoothscroll.polyfill();

代码示例

最后上一段在项目中真实用到的代码。


//示例
function setMove(index) {
  let itemWidth = document.querySelector(".SlidingTab-content-item").offsetWidth;
  let bottomLineWidth = document.querySelector(".tab-item").offsetWidth;
  document.querySelector(".active-line").style.left = ((bottomLineWidth+20) *(index)) + 'px';
  document.querySelector(".SlidingTab-content-box").style.left = -(itemWidth * (index)) + 'px';
  smoothscroll.polyfill(); //ios流畅滑动
  document.querySelector(".SlidingTab-header").scrollTo({top:0,left:(bottomLineWidth+20)*(index-1),behavior:'smooth'});
}