英文:
How to scroll into variable element with Vue 3?
问题
Here's the translated code:
var source = ''
function showScrollInto(currentLocation, toLocation) {
source = currentLocation // where to return to after section is hidden
document.getElementById(toLocation).style.display = 'block'
document.getElementById(toLocation).scrollIntoView()
}
// To return to the original location:
document.getElementById(source).scrollIntoView()
// showScrollInto is called when clicking a button:
<button onClick="showScrollInto('top', 'interesse')">TITLE</button>
// Now I turned that function into a method and tried
import { ref } from "vue"
var source = ""
const toLocation = ref('Vue.js')
export default {
name: "App",
data() {
return {
hideAlleClubs: true,
hideIkWilKennismaken: true,
hideAlleLocaties: true,
hideMeerOverKegelen: true,
hideInteresse: true
}
},
methods: {
showScrollInto(event, currentLocation, toLocation) {
source = currentLocation // where to return to after section is hidden
this.hideInteresse = false
this.$refs[toLocation].scrollIntoView({ behavior: 'smooth' })
// document.getElementById(toLocation).style.display = 'block'
// document.getElementById(toLocation).scrollIntoView()
}
}
}
// where showScrollInto is called by clicking a button like this:
<button @click="showScrollInto($event, 'kosten', 'interesse')">TITLE</button>
// Elements to scroll into are like this:
<section class="top-level-section" v-show="!hideInteresse" ref="interesse">
I've translated your JavaScript code into Vue 3. Please note that I've changed the this.$refs.toLocation
to this.$refs[toLocation]
to dynamically access the ref based on the toLocation
variable. This allows you to scroll to an element with a variable location name.
英文:
I have following function in pure JavaScript to scroll into an element and I would like to convert that code to Vue 3.
var source = ''
function showScrollInto(currentLocation, toLocation) {
source = currentLocation // where to return to after section is hidden
document.getElementById(toLocation).style.display = 'block'
document.getElementById(toLocation).scrollIntoView()
}
and to return to the original location:
document.getElementById(source).scrollIntoView()
showScrollInto is called when clicking a button:
<button onClick="showScrollInto('top', 'interesse')">TITLE</button>
Now I turned that function into a method and tried
import { ref } from "vue"
var source = ""
const toLocation = ref('Vue.js')
export default {
name: "App",
data() {
return {
hideAlleClubs: true,
hideIkWilKennismaken: true,
hideAlleLocaties: true,
hideMeerOverKegelen: true,
hideInteresse: true
}
},
methods: {
showScrollInto(event, currentLocation, toLocation) {
source = currentLocation // where to return to after section is hidden
this.hideInteresse = false
this.$refs.toLocation.scrollIntoView({ behavior: 'smooth'})
// document.getElementById(toLocation).style.display = 'block'
// document.getElementById(toLocation).scrollIntoView()
}
}
}
where showScrollInto is called by clicking a button like this:
<button @click="showScrollInto($event, 'kosten', 'interesse')">TITLE</button>
Elements to scroll into are like this:
<section class = "top-level-section" v-show="!hideInteresse" ref="interesse">
Passing the variables into the method works, but I can't figure out how to scroll to a location where the location is a variable.
this.$refs.interesse.scrollIntoView({ behavior: 'smooth'}) works for going to element with id 'interesse', but I don't know how to turn that element-name into a variable.
Furthermore I understand that this$refs is not Vue 3 notation and should be turned into something like ref('value') but I don't know how to do that.
答案1
得分: 1
首先,阅读 Vue 文档 上关于模板引用的部分。页面左上角有一个切换按钮,可以在选项 API 和组合 API 语法之间切换。
引用变量的 refs 取决于您使用的 Vue 版本和/或语法。
<div ref="someRefName"></div>
Vue 2 / Options API
变量应该保存一个与元素上 ref 匹配的字符串
const refVar = "someRefName"
this.$refs[refVar].scrollIntoView({ behavior: "smooth" });
Vue 3 / Composition API
变量应该被赋值为 ref()
(需要导入)。常量的名称应该与元素上 ref 的名称匹配
const someElement = ref() // 分配给模板中的某个元素
someElement.value.scrollIntoView({ behavior: "smooth" });
不应混合选项 API 和组合 API,只能选择一种语法。
在这两种情况下,您可以拥有多个具有相同 ref 名称的元素,此时 Vue 将创建一个具有相同名称 ref 的数组,因此要访问特定元素,您还需要使用索引。
下面是一些简化的示例。希望这些能解决您剩下的问题,并可根据需要进行修改。
英文:
First, read through the Vue documentation on template refs. There is a toggle in the top-left of the page where you can switch between Options API and Composition API syntax.
Referencing refs with variables depends on your version of Vue and/or syntax.
<div ref="someRefName"></div>
Vue 2 / Options API
The variable should hold a string matching the ref on the element
const refVar = "someRefName"
this.$refs[refVar].scrollIntoView({ behavior: "smooth" });
Vue 3 / Composition API
The variable should be assigned ref()
(import required). The name of the const should match the name of the ref on the element
const someElement = ref() // assigned to some element in the template
someElement.value.scrollIntoView({ behavior: "smooth" });
Options API and Composition API should not be mixed, so only stick with one syntax.
In both cases you can have multiple elements with the same ref name, in which case Vue will create an array of all same name refs, so to access a specific one you would also use an index
Below are some simplified examples. Hopefully they will clear up any remaining questions and you can modify to fit your needs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论