uniapp 自定义滑动指示组件
作者:互联网
实现效果
<view>
<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="0"
:scroll-with-animation="true">
<view id="demo1" class="scroll-view-item_H uni-bg-red">A</view>
<view id="demo2" class="scroll-view-item_H uni-bg-green">B</view>
<view id="demo3" class="scroll-view-item_H uni-bg-blue">C</view>
</scroll-view>
</view>
<view class="uni-common-pb">
<image :src="currentIndex == 0?selectDot:normalDot"></image>
<image :src="currentIndex == 1?selectDot:normalDot"></image>
<image :src="currentIndex == 2?selectDot:normalDot"></image>
</view>
normalDot: '../../static/cc-dot-o.png',
selectDot: '../../static/cc-dot.png',
currentIndex: 0
scroll: function(e) {
// console.log(e.target.scrollLeft)
this.old.scrollTop = e.detail.scrollTop
// 判断滑动的位置 具体情况决定范围
if(e.target.scrollLeft < 140) {
this.currentIndex = 0;
}
if(e.target.scrollLeft >= 140 && e.target.scrollLeft <= 440) {
this.currentIndex = 1;
}
if(e.target.scrollLeft > 440) {
this.currentIndex = 2;
}
console.log(this.currentIndex)
},
.scroll-view_H {
white-space: nowrap;
width: 100%;
}
.scroll-view-item_H {
display: inline-block;
width: 600rpx;
height: 350rpx;
line-height: 350rpx;
text-align: center;
font-size: 36rpx;
margin-right: 50rpx;
border-radius: 20rpx;
}
.scroll-view-item_H:first-child {
margin-left: 50rpx;
}
.uni-bg-red {
background-color: red
}
.uni-bg-green {
background-color: green
}
.uni-bg-blue {
background-color: blue;
}
/* 指示器相关 */
.uni-common-pb {
width: 100%;
text-align: center;
margin-top: 50rpx;
}
.uni-common-pb image {
width: 80rpx;
height: 80rpx;
}
标签:uniapp,target,自定义,scrollLeft,width,..,currentIndex,组件,uni 来源: https://blog.csdn.net/weixin_45801434/article/details/120280442