微信小程序中uni-table行动态改变颜色

微信小程序中uni-table行动态改变颜色,第1张

可以通过在 uni-table 组件上绑定 v-for 来实现不同行的颜色变化。

步骤如下:

1.定义 data 属性,其中 colorArr 存放的是颜色字段:

data () {

return {

list: [],

colorArr: ['#f00', '#0f0', '#00f', '#fff']

}

},

2.在 uni-table 组件上绑定 v-for

<uni-table-column v-for="(item, index) in row.list"

:key="index"

:type="item.type"

:label="item.label"

:width="item.width"

:align="桐陵item.align"

:show-overflow-tooltip="item.showOverflowTooltip"

:class="['cell-type-'+item.type, {'bg-light': row.type=='指轮薯light'}]"

:style="{backgroundColor: colorArr[index]}"

>

{{ item.value }}

</uni-table-column>

上述代码在 uni-table 组件上绑定 v-for 来循环 list,在 uni-table-column 标签上传递行索引index 即可动态改变单元格的背景颜色。另外,也唯者可以给 uni-table-column 标签设置其他的样式

1.使用内置组件scroll-view

2.实现点击时出现的背景样式

3.使用scroll-into-view,实现点击时自动滚动

前言

本次主要内容是介绍页面tab的开发,如何实现tab与页面内容联动呢?关注我就知道!

本次效果展示

 

一、如何实现页面tab

1.使用内置组件scroll-view

如下图所示,我们需要使用到红色框框中的属性,此属性可也实现滚动

这里有一个大坑,不管是使用scroll-x还是scroll-y遍历数据都是出现在左边一数列(这里我们只关注scroll-x、scroll-y)

<scroll-view scroll-x="true" class='scroll-view-t' :scroll-into-view="scrollinto">

<view v-for="(item,index) in 20 " :key='index' :id="'tab'+index"

:class="currentIndex==index?'active':''" @click="changeTab(index)">

{{item}}

</view>

</scroll-view>

<scroll-view scroll-y="true" :style="'height:'+scrollH+'px'">

<view v-for="(item,index) in 100 " :key='index'>

{{item}}

</view>

</scroll-view>

我们只能通过样式来改变它,让它横过来

.scroll-view-t view {

display: inline-block

font-size: 32rpx !important

margin: 0 15rpx

}

.scroll-view-t {

white-space: nowrap

height: 88rpx

line-height: 88rpx

}

 

2.实现点击时出现的背景样式

这里我们需要定义一个默认的索引currentIndex,在通过点亏明击事件所传出去的索引进行判断,从而凳唤获得当前所点击的对象给到样式,样式我们就用三元表达式判断赋予样式,如下点击方法

changeTab(index) { //nabbar栏点击切换

// if (this.currentIndex === index) return

this.currentIndex = index

// this.scrollinto = 'tab' + index

// if (this.currentIndex <10) {

// this.scrollinto = 'tab0'

// }

},

如下是绑定的点击事件和三元表达式判断赋予样式,其中背景样式提前写好了 

<view v-for="(item,index) in 20 " :key='index' :id="'tab'+index"

:class="currentIndex==index?'active':''" @click="changeTab(index)">

{{item}}

</view>

 不过现在只实现了通过点击样式发生改变,感觉实现了tab切换,眼下要解决点击时tab这个导航条也要自己滚动起来,不能一边手动滚动,然后再点击吧

3.使用scroll-into-view,实现点击时自动滚动

使用它的目的主要是,在点解tab时可以实现,你向那个方向点,他就往那个方向滚动,不过在点回去的时候,就有坑了,需要对其作出判断 

 阅读文档很难理解对吧,我在这里说说我的理解,这里是想要我们通过在scroll-view中属性scroll-into-view绑定一个元素,此元素还要获得id,此枣空凯id还不能已数字开头,此id就是移动的关键,需要绑定,所遍历内容的索引,从而实现往哪里滚动,不过想点回去就需要进行判断

如下代码中在scroll-view,使用scroll-into-view绑定了一个自己定义的空元素scrollinto

在v-for遍历后获得了索引,id就通过索引进行了绑定,拼接了以tab开头

<scroll-view scroll-x="true" class='scroll-view-t' :scroll-into-view="scrollinto">

<view v-for="(item,index) in 20 " :key='index' :id="'tab'+index"

:class="currentIndex==index?'active':''" @click="changeTab(index)">

{{item}}

</view>

</scroll-view>

实现点击滚动很简单,要滚动回去就要判断了,如下代码,只要当前的this.scrollinto = 'tab' + index就能实现点击就滚动,回去是就要判断当前点击的缩影,手动赋值,给一个最好的区间,这样效果更好

changeTab(index) { //nabbar栏点击切换

if (this.currentIndex === index) return

this.currentIndex = index

this.scrollinto = 'tab' + index

if (this.currentIndex <10) {

this.scrollinto = 'tab0'

}

},


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12496892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存