当前位置:   article > 正文

微信小程序使用scroll-view制作横向滑动滚动条_微信小程序横向滚动条

微信小程序横向滚动条

示例图

废话不多说,直接上代码

  1. <view style="display: flex;position: relative;">
  2. <scroll-view class="scroll-view_H" scroll-x="true" style="width: 100%" bindscroll="getleft">
  3. <view class="bottom">
  4. <view class="map_btn_bg">
  5. <view class="map_btn_font">1</view>
  6. </view>
  7. <view class="map_btn_bg">
  8. <view class="map_btn_font">2</view>
  9. </view>
  10. <view class="map_btn_bg">
  11. <view class="map_btn_font">3</view>
  12. </view>
  13. <view class="map_btn_bg">
  14. <view class="map_btn_font">4</view>
  15. </view>
  16. <view class="map_btn_bg">
  17. <view class="map_btn_font">5</view>
  18. </view>
  19. <view class="map_btn_bg">
  20. <view class="map_btn_font">6</view>
  21. </view>
  22. <view class="map_btn_bg">
  23. <view class="map_btn_font">7</view>
  24. </view>
  25. <view class="map_btn_bg">
  26. <view class="map_btn_font">8</view>
  27. </view>
  28. </view>
  29. </scroll-view>
  30. <!--滚动条部分-->
  31. <view class="slide">
  32. <view class='slide-bar'>
  33. <view class="slide-show" style="width:{{slideWidth}}rpx; margin-left:{{slideLeft<=1 ? 0 : slideLeft+'rpx'}};"></view>
  34. </view>
  35. </view>
  36. </view>
  1. .scroll-view_H{
  2. white-space: nowrap;
  3. height: 230rpx;
  4. }
  5. .bottom {
  6. display: flex;
  7. }
  8. .map_btn_bg {
  9. width: 160rpx;
  10. height: 180rpx;
  11. display: flex;
  12. align-items: center;
  13. justify-content: center;
  14. border-radius: 24rpx;
  15. flex-direction: column;
  16. flex-shrink: 0;
  17. background-color: #fff;
  18. margin-right: 20rpx;
  19. }
  20. .map_btn_font {
  21. font-size: 28rpx;
  22. font-family: PingFang SC, PingFang SC;
  23. font-weight: 400;
  24. color: #606060;
  25. }
  26. .slide{
  27. height: 20rpx;
  28. position: absolute;
  29. bottom: 0;
  30. left: 50%;
  31. z-index: 9999;
  32. margin-left: -50rpx;
  33. }
  34. .slide .slide-bar{
  35. width: 100rpx;
  36. margin:0 auto;
  37. height: 10rpx;
  38. background:#eee;
  39. border-radius: 8rpx;
  40. }
  41. .slide .slide-bar .slide-show{
  42. height:100%;
  43. border-radius: 8rpx;
  44. background-color: #00aeff;
  45. }

  1. // 滑动比例计算
  2. slideWidth: '', //滑块宽
  3. slideLeft: 0, //滑块位置
  4. totalLength: '', //当前滚动列表总长
  5. slideRatio: '', //滑块比例
  6. //加载页面时候执行getRatio方法进行计算
  7. getRatio() {
  8. var _totalLength = 1440; //总长度(单个块宽度*总数)
  9. var _ratio = 100 / _totalLength * (750 / this.data.windowWidth); //滚动列表长度与滑条长度比例
  10. var _showLength = 750 / _totalLength * 100; //当前显示蓝色滑条的长度(保留两位小数)
  11. this.setData({
  12. slideWidth: _showLength,
  13. totalLength: _totalLength,
  14. slideRatio: _ratio
  15. })
  16. },
  17. //slideLeft动态变化
  18. getleft(e) {
  19. this.setData({
  20. slideLeft: e.detail.scrollLeft * this.data.slideRatio
  21. })
  22. },

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/862451
推荐阅读
相关标签
  

闽ICP备14008679号