如何在Vue 3中滚动到变量元素?

huangapple go评论66阅读模式
英文:

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 = &#39;&#39;
function showScrollInto(currentLocation, toLocation) {
source = currentLocation  // where to return to after section is hidden
document.getElementById(toLocation).style.display = &#39;block&#39;
document.getElementById(toLocation).scrollIntoView()
}

and to return to the original location:

document.getElementById(source).scrollIntoView()

showScrollInto is called when clicking a button:

&lt;button onClick=&quot;showScrollInto(&#39;top&#39;, &#39;interesse&#39;)&quot;&gt;TITLE&lt;/button&gt;

Now I turned that function into a method and tried

import { ref } from &quot;vue&quot;
var source = &quot;&quot;
const toLocation = ref(&#39;Vue.js&#39;)
export default {
name: &quot;App&quot;,
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: &#39;smooth&#39;})
// document.getElementById(toLocation).style.display = &#39;block&#39;
// document.getElementById(toLocation).scrollIntoView()
}
}
}

where showScrollInto is called by clicking a button like this:

&lt;button @click=&quot;showScrollInto($event, &#39;kosten&#39;, &#39;interesse&#39;)&quot;&gt;TITLE&lt;/button&gt; 

Elements to scroll into are like this:

&lt;section class = &quot;top-level-section&quot; v-show=&quot;!hideInteresse&quot; ref=&quot;interesse&quot;&gt;

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 版本和/或语法。

&lt;div ref=&quot;someRefName&quot;&gt;&lt;/div&gt;

Vue 2 / Options API

变量应该保存一个与元素上 ref 匹配的字符串

const refVar = &quot;someRefName&quot;
this.$refs[refVar].scrollIntoView({ behavior: &quot;smooth&quot; });

Vue 3 / Composition API

变量应该被赋值为 ref()(需要导入)。常量的名称应该与元素上 ref 的名称匹配

const someElement = ref() // 分配给模板中的某个元素
someElement.value.scrollIntoView({ behavior: &quot;smooth&quot; });

不应混合选项 API 和组合 API,只能选择一种语法。

在这两种情况下,您可以拥有多个具有相同 ref 名称的元素,此时 Vue 将创建一个具有相同名称 ref 的数组,因此要访问特定元素,您还需要使用索引。

下面是一些简化的示例。希望这些能解决您剩下的问题,并可根据需要进行修改。

Options API 代码沙盒示例

Composition API 代码沙盒示例

英文:

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.

&lt;div ref=&quot;someRefName&quot;&gt;&lt;/div&gt;

Vue 2 / Options API

The variable should hold a string matching the ref on the element

const refVar = &quot;someRefName&quot;
this.$refs[refVar].scrollIntoView({ behavior: &quot;smooth&quot; });

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: &quot;smooth&quot; });

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.

Options API codesandbox example

Composition API codesandbox example

huangapple
  • 本文由 发表于 2023年5月25日 19:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定