Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve simple and seamless scrolling effect with vue

Shulou Source: shulou.com Published: 2022-06-01 00:23:37 11月14日 Update

This article introduces the knowledge of "how to achieve simple and seamless scrolling effect in vue". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect.

Realization idea

How to copy a list in vue without losing bound events? simply use slot slots, using two slots, we have two lists.

Component complete code

Export default {name: "listScroll", created () {}, mounted () {/ / do not scroll if (this.boxHeight) when the box content height is less than the visible height

< this.ele0.clientHeight) { this.start(this.height); this.setEvet(); } else { this.isScroll = false; } }, props: { speed: { default: 1, type: Number, }, }, computed: { //第一个slot ele0() { return this.$refs.box.children[0]; }, //第二个slot ele1() { return this.$refs.box.children[1]; }, //盒子的可视高度 boxHeight() { return this.$refs.box.clientHeight; }, }, data() { return { height: 0, isScroll: true, }; }, methods: { //鼠标移入停止滚动 移出继续滚动 setEvet() { this.$refs.box.onmouseenter = () =>

{this.isScroll = false; / / this.height = 0;}; this.$refs.box.onmouseleave = () = > {this.isScroll = true; this.$nextTick (() = > {this.start (this.height);});}, / / scrolling method start (height) {this.ele0.style = `transform:translateY (- ${height} px) `; this.ele1.style = `height:$ {this.boxHeight} px;transform:translateY (- ${height} px); overflow: hidden;`; if (height > = this.ele0.clientHeight) {this.height = 0;} else {this.height + = this.speed;} if (! this.isScroll) return; window.requestAnimationFrame (() = > {this.start (this.height);}) },},}; .listScroll {overflow: hidden;}. Hover {overflow-y: auto;}. Hide {display: none;}

Use

{{item.xh}} {{item.label}} import ListScroll from "@ / components/listScroll"; export default {name: "scroll", components: {ListScroll}, data () {return {list: new Array (10) .fill (1) .map ((s, I) = > ({xh: I + 1, label: "hello wrold"}),};},} .box {height: 300px;} .list {padding: 010px; width: 300px; .item {display: flex; justify-content: space-between; padding: 5px 0; cursor: pointer; &: hover {background-color: # 95a5a6;} "how vue achieves simple seamless scrolling" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Effect content seamless two more knowledge height slot practical learned next event code dilemma reality idea situation article method case Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology MySQL Shulou Tech Info Apple