Created
April 18, 2021 05:21
-
-
Save phanmn/a7aad0702e44f63e8c8235b71a999079 to your computer and use it in GitHub Desktop.
vue3-marquee-text-component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import { defineComponent, h, useCssModule } from 'vue'; | |
// https://github.com/EvodiaAut/vue-marquee-text-component/blob/master/src/components/MarqueeText.vue | |
export default defineComponent({ | |
name: 'MarqueeText', | |
props: { | |
duration: { | |
type: Number, | |
default: 15, | |
}, | |
repeat: { | |
type: Number, | |
default: 2, | |
validator: function (val) { | |
return val > 0; | |
}, | |
}, | |
paused: { | |
type: Boolean, | |
default: false, | |
}, | |
reverse: { | |
type: Boolean, | |
default: false, | |
}, | |
}, | |
setup(props, { slots, attrs }) { | |
const style = useCssModule(); | |
const text = h( | |
'div', | |
{ | |
class: style.text, | |
style: { | |
animationDuration: `${props.duration}s`, | |
animationDirection: props.reverse ? 'reverse' : undefined, | |
}, | |
}, | |
slots.default(), | |
); | |
return () => { | |
return h( | |
'div', | |
{ | |
...attrs, | |
class: `${style.wrap}`, | |
}, | |
[ | |
h( | |
'div', | |
{ | |
class: [props.paused ? style.paused : undefined, style.content], | |
}, | |
Array(props.repeat).fill(text), | |
), | |
], | |
); | |
}; | |
}, | |
}); | |
</script> | |
<style module> | |
.wrap { | |
overflow: hidden; | |
} | |
.content { | |
width: 100000px; | |
} | |
.text { | |
animation-name: animation; | |
animation-timing-function: linear; | |
animation-iteration-count: infinite; | |
float: left; | |
} | |
.paused .text { | |
animation-play-state: paused; | |
} | |
@keyframes animation { | |
0% { | |
transform: translateX(0); | |
} | |
100% { | |
transform: translateX(-100%); | |
} | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment