javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > 小程序上下移动切换位置

小程序实现上下移动切换位置

作者:Bright2017

这篇文章主要为大家详细介绍了小程序实现上下移动切换位置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

 本文实例为大家分享了小程序实现上下移动切换位置的具体代码,供大家参考,具体内容如下

<view wx:for="{{companyData}}" wx:key="{{item.id}}" class="overHiden">
   <view class="floarLeft">{{item.name}}</view>
   <view class="floarRight" wx:if="{{index != 0}}" data-index="{{index}}" bindtap="topClick">
   <image src="../../images/32.png" class="rightmenuImg"></image>
   </view>
   <view class="floarRight" wx:if="{{index != companyData.length-1}}" data-index="{{index}}" bindtap="bottomClick">
   <image src="../../images/33.png" class="rightmenuImg"></image>
   </view>
</view>
data{
 companyData:[
  {
   id:0,
   name:'产品1'
  },
  {
   id:1,
   name:'产品2'
  },
  {
   id:2,
   name:'产品3'
  },
  {
   id:3,
   name:'产品4'
  },
  {
   id:4,
   name:'产品5'
  }
 ]
}
//位置向上移动
topClick: function(e) {
 var that = this
 var index = e.currentTarget.dataset.index
 var one = that.data.companyData[index]
 var two = that.data.companyData[index - 1]
 var index2 = index - 1
 var data1 = "companyData[" + index + "]"
 var data2 = "companyData[" + index2 + "]"
 that.setData({
  [data1]: two,
  [data2]: one
 })
 },
 // 位置向下移动
 bottomClick: function(e) {
 var that = this
 var index = e.currentTarget.dataset.index
 var one = that.data.companyData[index]
 var two = that.data.companyData[index + 1]
 var index2 = index + 1
 var data1 = "companyData[" + index + "]"
 var data2 = "companyData[" + index2 + "]"
 that.setData({
  [data1]: two,
  [data2]: one
 })
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文