加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

微信小程序 | 切换按钮或者view的选中状态

发布时间:2020-12-14 19:35:42 所属栏目:资源 来源:网络整理
导读:实现非常简单,通过一个简单的判断当前data中的选中值,点击按钮时更新data值,重新渲染页面。 index.wxml文件 view class = "switch-type" "btn {{currentSelectTripType == 'pinche' ? 'active' : ''}}" bindtap = 'selectedPinche' data-id = 'pinche' 拼

实现非常简单,通过一个简单的判断当前data中的选中值,点击按钮时更新data值,重新渲染页面。

index.wxml文件

<view class="switch-type">
    "btn {{currentSelectTripType == 'pinche' ? 'active' : ''}}" bindtap='selectedPinche' data-id='pinche'>拼车</view>
    "btn {{currentSelectTripType == 'baoche' ? 'active' : ''}}" 'selectedBaoche' 'baoche'>包车</view>
  </view>

index.js文件

Page({
  data: {~~~~
    currentSelectTripType: 'pinche',},// 更新data 切换选中状态
  selectedPinche: function (e) {
    this.setData({
      currentSelectTripType: e.currentTarget.dataset.id
    })
  },selectedBaoche: function(e) {
    附上一个简单的样式

index.wxss

.switch-type {
  display: flex;
}
.switch-type .btn:first-child {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.switch-type .btn:last-child {
  border-top-left-radius: border-bottom-left-radius: 0;
}
.switch-type .btn {
  background: #dfdfdf;
  padding: 10rpx 40rpx;
}
.switch-type .btn.active {
  border: 2rpx solid #007bff;
  #fff;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读