vue点击左侧,右边滚动到最上面

image.png
 <div class="ingredients-list">
          <div class="ingredient-item" v-for="(item, index) in $tools.metadata.foodBankType" :class="{'active': index === activeFoodIndredientIndex}" @click="selectFood(item, index)">
            <span class="name">{{ item.label }}</span>
          </div>
        </div>
        <div class="food-container" ref="rightFoodList">
          <div class="food-item-select" v-for="item in rightFoodList" :key="item.id">
            <div class="select-left">
              <div class="food-name">{{ item.name }}</div>
              <div class="food-energe">
                <span class="value">{{ item.kcal ? (item.kcal).toFixed(2) : '' }}</span>
                <span>千卡/100g</span>
              </div>
            </div>
            <!--            添加食物-->
            <img class="add-icon" @click.stop="showSelectFood(item)" src="https://cdn.xun-qi.cn/seer/base/upload/img/1671074807714.png"/>
          </div>
        </div>


js部分


selectFood(item, index) {
      this.activeFoodIndredientIndex = index
      this.query.type = item.value
      this.refresh()
    },`

 refresh() {
      this.setLoading(true)
      this.loadYingKangFoodBank({
        search: this.query.search,
        type: this.query.type,
        callback: (res) => {
          console.log(res)
          this.rightFoodList = res.data.content
          this.setLoading(false)
          this.$nextTick(()=>{
            let top = this.$refs.rightFoodList.scrollTop
            // 定时改变高度,实现滚动效果
            const timeTop = setInterval(() => {
              this.$refs.rightFoodList.scrollTop = top -= 30;
              if (top <= 0) {
                clearInterval(timeTop);
              }
            }, 10);
          })
        }
      })
    },
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容