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

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

所属分类: 网页特效-筛选&排序    2024-12-25 02:53:03

原生js实现的DIV标签拖拽移动排序特效 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

原生js实现的DIV标签拖拽移动排序特效(共3个文件)

    • index.html

使用方法

  • code
  • source
  1. const container = document.getElementById('container');
  2. const fake = document.getElementById('fake');
  3. const margin = 10;
  4. let divs = ['apple','banana','pear','strawberry','watermelon','blueberry', 'pineapple'].map(txt => {
  5. const d = document.createElement('div');
  6. d.classList.add('block');
  7. d.classList.add(txt);
  8. Object.assign(d.style,{
  9. margin: margin + 'px',
  10. })
  11. d.innerHTML = txt;
  12. container.appendChild(d);
  13. return d;
  14. });
  15. divs.forEach((d,startidx) => {
  16. d.addEventListener('mousedown',()=>{
  17. const x0 = container.getBoundingClientRect().x;
  18. const y0 = container.getBoundingClientRect().y;
  19. const others = divs.filter(d2 => d !== d2);
  20. const positions = others.map(d => d.getBoundingClientRect().x);
  21. const widths = others.map(d => d.getBoundingClientRect().width);
  22. let moved = false;
  23. let finalpos = d.getBoundingClientRect().x;
  24. let finalidx = startidx;
  25. const selfClone = d.cloneNode(true);
  26. const clones = [];
  27. function movedCBs(){
  28. Object.assign(selfClone.style,{
  29. left: d.getBoundingClientRect().x + 'px',
  30. top: d.getBoundingClientRect().y + 'px',
  31. position: 'fixed',
  32. margin: 0,
  33. 'z-index': 2
  34. })
  35. fake.appendChild(selfClone);
  36. container.classList.add('hidden');
  37. others.forEach((d,i) => {
  38. const position = positions[i];
  39. const clone = d.cloneNode(true);
  40. Object.assign(clone.style,{
  41. position: 'fixed',
  42. left: position + 'px',
  43. top: y0 + margin + 'px',
  44. transition: '.2s',
  45. margin: 0,
  46. })
  47. fake.appendChild(clone);
  48. clones.push(clone);
  49. });
  50. }
  51. function transitionEnd(){
  52. fake.innerHTML = '';
  53. container.classList.remove('hidden');
  54. d.parentNode.removeChild(d);
  55. console.log(finalidx)
  56. container.insertBefore(d, container.children[finalidx]);
  57. divs = Array.from(container.children);
  58. }
  59. function move(e){
  60. if (!moved){
  61. movedCBs();
  62. moved = true;
  63. }
  64. const posx = selfClone.getBoundingClientRect().x + e.movementX
  65. Object.assign(selfClone.style,{
  66. left: posx + 'px',
  67. top: selfClone.getBoundingClientRect().y + e.movementY + 'px'
  68. })
  69. let pos = x0 + margin;
  70. let counter;
  71. let spliced = false;
  72. for (counter=0; counter<widths.length; counter++){
  73. const nextpos = pos + widths[counter] + margin*2;
  74. if (!spliced && Math.abs(nextpos - posx) >= Math.abs(pos - posx)){
  75. spliced = true;
  76. finalpos = pos;
  77. finalidx = counter;
  78. }
  79. Object.assign(clones[counter].style,{
  80. left: pos + (spliced ? selfClone.getBoundingClientRect().width + margin*2 : 0) + 'px'
  81. })
  82. pos = nextpos;
  83. }
  84. if (!spliced){
  85. finalpos = pos;
  86. finalidx = counter;
  87. }
  88. }
  89. document.addEventListener('mousemove', move );
  90. document.addEventListener('mouseup', ()=>{
  91. document.removeEventListener('mousemove',move);
  92. if (!moved) return;
  93. Object.assign(selfClone.style,{
  94. transition: '.2s'
  95. });
  96. setTimeout(()=>{
  97. Object.assign(selfClone.style,{
  98. left: finalpos + 'px',
  99. top: y0 + margin + 'px'
  100. });
  101. },0)
  102. setTimeout(transitionEnd, 200)
  103. },{once:true})
  104. })
  105. })
const container = document.getElementById('container');

const fake = document.getElementById('fake');

