vue2移动端滑动移动指

vue2移动端滑动移动指令+按钮滑动时候页面被跟着滚动的尴尬问题

当触摸开始的时候 给body加上overflow = 'hidden'属性阻止按摩会随着页面滚动

export default {
  name: '',
  data () {
    return {}
  },
  created () {
  },
  mounted () {
  },
  methods: {},
  directives: {
        // DIV点击移动
        drag: {
            inserted: function (el) {
                el.ontouchstart = function (e) {
                    var disx = e.touches[0].pageX - el.offsetLeft;
                    var disy = e.touches[0].pageY - el.offsetTop;
                    // 当触摸开始的时候 给body加上overflow = 'hidden'属性阻止按摩会随着页面滚动
                    document.body.style.overflow = 'hidden'
                    document.ontouchmove = function (e) {
                        el.style.left = e.touches[0].pageX - disx + 'px';
                        el.style.top = e.touches[0].pageY - disy + 'px';
                    }
                    document.ontouchend = function () {
                       //  触摸结束 再设置为自动
                        document.body.style.overflow = 'auto'
                        document.ontouchmove = document.ontouchend = null;
                    }
                }
            },

        }
    }
}



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

推荐阅读更多精彩内容