vue中禁止ios浏览器页面滚动的橡皮筋效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37068028/article/details/80183781
在iPhone浏览器上滚动页面时,页面出现了橡皮筋效果
layout.vue

<template>
    <div class="layout">
        <header></header>
        <router-view/>
        <footer></footer>
    </div>
</template>
...

方法二: fixed定位

检查问题,发现页面没高度,解决办法:
html,body统一设为100%,在父组件的根元素设置position:fixed可以使页面无橡皮筋效果

<style>
.layout{
    position: fixed;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>

不要将手机页面的高度设为100vh,手机浏览器上有地址栏,下有功能键,会占据高度。

方法二:阻止body的touchmove事件

单纯解决橡皮筋效果,可以将body的touchmove事件禁止,可以替代第一种方法

// App.vue

mounted () {
    document.body.addEventListener('touchmove', function (e) {
        e.preventDefault() // 阻止默认的处理方式(阻止下拉滑动的效果)
    }, {passive: false}) // passive 参数不能省略,用来兼容ios和android
}

猜你喜欢

转载自blog.csdn.net/m0_37068028/article/details/80183781