轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

滑动的顶部tab选项卡,简易table表格,swiper图片显示不完整

发布者: risinvren | 发布时间: 2018-5-12 10:05| 查看数: 7374| 评论数: 1|帖子模式

作者:oys1341,来自原文地址

一:会滑动的顶部tab选项卡

主要是在scroll-view设置scroll-x=“true”,然后在设置scroll-left(横向滚动条位置)为一定的数值,就可以了

不废话直接进入主题

test.wxml

  1. <scroll-view class="tab-scoller " scroll-x="true" scroll-left="{{scrollLength}}">
  2. <block wx:for="{{listTab}}" wx:key="code">
  3. <view class="tab-view" data-index="{{index}}" id="{{item.code}}" bindtap="reflashData">
  4. <text class="tab-text active" wx:if="{{index == curIndex}}">{{item.text}}</text>
  5. <text class="tab-text" wx:else>{{item.text}}</text>
  6. </view>
  7. </block>
  8. </scroll-view>
  9. <view style="width:100%;heigth:100%;">
  10. <text style="position:absolute;top:50%;left:40%; font-size:1.5rem">{{curText}}</text>
  11. </view>

test.wxss

  1. .tab-scoller {
  2. background: linear-gradient(to bottom, #2262fc, rgba(57, 134, 222, 0.84));
  3. height: 3rem;
  4. white-space: nowrap;
  5. display: flex;
  6. }
  7. /*取消移动条*/
  8. ::-webkit-scrollbar {
  9. width: 0;
  10. height: 0;
  11. color: transparent;
  12. }
  13. .active {
  14. color: #000 !important;
  15. background-color: #fff;
  16. }
  17. .tab-view {
  18. text-align: center;
  19. color: #fff;
  20. font-size: 1rem;
  21. height: 1.2rem;
  22. width: 4rem;
  23. margin-top: 1rem;
  24. border-right: 1px solid #fff;
  25. display: inline-block;
  26. line-height: 1.2rem;
  27. }
  28. .tab-text {
  29. display: block;
  30. bottom: 0.4rem;
  31. position: relative;
  32. height: 1.5rem;
  33. margin: 0 5%;
  34. border-radius: 0.5rem;
  35. padding: 0.3rem 0.2rem 0;
  36. color: #fff;
  37. }

test.js

  1. // pages/more/test.js
  2. Page({
  3. data: {
  4. listTab:[
  5. {"code":"01","text":"tab1"},
  6. {"code":"02","text":"tab2"},
  7. {"code":"03","text":"tab3"},
  8. {"code":"04","text":"tab4"},
  9. {"code":"05","text":"tab5"},
  10. {"code":"06","text":"tab6"},
  11. {"code":"07","text":"tab7"}
  12. ],
  13. curIndex:0,
  14. curText:null,
  15. scrollLength: 0
  16. },
  17. onLoad: function () {
  18. console.log('onLoad')
  19. this.initData(0)
  20. },
  21. //初始化数据
  22. initData:function(index){
  23. var that = this
  24. this.setData({
  25. curIndex:index,
  26. curText:that.data.listTab[index].text,
  27. })
  28. },
  29. //tab点击事件,刷新数据
  30. reflashData:function(event){
  31. var that = this
  32. var index = event.currentTarget.dataset.index
  33. //移动滚动条,//200和35是我估算的
  34. if(index > this.data.curIndex ){
  35. if(that.data.scrollLength < 200){
  36. this.setData({
  37. scrollLength: that.data.scrollLength + 35 * (index - that.data.curIndex)
  38. })
  39. }
  40. }else{
  41. if(that.data.scrollLength > 0){
  42. this.setData({
  43. scrollLength: that.data.scrollLength - 35 * (that.data.curIndex - index)
  44. })
  45. }
  46. }
  47. //移动view位置,改变选中颜色
  48. this.initData(index)
  49. },
  50. })

二:简易table表格

由于需要开发小程序,前端又是自己弄,类似table的标签也没有,后来看到小程序文档中推荐使用flex布局,就把css中的flex布局学了一遍,效果还行,大家将就看一下 

table.wxml

  1. <view class="table">
  2. <view class="tr bg-w">
  3. <view class="th">head1</view>
  4. <view class="th">head2</view>
  5. <view class="th ">head3</view>
  6. </view>
  7. <block wx:for="{{listData}}" wx:key="{{code}}">
  8. <view class="tr bg-g" wx:if="{{index % 2 == 0}}">
  9. <view class="td">{{item.code}}</view>
  10. <view class="td">{{item.text}}</view>
  11. <view class="td">{{item.type}}</view>
  12. </view>
  13. <view class="tr" wx:else>
  14. <view class="td">{{item.code}}</view>
  15. <view class="td">{{item.text}}</view>
  16. <view class="td">{{item.type}}</view>
  17. </view>
  18. </block>
  19. </view>

table.wxss

  1. .table {
  2. border: 0px solid darkgray;
  3. }
  4. .tr {
  5. display: flex;
  6. width: 100%;
  7. justify-content: center;
  8. height: 3rem;
  9. align-items: center;
  10. }
  11. .td {
  12. width:40%;
  13. justify-content: center;
  14. text-align: center;
  15. }
  16. .bg-w{
  17. background: snow;
  18. }
  19. .bg-g{
  20. background: #E6F3F9;
  21. }
  22. .th {
  23. width: 40%;
  24. justify-content: center;
  25. background: #3366FF;
  26. color: #fff;
  27. display: flex;
  28. height: 3rem;
  29. align-items: center;
  30. }

table.js

  1. Page({
  2. data: {
  3. listData:[
  4. {"code":"01","text":"text1","type":"type1"},
  5. {"code":"02","text":"text2","type":"type2"},
  6. {"code":"03","text":"text3","type":"type3"},
  7. {"code":"04","text":"text4","type":"type4"},
  8. {"code":"05","text":"text5","type":"type5"},
  9. {"code":"06","text":"text6","type":"type6"},
  10. {"code":"07","text":"text7","type":"type7"}
  11. ]
  12. },
  13. onLoad: function () {
  14. console.log('onLoad')
  15. }
  16. })

效果图如下

三:swiper图片显示不完整

我把微信小程序中swiper的官方代码拷贝下来发现图片显示不完全,像这样不能完全填满整个宽度,后来看官方image标签的解释和描述,各种mode换了个遍,css也修改了很多次,还是不会(水平不到家······)

后来就想到把图片作为背景,把官方的代码修改一下,好像就行了, 
官方wxml代码

  1. <swiper indicator-dots="{{indicatorDots}}"
  2. autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  3. <block wx:for="{{imgUrls}}">
  4. <swiper-item>
  5. <image src="{{item}}" class="slide-image" width="355" height="150"/>
  6. </swiper-item>
  7. </block>
  8. </swiper>

修改后的代码

  1. <swiper indicator-dots="{{indicatorDots}}"
  2. autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  3. <block wx:for="{{imgUrls}}">
  4. <!--差别就再这里了-->
  5. <swiper-item style="background:url({{item}});background-repeat: no-repeat;background-size:100% 200px; ">
  6. </swiper-item>
  7. </block>
  8. </swiper>

反正经过这次,以后发现图片显示不全的问题,就试试把图片当作背景吧

最新评论

kimjfsim 发表于 2022-6-15 06:53
可以免费下载代码的网站

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表