小程序拖拽排序長按排序

js文件

var app = getApp();
var x, y, x1, y1, x2, y2;
Page({
  data: {
    all_list: [{ id: 1, text: '推薦' }, { id: 2, text: 'logo設(shè)計(jì)' }, { id: 3, text: 'ui設(shè)計(jì)' }, { id: 4, text: 'css設(shè)計(jì)' }, { id: 5, text: 'js設(shè)計(jì)' }, { id: 6, text: '裝修設(shè)計(jì)' }, { id: 7, text: '圣誕樹1' }, { id: 8, text: '圣誕樹2' }, { id: 9, text: '圣誕樹3' }, { id: 10, text: '圣誕樹4' }, { id: 11, text: '圣誕樹5' }, { id: 12, text: '圣誕樹6' }, { id: 13, text: '圣誕樹7' }, { id: 14, text: '圣誕樹8' }, { id: 15, text: '圣誕樹9' }, { id: 16, text: '圣誕樹10' }, { id: 17, text: '圣誕樹11' }, { id: 18, text: '圣誕樹12' }, { id: 19, text: '圣誕樹13' }, { id: 20, text: '圣誕樹14' }, { id: 21, text: '圣誕樹15' }, { id: 22, text: '圣誕樹16' }, { id: 23, text: '圣誕樹17' }, { id: 24, text: '圣誕樹18' }, { id: 25, text: '圣誕樹19' }, { id: 26, text: '家裝修' }],//已經(jīng)增加的列表
    current: -1,
    s_v: 10,
    s_h: 10,
    u_w: 90,
    u_h: 30,
    all_width: '',//總的寬度
    move_x: '', move_y: ''
  },
  onLoad: function () {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        var width = self.data.all_width = res.windowWidth, _w = 0, row = 0, column = 0;
        var arr = [].concat(self.data.all_list)
        arr.forEach(function (n, i) {
          n.left = (self.data.u_w + self.data.s_h) * row + self.data.s_h;
          n.top = (self.data.u_h + self.data.s_v) * column + self.data.s_v;
          n._left = n.left;
          n._top = n.top;
          _w += self.data.u_w + self.data.s_h;
          if (_w + self.data.u_w + self.data.s_h > width) {
            _w = 0;
            row = 0;
            column++;
          } else {
            row++;
          }

        });
        console.log(arr);
        self.setData({
          all_list: arr
        })
      }
    });
  },
  movestart: function (e) {
    // console.log('start');
    // currindex = e.target.dataset.index;
    // this.data.current = e.target.dataset.index;
    x = e.touches[0].clientX;
    y = e.touches[0].clientY;
    x1 = e.currentTarget.offsetLeft;
    y1 = e.currentTarget.offsetTop;
    // var arr = [].concat(this.data.all_list);
    // arr.forEach(function(n,i){
    //   n._class='move';
    //   n._style='';
    // });
    this.setData({
      current: e.target.dataset.index,
      move_x: x1,
      move_y: y1
    });
  },
  move: function (e) {
    var self = this;
    // // console.log('move',e.target.dataset.current);
    x2 = e.touches[0].clientX - x + x1;
    y2 = e.touches[0].clientY - y + y1;
    // // this.setData({
    // //   current: currindex,
    // //   start: { x: x2, y: y2 }
    // // })
    var underIndex = this.getCurrnetUnderIndex();

    // var now_current=this.data.current;
    if (underIndex != null && underIndex != this.data.current) {
      var arr = [].concat(this.data.all_list);
      this.changeArrayData(arr, underIndex, this.data.current);
      // console.log(underIndex);
      // now_current = underIndex;
      this.setData({
        all_list: arr,
        current: underIndex
      })
    }
    // console.log(self.data.current,arr);


    this.setData({
      move_x: x2,
      move_y: y2
    });
  },
  moveend: function (e) {




    this.setData({
      current: -1,
    })
  },
  changeArrayData: function (arr, i1, i2) {
    var temp = arr[i1];
    arr[i1] = arr[i2];
    arr[i2] = temp;

    var _left = arr[i1]._left, _top = arr[i1]._top;
    arr[i1]._left = arr[i2]._left;
    arr[i1]._top = arr[i2]._top;
    arr[i2]._left = _left;
    arr[i2]._top = _top;

    var left = arr[i1].left, top = arr[i1].top;
    arr[i1].left = arr[i2].left;
    arr[i1].top = arr[i2].top;
    arr[i2].left = left;
    arr[i2].top = top;

  },
  getCurrnetUnderIndex: function (endx, endy) {//獲取當(dāng)前移動下方index
    var endx = x2 + this.data.u_w / 2,
      endy = y2 + this.data.u_h / 2;
    var v_judge = false, h_judge = false, column_num = (this.data.all_width - this.data.s_h) / (this.data.s_h + this.data.u_w) >> 0;
    // console.log(endx,endy);
    var _column = (endy - this.data.s_v) / (this.data.u_h + this.data.s_v) >> 0;
    var min_top = this.data.s_v + (_column) * (this.data.u_h + this.data.s_v),
      max_top = min_top + this.data.u_h;
    // console.log('v', _column, endy, min_top, max_top);
    if (endy > min_top && endy < max_top) {
      v_judge = true;
    }
    var _row = (endx - this.data.s_h) / (this.data.u_w + this.data.s_h) >> 0;
    var min_left = this.data.s_h + (_row) * (this.data.u_w + this.data.s_h),
      max_left = min_left + this.data.u_w;
    // console.log('x', _row, endx, min_left, max_left);
    if (endx > min_left && endx < max_left) {
      h_judge = true;
    }
    if (v_judge && h_judge) {
      var index = _column * column_num + _row;
      if (index > this.data.all_list.length - 1) {//超過了
        return null;
      } else {
        return index;
      }
    } else {
      return null;
    }
  }
})

