当前位置:首页 > VUE

vue实现滚动中断

2026-01-11 22:42:59VUE

实现滚动中断的方法

在Vue中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法:

监听滚动事件并阻止默认行为

methods: {
  handleScroll(event) {
    if (shouldStopScroll) {
      event.preventDefault();
      event.stopPropagation();
    }
  }
},
mounted() {
  window.addEventListener('scroll', this.handleScroll, { passive: false });
},
beforeDestroy() {
  window.removeEventListener('scroll', this.handleScroll);
}

使用CSS固定位置

通过CSS的overflow属性可以快速实现滚动锁定:

.stop-scrolling {
  overflow: hidden;
}

在Vue中动态切换类:

vue实现滚动中断

this.$refs.container.classList.add('stop-scrolling');

使用Vue自定义指令

创建自定义指令来处理滚动中断:

Vue.directive('stop-scroll', {
  inserted(el, binding) {
    el.style.overflow = binding.value ? 'hidden' : 'auto';
  },
  update(el, binding) {
    el.style.overflow = binding.value ? 'hidden' : 'auto';
  }
});

使用方式:

vue实现滚动中断

<div v-stop-scroll="shouldStop"></div>

平滑滚动中断的实现

对于需要平滑停止滚动的场景,可以使用requestAnimationFrame

let scrollStopFrame;
methods: {
  smoothStopScroll() {
    const start = window.pageYOffset;
    const duration = 500;
    const startTime = performance.now();

    const animateScroll = (time) => {
      const elapsed = time - startTime;
      const progress = Math.min(elapsed / duration, 1);
      window.scrollTo(0, start * (1 - progress));

      if (progress < 1) {
        scrollStopFrame = requestAnimationFrame(animateScroll);
      }
    };

    scrollStopFrame = requestAnimationFrame(animateScroll);
  },
  cancelStop() {
    cancelAnimationFrame(scrollStopFrame);
  }
}

移动端触摸滚动处理

对于移动设备,需要同时处理touchmove事件:

mounted() {
  document.addEventListener('touchmove', this.preventTouch, { passive: false });
},
methods: {
  preventTouch(e) {
    if (this.shouldStopScroll) {
      e.preventDefault();
    }
  }
},
beforeDestroy() {
  document.removeEventListener('touchmove', this.preventTouch);
}

滚动位置保持

中断滚动后需要保持位置时:

data() {
  return {
    scrollPosition: 0,
    isScrollStopped: false
  };
},
methods: {
  lockScroll() {
    this.scrollPosition = window.pageYOffset;
    this.isScrollStopped = true;
    document.body.style.position = 'fixed';
    document.body.style.top = `-${this.scrollPosition}px`;
  },
  unlockScroll() {
    document.body.style.position = '';
    document.body.style.top = '';
    window.scrollTo(0, this.scrollPosition);
    this.isScrollStopped = false;
  }
}

以上方法可根据具体需求选择使用或组合使用。对于复杂场景,可能需要结合Vue的响应式数据和生命周期钩子来实现更精细的控制。

标签: vue
分享给朋友:

相关文章

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…