Vue.js 实现禁止 h5 页面在 ios 浏览器内置的下拉 bounce 效果

介绍

vue-disbounce是一款基于Vue.js的自定义组件,可以有效避免触发h5页面在ios浏览器内置的下拉bounce效果。

组件

<template>
    <div
        :style="{'background-color':backgroundColor }"
        class="vd-wrapper"
    >
        <div
            class="vd"
            ref="vd"
        >
            <slot></slot>
        </div>
    </div>
</template>

<script>
export default {
    name: "vd",

    props: {
        backgroundColor: {
            type: String,
            default: "#ffffff"
        }
    },

    data() {
        return {};
    },

    mounted() {
        this.vd = this.$refs["vd"];
        this.vd.addEventListener("touchstart", this.touchstartEvent);
        this.vd.addEventListener("touchmove", this.touchmoveEvent, {
            passive: false // 阻止默认事件时,设置passive为false,提高性能
        });
    },

    methods: {
        getPoint(e) {
            return {
                x: e.touches ? e.touches[0].pageX : e.clientX,
                y: e.touches ? e.touches[0].pageY : e.clientY
            };
        },

        touchstartEvent(e) {
            this.startPoint = this.getPoint(e);
        },

        touchmoveEvent(e) {
            if (!this.startPoint) return;

            const scrollTop = this.vd.scrollTop; // 距离页面顶部的距离
            const curPoint = this.getPoint(e);
            const moveY = curPoint.y - this.startPoint.y; // 纵向移动的距离

            // 下拉
            if (moveY > 0) {
                // 如果在顶部,阻止浏览器默认的滚动,避免触发bounce
                if (scrollTop <= 0) e.preventDefault();

                // 上拉
            } else {
                const scrollHeight = this.vd.scrollHeight; // 全文区域的高度
                const clientHeight = this.vd.clientHeight; // 可见区域的高度
                const scrollBottom = scrollHeight - clientHeight - scrollTop; // 距离页面底部的距离

                // 如果在底部,阻止浏览器默认的滚动,避免触发ios的bounce效果
                if (scrollBottom <= 0) e.preventDefault();
            }
        }
    }
};
</script>

<style scoped="scoped">
.vd-wrapper {
    height: 100vh;
}

.vd {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}
</style>

调用

为了方便调用,笔者已经上传到npm
npm安装依赖npm i vue-disbounce
安装完以后,在app.vue文件调用

<template>
    <VueDisbounce id="app">
        <!-- 如果有使用路由, -->
        <router-view />
    </VueDisbounce>
</template>

<script>
import VueDisbounce from "vue-disbounce";

export default {
    components: {
        VueDisbounce
    }
};
</script>

作者

SuperIron

相关链接

https://www.npmjs.com/package/vue-disbounce
https://github.com/SuperIron/vue-disbounce

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容