代码改变世界

设置 scroll-into-view 无效

  法子  阅读(1570)  评论(1编辑  收藏  举报

1、校验scroll-into-view的赋值是否和view的id一致,因为id一般包含变量,很容易和预期的值不一样

2、数值改变才会触发滚动。比如scroll-into-view的值是'id-40-view',我们手动滚动到其他位置,然后想滚动回来,就再次给它赋'id-40-view',是不会触发滚动的,因为它的值没有改变。所以要及时清空scroll-into-view的值,再给它赋新值的时候才会触发滚动

复制代码
<template>
  <u-popup :show="show" :closeable="true" round="24rpx" mode="bottom"
    @close="handleClickClose">
    <view class="content-view">
      <scroll-view scroll-y style="height: 100%;" :scroll-into-view="scrollIntoView">
        <view class="scroll-view">
          <view :id="'id-' + index + '-view'" v-for="(item, index) in list" :key="index">
            {{ index + 1 }}
          </view>
        </view>
      </scroll-view>
    </view>
  </u-popup>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    currentNumber: {
      type: Number,
      default: 0,
    },
    list: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      show: false,
      scrollIntoView: '', // 值改变才会触发
    }
  },
  watch: {
    visible (val) {
      this.show = val
      if (val) {
        this.$nextTick(() => {
          // 确保赋值和 view 的 id 一致
          this.scrollIntoView = `id-${this.currentNumber}-view`
        })
      }
    }
  },
  methods: {
    handleClickClose () {
      this.show = false
      this.scrollIntoView = '' // 清空数值
      this.$emit('update:visible', this.show)
    },
  },
}
</script>

<style lang="scss" scoped>
.pop-catalogue {
  background-color: #F7F9FC;

  .content-view {
    height: 968rpx;

    .scroll-view {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
    }

    .item-view {
      width: 72rpx;
      height: 72rpx;
      border-radius: 12rpx;
      color: #9EA5BB;
      font-size: 26rpx;
    }
  }
}
</style>
复制代码

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示