wxss

.tip{
  color:#aaa;
  font-size:12px;
}
.normal_title{
  font-size:16px;
  margin:10rpx 20rpx;
  font-weight: bold;
}
.item{
 position:absolute;
 border:1px solid black;
 font-size:13px;
 padding:7px 0px;
 width:90px;
 text-align:center;
 border-radius:90px;
}
.item_container{
  height:300rpx;
  /* border:1px solid black; */
  position:relative;
}

wxml

<view >
   <text class='normal_title'>拖動排序</text>
    <view class='item_container'>
    <view wx:for='{{all_list}}' class="item {{index==current?'moving':'normal'}}"  style="left:{{index==current?move_x:item.left}}px;top:{{index==current?move_y:item.top}}px" catchtouchmove="move" catchtouchstart="movestart" bindtouchend="moveend" data-index="{{index}}">
      {{item.text}}
    </view>
   </view> 
</view>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末腰根,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子刽严,更是在濱河造成了極大的恐慌德迹,老刑警劉巖勺远,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件增显,死亡現(xiàn)場離奇詭異疗涉,居然都是意外死亡易稠,警方通過查閱死者的電腦和手機(jī)缸废,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來驶社,“玉大人企量,你說我怎么就攤上這事⊥龅纾” “怎么了届巩?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長份乒。 經(jīng)常有香客問我恕汇,道長,這世上最難降的妖魔是什么或辖? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任拇勃,我火速辦了婚禮,結(jié)果婚禮上孝凌,老公的妹妹穿的比我還像新娘方咆。我一直安慰自己,他們只是感情好蟀架,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布瓣赂。 她就那樣靜靜地躺著,像睡著了一般片拍。 火紅的嫁衣襯著肌膚如雪煌集。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天捌省,我揣著相機(jī)與錄音苫纤,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛卷拘,可吹牛的內(nèi)容都是我干的喊废。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼栗弟,長吁一口氣:“原來是場噩夢啊……” “哼污筷!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起乍赫,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤瓣蛀,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后雷厂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惋增,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年改鲫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了器腋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡钩杰,死狀恐怖纫塌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情讲弄,我是刑警寧澤措左,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站避除,受9級特大地震影響怎披,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瓶摆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一凉逛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧群井,春花似錦状飞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至荐吉,卻和暖如春焙糟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背样屠。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工穿撮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缺脉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓悦穿,卻偏偏與公主長得像攻礼,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子咧党,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容