EN | 中文
The parallax are a common way for users to interact, implemented using Vue hooks. Support both vue3
and vue2
,You need to install plug-ins vue-composition-api
in vue2
。
inspired byGithub挂了,它错误页的Parallax效果研究一下?
yarn add vue-use-parallax
npm install vue-use-parallax --save
yarn add vue-composition-api vue-use-parallax-v2
<template>
<div class="container">
<div class="banner" ref="parallaxRef">
<div class="bg" ref="bgRef"></bg>
<div class="title" ref="titleRef">Title</div>
<div class="slogan" ref="sloganRef">Slogan</div>
<div class="post" ref="postRef"><img src="//img.domain.com/post.jpg" /></div>
</div>
</div>
</template>
<script>
// Vue3
import { ref } from 'vue'
import useParallax from 'vue-use-parallax'
export default {
setup () {
// bg
const parallaxRef = ref(null)
const bgRef = ref(null)
const titleRef = ref(null)
const sloganRef = ref(null)
const postRef = ref(null)
const { setParallax } = useParallax(parallaxRef, [bgRef, [titleRef, 20]])
setParallax([
[sloganRef, 30],
[postRef, -20]
])
return {
parallaxRef,
bgRef,
titleRef,
sloganRef,
postRef
}
}
}
</script>
<template>
<div class="container">
<div class="banner" ref="parallaxRef">
<div class="bg" ref="bgRef"></bg>
<div class="title" ref="titleRef">Title</div>
<div class="slogan" ref="sloganRef">Slogan</div>
<div class="post" ref="postRef"><img src="//img.domain.com/post.jpg" /></div>
</div>
</div>
</template>
<script>
import { onMounted, onUnmounted, ref } from '@vue/composition-api'
import useParallax from 'vue-use-parallax2'
export default {
setup () {
const wrapperRef = ref(null)
const bgRef = ref(null)
const titleRef = ref(null)
const heroRef = ref(null)
const buttonRef = ref(null)
// onMounted
onMounted(() => {
const { setParallax, resetParallax } = useParallax(wrapperRef.value, [bgRef.value, [titleRef.value, 20]])
setParallax([
[heroRef.value, 50],
[buttonRef.value, -20]
])
// destoryed
onUnmounted(() => {
resetParallax()
})
})
return {
wrapperRef,
bgRef,
titleRef,
heroRef,
buttonRef
}
}
}
</script>
useParallax(elRef: Ref<HTMLElement>, options: Array<[Ref<HTMLElement>, number] | Ref<HTMLElement>>)
elRef
Wrap the ref objectoptions
move child elements and offset values
const { startParallax, setParallax, resetParallax } = useParallax(wrapperRef.value)
startParallax()
Register to start listening for mouse movement visual biases. Default initialization calls. If manually 'resetParallax()' can be manually start monitoring.setParallax(options)
Add new move childrenresetParallax()
stop parallax event
Receive an array,Each item in the array can be a ref object
in the DOM, with a default offset of 10px,eg. setParallax([bannerRef])
If you want to configure the offset value of the DOM separately, you pass in an array where the first item is the REF object of the DOM, the second is the value of the offset value, and the reverse is the negative value. eg. setParallax([bannerRef, [titleRef, 100], [descriptionRef, -50]])
MIT License © 2020-present tomieric