限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

原生js支持鼠标拖拽拖放排序的table表格行

所属分类: 网页特效-筛选&排序    2023-12-05 11:12:22

原生js支持鼠标拖拽拖放排序的table表格行 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

原生js支持鼠标拖拽拖放排序的table表格行(共3个文件)

    • index.html

使用方法

(function() {
  "use strict";
  
  const table = document.getElementById('table');
  const tbody = table.querySelector('tbody');
  
  var currRow = null,
      dragElem = null,
      mouseDownX = 0,
      mouseDownY = 0,         
      mouseX = 0,
      mouseY = 0,      
      mouseDrag = false;  
  
  function init() {
    bindMouse();
  }
  
  function bindMouse() {
    document.addEventListener('mousedown', (event) => {
      if(event.button != 0) return true;
      
      let target = getTargetRow(event.target);
      if(target) {
        currRow = target;
        addDraggableRow(target);
        currRow.classList.add('is-dragging');


        let coords = getMouseCoords(event);
        mouseDownX = coords.x;
        mouseDownY = coords.y;      

        mouseDrag = true;   
      }
    });
    
    document.addEventListener('mousemove', (event) => {
      if(!mouseDrag) return;
      
      let coords = getMouseCoords(event);
      mouseX = coords.x - mouseDownX;
      mouseY = coords.y - mouseDownY;  
      
      moveRow(mouseX, mouseY);
    });
    
    document.addEventListener('mouseup', (event) => {
      if(!mouseDrag) return;
      
      currRow.classList.remove('is-dragging');
      table.removeChild(dragElem);
      
      dragElem = null;
      mouseDrag = false;
    });    
  }
  
  
  function swapRow(row, index) {
     let currIndex = Array.from(tbody.children).indexOf(currRow),
         row1 = currIndex > index ? currRow : row,
         row2 = currIndex > index ? row : currRow;
         
     tbody.insertBefore(row1, row2);
  }
    
  function moveRow(x, y) {
    dragElem.style.transform = "translate3d(" + x + "px, " + y + "px, 0)";
    
    let	dPos = dragElem.getBoundingClientRect(),
        currStartY = dPos.y, currEndY = currStartY + dPos.height,
        rows = getRows();

    for(var i = 0; i < rows.length; i++) { let rowElem = rows[i], rowSize = rowElem.getBoundingClientRect(), rowStartY = rowSize.y, rowEndY = rowStartY + rowSize.height; if(currRow !== rowElem && isIntersecting(currStartY, currEndY, rowStartY, rowEndY)) { if(Math.abs(currStartY - rowStartY) < rowSize.height / 2) swapRow(rowElem, i); } } } function addDraggableRow(target) { dragElem = target.cloneNode(true); dragElem.classList.add('draggable-table__drag'); dragElem.style.height = getStyle(target, 'height'); dragElem.style.background = getStyle(target, 'backgroundColor'); for(var i = 0; i < target.children.length; i++) { let oldTD = target.children[i], newTD = dragElem.children[i]; newTD.style.width = getStyle(oldTD, 'width'); newTD.style.height = getStyle(oldTD, 'height'); newTD.style.padding = getStyle(oldTD, 'padding'); newTD.style.margin = getStyle(oldTD, 'margin'); } table.appendChild(dragElem); let tPos = target.getBoundingClientRect(), dPos = dragElem.getBoundingClientRect(); dragElem.style.bottom = ((dPos.y - tPos.y) - tPos.height) + "px"; dragElem.style.left = "-1px"; document.dispatchEvent(new MouseEvent('mousemove', { view: window, cancelable: true, bubbles: true } )); } function getRows() { return table.querySelectorAll('tbody tr'); } function getTargetRow(target) { let elemName = target.tagName.toLowerCase(); if(elemName == 'tr') return target; if(elemName == 'td') return target.closest('tr'); } function getMouseCoords(event) { return { x: event.clientX, y: event.clientY }; } function getStyle(target, styleName) { let compStyle = getComputedStyle(target), style = compStyle[styleName]; return style ? style : null; } function isIntersecting(min0, max0, min1, max1) { return Math.max(min0, max0) >= Math.min(min1, max1) &&
             Math.min(min0, max0) <= Math.max(min1, max1); } init(); })();

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 筛选&排序

原生js实现的DIV标签拖拽移动排序特效

一款支持拖动排序的js插件,鼠标单击按住不放并拖动标签,可拖放到任意为止,按键放手后会自动插入到对应为止,这样就完成位置移动排序。
  筛选&排序
 2321  0

zepto手机移动端字母索引列表

一款手机端字母快速滚动定位特效,常用于手机通讯录功能,点击特定字母可滚动直达到对应的列表位置。
  筛选&排序
 3305  0

table表单数据处理插件DataTables

一款响应式数据处理插件,带分页、排序,可设置显示数据条数及即时搜索查询功能。
  筛选&排序
 3555  0

jquery支持触摸鼠标拖动排序的网格布局插件

一个支持触摸拖拽的拖动排序网格布局插件,可通过鼠标拖动或移动端触摸事件动态重新排列网格方块。还支持动态添加网格方块。
  筛选&排序
 7514  0

评论数(1) 回复有机会获得K币 用户协议

    jq小菜鸡0
    2023-12-19 17:25:18
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论