javascript - 为什么不将 scrollIntoView 应用于 vuejs 中的选定元素?
问题描述
我目前正试图让它滚动到视图的一部分,如下所示:
mounted: function() {
const el = this.$el.getElementsByClassName('outstandingStandarItems')[0];
if(el){
el.scrollIntoView({behavior: 'smooth'});
console.log(el);
}
}
但这并不适用:el.scrollIntoView({behavior: 'smooth'});
我应该去的部分是这样的:
<el-card shadow="never" class="box-card outstandingStandarItems" v-if="isOutstanding">
</el-card>
我需要scrollIntoView
在加载视图时应用它,这就是我将它安装在其中的原因。如果我将它添加到一个方法中,然后添加一个按钮以转到该方法,那么它确实会正确应用它。
<el-button @click="gotoOutStanding()">GOT OUT</el-button>
gotoOutStanding(){
this.$refs.outstandingItems.$el.scrollIntoView({behavior: 'smooth'});
},
但是我需要在视图加载时自动发生这种情况。
解决方案
我认为问题出在:
const el = this.$el.getElementsByClassName('outstandingStandarItems')[0];
你应该尝试使用 $refs:
<el-card shadow="never" ref="items" class="box-card outstandingStandarItems" v-if="isOutstanding">
</el-card>
接着:
const el = this.$ref.item;
为了更深入的了解去这里
推荐阅读
- ios - CATransaction 完成被调用甚至视图控制器消失
- javascript - Javascript函数生成所有排列
- ios - 如何从 AppDelegate.swift 路由到任何视图控制器
- php - 写一个通用行
- python - 解决 Python RC4 密码代码中的索引错误
- node.js - moment().startOf('day')` 显示为`Moment<2020-05-01T00:00:00+02:00>`
- swift - 无法获得带有详细信息的 Stripe 付款 POST 响应 - Swift
- django - Django,如何在注释中修复浮点字段中的最大小数?
- amazon-web-services - 如何将以前的任务定义与 ECS 和 bitbucket 管道部署一起使用
- php - php中的3维数组