vue.js - 如何像底部滚动一样将滚动第二个滚动添加到顶部?
问题描述
我的 Vue 组件大于浏览器大小。所以浏览器添加到它的底部滚动条。我需要第二个在顶部。我没有要显示的代码,因为我不知道该怎么做。我用谷歌搜索并发现只有纯\jquery 解决方案,但我不知道如何将它们集成到 Vue 项目中。
纯JS话题https://stackoverflow.com/a/50776007/1432751
https://jsfiddle.net/bqsw8pt9/2/
new Vue({
el: '#app',
data: {
message: 'Very loooooooooooooooooooooooooooooooooooooooong teeeeeeeeeeeeeeeeeeeeeeeeeexttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt'
}
})
解决方案
为达到这个:
- 创建两个具有溢出属性的元素,两个元素的宽度应该相同,这样滚动行为才会一致。
- 将滚动处理程序添加到第一个元素,将第二个元素滚动到其当前位置,即当元素 1 滚动到 20 时,也将元素 2 滚动到 20。
- 向第二个元素添加一个滚动处理程序,它将第一个元素滚动到其当前位置。
- 为确保两个元素不会在同一实例中尝试相互更新,请保持滚动状态,当它们中的任何一个触发滚动处理程序时,该状态会更新。
请找到您的小提琴的更新版本:link to fiddle
new Vue({
el: '#app',
data: {
message: 'Very loooooooooooooooooooooooooooooooooooooooong teeeeeeeeeeeeeeeeeeeeeeeeeexttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt',
scrolling: false
},
methods: {
handleScroll1: function () {
if(this.scrolling) {
this.scrolling = false;
return;
}
this.scrolling = true;
console.log(this.scrolling, this.$refs["wrapper1"].scrollLeft);
this.$refs["wrapper2"].scrollLeft = this.$refs["wrapper1"].scrollLeft;
},
handleScroll2: function () {
if(this.scrolling) {
this.scrolling = false;
return;
}
this.scrolling = true;
console.log(this.scrolling, this.$refs["wrapper2"].scrollLeft);
this.$refs["wrapper1"].scrollLeft = this.$refs["wrapper2"].scrollLeft;
}
}
})
作为旁注,我想建议您学习 Vanilla JS,它可以让您更好地掌握 javascript,还可以让您轻松理解任何框架中的任何代码,因此您可以随时根据自己的需要进行调整. 这个答案基于StanleyH 和 HoldOffHunger的类似答案
推荐阅读
- javascript - javascript: css 没有解决,为什么?
- python - 如何找到离线最近的点?
- django - Django-Filters Lookup_expr type 'AND'
- java - Java int 无法更改,因为 lambda 表达式必须是 final 或有效 final
- c - 带有 UNIX 域数据报套接字的 sendto 上的“无效参数”
- python - 查找按位运算的一个操作数
- c++ - 如何使用 v4l2loopback 将图像渲染到 /dev/video0?
- android - 显示对话框后更改警告对话框的正按钮文本
- android - 画布 drawText 和表情符号 alpha 颜色
- python - Selenium 不会在远程服务器上无头启动