需求:
有时候,需求就是这么。。。。。。明明可以滑动显示,他们就说,用户不知道滑动,还需要手摸手的去教,好,这也是个不错的理由。
要求,点击swiper cell ,触发滑动展示,
官方文档说明,可以调用实例的open方法,传参,‘left’ 'right’
发现传参后,根本没用效果。
查看官方ISSUS后,得到了解决办法。
handleClick(event) {
event.stopPropagation();
this.$refs["cell"].open("right");
},
传入事件对象
,阻止事件冒泡,再配合open使用,就可以拉。
拓展: 很容易就可以实现点击打开,再点击,关闭的效果。
handleClick(event) {
event.stopPropagation();
if (this.$refs["cell"].offset) {
this.$refs["cell"].close();
return;
}
this.$refs["cell"].open("right");
},
继续拓展: swiperCell是在v-for中渲染的,这个时候,需要动态的设置ref, 通过点击的item来触发对于的swiperCell的open()方法。
<van-swipe-cell
class="van-hairline--bottom"
v-for="(item, index) in list"
:ref="`list${item.id}`"
:key="index"
>
<div @click="handleClick($event, item)">{{item.name}}</div>
</van-swipe-cell>
handleClick(event, item) {
// console.log(event, item);
event.stopPropagation();
if (this.$refs[`list${item.id}`][0].offset) {
this.$refs[`list${item.id}`][0].close();
return;
}
this.$refs[`list${item.id}`][0].open("right");
},
v-for, 动态设置refs,可以参考这篇文章:
https://blog.csdn.net/LzzMandy/article/details/91372406