uni-app中标签元素绑定的id属性被追加随机数导致scroll-into-view失效,如id="edd00d78--today"使得scroll-into-view="today"无法定位

uni-app中标签元素绑定的id属性被追加随机数导致scroll-into-view失效,如id="edd00d78–today"使得scroll-into-view="today"无法定位

示例代码:

<scroll-view scroll-x="true" scroll-into-view="today">  
    <view class="list">  
        <view  
            :id="item.date === today ? 'today' : ''"  
            class="item"  
            :class="[item.date === curDate ? 'cur' : '', item.date === today ? 'today' : '']"  
            :key="idx"  
            v-for="(item, idx) in list" @click="switchDate(item.date)">
            <view class="week">{{item.weekName}}</view>  
            <view class="date">  
                <view class="num">{{item.date !== today ? item.day : '今'}}</view>  
            </view>  
        </view>  
    </view>  
</scroll-view>

操作步骤:

<scroll-view scroll-x="true" scroll-into-view="today">  
    <view class="list">  
        <view  
            :id="item.date === today ? 'today' : ''"  
            class="item"  
            :class="[item.date === curDate ? 'cur' : '', item.date === today ? 'today' : '']"  
            :key="idx"  
            v-for="(item, idx) in list" @click="switchDate(item.date)">
            <view class="week">{{item.weekName}}</view>  
            <view class="date">  
                <view class="num">{{item.date !== today ? item.day : '今'}}</view>  
            </view>  
        </view>  
    </view>  
</scroll-view>

预期结果:

id="today"

实际结果:

实际打包后 id="edd00d78--today"

bug描述:

标签元素 绑定的id 属性 会被追加一串随机数 id=“edd00d78–today” 导致scroll-into-view=“today” 失效

Image Image

信息类别 详细信息
产品分类 uniapp/小程序/微信
PC开发环境 Windows
操作系统版本 window10 64
HBuilderX类型 正式
HBuilderX版本 3.3.13
第三方工具版本 1.05
基础库版本 2.23.4
项目创建方式 HBuilderX

更多关于uni-app中标签元素绑定的id属性被追加随机数导致scroll-into-view失效,如id="edd00d78--today"使得scroll-into-view="today"无法定位的实战教程也可以访问 https://www.itying.com/category-93-b0.html

8 回复

同遇到这个问题,解决了吗?

更多关于uni-app中标签元素绑定的id属性被追加随机数导致scroll-into-view失效,如id="edd00d78--today"使得scroll-into-view="today"无法定位的实战教程也可以访问 https://www.itying.com/category-93-b0.html


还没,在线等官方救火队员。。。

今天也在找这个问题的解决方案, 我在另外一篇文章中看到, 这个问题是需要通过设置scroll-view的高度, 来解决的, 如: <scroll-view style="height: 200px" scroll-y :scroll-into-view="scrollViewId"></scorll-view> 以此评论,希望后面的有问题的, 可以参考这个方法解决

救命啊 我也遇到这个问题

这样的话scroll-view组件的scroll-into-view属性根本没有用啊

楼主解决了吗

2025-3-10 还没解决

在 uni-app 中,某些情况下(尤其是在小程序环境中),框架可能会为元素的 id 属性自动追加随机数,以确保 id 的唯一性。这会导致你在使用 scroll-into-view 时无法正确定位到目标元素,因为 scroll-into-view 的值与实际的 id 不匹配。

解决方案

1. 使用 ref 替代 id

在 uni-app 中,推荐使用 ref 来获取元素,而不是依赖 id。你可以通过 this.$refs 来访问元素,并手动调用滚动方法。

<template>
  <view>
    <scroll-view :scroll-into-view="scrollIntoViewId" scroll-y>
      <view ref="today">Today</view>
      <view ref="tomorrow">Tomorrow</view>
    </scroll-view>
    <button @click="scrollToToday">Scroll to Today</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      scrollIntoViewId: ''
    };
  },
  methods: {
    scrollToToday() {
      this.scrollIntoViewId = 'today';
      // 或者直接操作 DOM
      // this.$refs.today.scrollIntoView();
    }
  }
};
</script>

2. 动态生成 id

如果你仍然需要使用 id,可以动态生成 id,并在 scroll-into-view 中使用相同的值。

<template>
  <view>
    <scroll-view :scroll-into-view="scrollIntoViewId" scroll-y>
      <view :id="todayId">Today</view>
      <view :id="tomorrowId">Tomorrow</view>
    </scroll-view>
    <button @click="scrollToToday">Scroll to Today</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      todayId: 'today',
      tomorrowId: 'tomorrow',
      scrollIntoViewId: ''
    };
  },
  methods: {
    scrollToToday() {
      this.scrollIntoViewId = this.todayId;
    }
  }
};
</script>

3. 使用 uni.createSelectorQuery

如果你需要更精确地控制滚动行为,可以使用 uni.createSelectorQuery 来获取元素的位置,并手动设置滚动位置。

<template>
  <view>
    <scroll-view ref="scrollView" scroll-y>
      <view id="today">Today</view>
      <view id="tomorrow">Tomorrow</view>
    </scroll-view>
    <button @click="scrollToToday">Scroll to Today</button>
  </view>
</template>

<script>
export default {
  methods: {
    scrollToToday() {
      const query = uni.createSelectorQuery().in(this);
      query.select('#today').boundingClientRect(data => {
        if (data) {
          this.$refs.scrollView.scrollTop = data.top;
        }
      }).exec();
    }
  }
};
</script>
回到顶部