小程序实现展开隐藏列表 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 小程序相关 >

小程序实现展开隐藏列表

发表时间:2020-9-28

发布人:葵宇科技

浏览次数:45

一、实现思路

用户点击wxml中的某个事件后,会触发js中的函数,该函数可以动态修改css样式,效果如图所示。

二、代码

wsml:

<view class='display-title' catchtap="showHidden" data-id="1">
		<image class="list-icon-left" src="../../images/list.png"></image><text class="title-text">宣传教育</text>
		<image class="list-icon-right" src="{{arrow[0].url}}"></image>
	</view>
	<view class="{{arrow[0].css}}">
		<block wx:for="{{propagate}}" wx:key="index">
		<view wx:if="{{item.type == 1}}">
			<view class='text-content' catchtap="catchTapPropagate" data-id="{{item.id}}">{{item.title}}</view>
			</view>
		</block>
	</view>
	<view class='display-title' catchtap="showHidden" data-id="2">
		<image class="list-icon-left" src="../../images/list.png"></image><text class="title-text">法制法规</text>
		<image class="list-icon-right" src="{{arrow[1].url}}"></image>
	</view>
	<view class="{{arrow[1].css}}">
		<block wx:for="{{propagate}}" wx:key="index">
		<view wx:if="{{item.type == 2}}">
			<view class='text-content' catchtap="catchTapPropagate" data-id="{{item.id}}">{{item.title}}</view>
			</view>
		</block>
	</view>
	<view class='display-title' catchtap="showHidden" data-id="3">
		<image class="list-icon-left" src="../../images/list.png"></image><text class="title-text">案例警示</text>
		<image class="list-icon-right" src="{{arrow[2].url}}"></image>
	</view>
	<view class="{{arrow[2].css}}">
		<block wx:for="{{propagate}}" wx:key="index">
		<view wx:if="{{item.type == 3}}">
			<view class='text-content' catchtap="catchTapPropagate" data-id="{{item.id}}">{{item.title}}</view>
		</view>
		</block>
	</view>

js:

 data: {
    arrow:[{url:"../../images/down.png",css:"content-hidden",state:0},    
           {url:"../../images/down.png",css:"content-hidden",state:0}, 
           {url:"../../images/down.png",css:"content-hidden",state:0}],
}
showHidden: function (e) {
  var that=this
  var id = e.currentTarget.dataset.id;
  console.log(that.data.arrow)
  if(id==1){
    if(that.data.arrow[0].state==0){
      that.setData({
        'arrow[0].url':'../../images/up.png',
        'arrow[0].css':'content-show',
        'arrow[0].state':1,
      })
    }else{
      that.setData({
        'arrow[0].url':'../../images/down.png',
        'arrow[0].css':'content-hidden',
        'arrow[0].state':0,
      })
      }
  }else if(id==2){
    if(that.data.arrow[1].state==0){
      that.setData({
        'arrow[1].url':'../../images/up.png',
        'arrow[1].css':'content-show',
        'arrow[1].state':1,
      })
    }else{
      that.setData({
        'arrow[1].url':'../../images/down.png',
        'arrow[1].css':'content-hidden',
        'arrow[1].state':0,
      })
      }
  }else{
    if(that.data.arrow[2].state==0){
      that.setData({
        'arrow[2].url':'../../images/up.png',
        'arrow[2].css':'content-show',
        'arrow[2].state':1,
      })
    }else{
      that.setData({
        'arrow[2].url':'../../images/down.png',
        'arrow[2].css':'content-hidden',
        'arrow[2].state':0,
      })
      }
  }
},

wxss:

.content-hidden{
  width: 100%;
  font-size: large;
  font-family: Georgia, 'Times New Roman', Times, serif;
  display: none;
}
.content-show{
  width: 100%;
  font-size: large;
  font-family: Georgia, 'Times New Roman', Times, serif;
  display: block;
}

相关案例查看更多