const margin = 10;

let divs = ['apple','banana','pear','strawberry','watermelon','blueberry', 'pineapple'].map(txt => {
    const d = document.createElement('div');
    d.classList.add('block');
    d.classList.add(txt);
    Object.assign(d.style,{
        margin: margin + 'px',
    })
    d.innerHTML = txt;
    container.appendChild(d);
    return d;
});


divs.forEach((d,startidx) => {
    d.addEventListener('mousedown',()=>{
        
        const x0 = container.getBoundingClientRect().x;
        const y0 = container.getBoundingClientRect().y;
        
        const others = divs.filter(d2 => d !== d2);
        const positions = others.map(d =>  d.getBoundingClientRect().x);
        const widths = others.map(d =>  d.getBoundingClientRect().width);
        
        let moved = false; 
        
        let finalpos = d.getBoundingClientRect().x;
        let finalidx = startidx;
        
        const selfClone = d.cloneNode(true);
        const clones = [];
        
        function movedCBs(){
                Object.assign(selfClone.style,{
                    left: d.getBoundingClientRect().x + 'px',
                    top: d.getBoundingClientRect().y + 'px',
                    position: 'fixed',
                    margin: 0,
                    'z-index': 2
                })
              fake.appendChild(selfClone);
              container.classList.add('hidden');
            
            others.forEach((d,i) => {
                const position = positions[i];
                const clone = d.cloneNode(true);
                Object.assign(clone.style,{
                    position: 'fixed',
                    left: position + 'px',
                    top: y0 + margin + 'px',
                    transition: '.2s',
                    margin: 0,
                })
                fake.appendChild(clone);
                clones.push(clone);
            });
        }
    
        
        function transitionEnd(){
            fake.innerHTML = '';
            container.classList.remove('hidden');
            
            d.parentNode.removeChild(d);
            console.log(finalidx)
            container.insertBefore(d, container.children[finalidx]);
            divs = Array.from(container.children);    
        }
    
    
        function move(e){
            
            if (!moved){
                movedCBs();
                moved = true;
            } 
            
            const posx = selfClone.getBoundingClientRect().x + e.movementX
            Object.assign(selfClone.style,{
                left: posx + 'px',
                top: selfClone.getBoundingClientRect().y + e.movementY + 'px'
            })
            
            let pos = x0 + margin;
            let counter;
            let spliced = false;
            for (counter=0; counter<widths.length; counter++){
                const nextpos = pos + widths[counter] + margin*2;
                if (!spliced && Math.abs(nextpos - posx) >= Math.abs(pos - posx)){
                    spliced = true;
                    finalpos = pos;
                    finalidx = counter;
                }
                
                Object.assign(clones[counter].style,{
                    left: pos + (spliced ? selfClone.getBoundingClientRect().width + margin*2 : 0) + 'px'
                })
            
                pos = nextpos;
            }
            if (!spliced){
                 finalpos = pos;
                 finalidx = counter;
            }
        }
        
        
        
        document.addEventListener('mousemove', move );
        document.addEventListener('mouseup', ()=>{
            
            document.removeEventListener('mousemove',move);
            if (!moved) return;
            
            Object.assign(selfClone.style,{
                transition: '.2s'
            });
            setTimeout(()=>{
                Object.assign(selfClone.style,{
                    left: finalpos + 'px',
                    top: y0 + margin + 'px'
                });
            },0)
            setTimeout(transitionEnd, 200)
        },{once:true})
        
    })
    
})

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

jquery带筛选菜单按钮的显示隐藏特效代码

一款带筛选功能的分类导航菜单,无刷新收起展开动画特效,很实用。
  筛选&排序
 2345  0

jquery支持多选过滤的下拉菜单插件

一个高级的多选下拉组件,可对选项进行排序、筛选、选中/取消选中,非常实用。
  筛选&排序
 8476  0

sortablejs创建的拖放排序列表特效代码

一款列表布局中支持拖放排序的效果,可动态排序列表顺序,通过鼠标拖放重新排列轻松的调整位置。
  筛选&排序
 3590  0

jquery多条件下拉菜单选择筛选特效代码

一款支持多条件同时选择的下拉菜单,支持单选、多选功能,很实用。
  筛选&排序
 1384  0

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

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论