63.实现一个拖拽排序

Posted famliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了63.实现一个拖拽排序相关的知识,希望对你有一定的参考价值。

//为了方便借助vue和jquery

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>拖拽排序</title>
    <style>
        ul,
        li {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        ul {
            margin: 0 auto;
            width: 200px;
        }

        .item {
            font-size: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
            padding: 8px 10px;
            margin: 15px 0;

        }

        .item.before {
            margin-top: 45px;
            transition: margin-top 0.15s ease-in;
        }

        .item.after {
            margin-bottom: 45px;
            transition: margin-bottom 0.15s ease-in;
        }
    </style>
</head>

<body>
    <div id="app">
        <ul class="items">
            <li class="item" v-for="i in 5" draggable="true" @dragstart="dragstart($event)">{{i}}</li>
        </ul>
    </div>
    <script src="./libs/jquery.min.js"></script>
    <script src="./libs/vue.js"></script>

    <script>
        new Vue({
            el: ‘#app‘,
            data: {
            },
            methods: {
                dragstart(e) {
                    let dragTarget = $(e.currentTarget);
                    let overTarget = null;
                    let allItems = dragTarget.parent().children(‘.item‘);
                    let x = null, y = null;
                    let overTargetSize = {
                        width: null,
                        height: null,
                    }
                    for (let i = 0; i < allItems.length; i++) {
                        const el = allItems[i];
                        el.ondragenter = function (evt) {
                            overTarget = $(evt.target)
                            return true;
                        }
                        el.ondragover = function (evt) {
                            x = evt.offsetX;
                            y = evt.offsetY;
                            // overTargetSize.width = evt.target.offsetWidth;
                            // overTargetSize.height = evt.target.offsetHeight;
                            $(allItems).removeClass(‘after‘).removeClass(‘before‘)
                            if (y > 19) {
                                $(evt.target).addClass(‘after‘);
                            } else if (y <= 19) {
                                $(evt.target).addClass(‘before‘);
                            }
                        }
                        el.ondragend = function (evt) {

                            if (y > 19) {
                                $(overTarget).after(dragTarget)
                            } else if (y <= 19) {
                                $(overTarget).before(dragTarget)
                            }
                            $(allItems).removeClass(‘after‘).removeClass(‘before‘);
                            x = null, y = null;
                            return false;
                        }

                    }
                },
            }
        })
    </script>
</body>

</html>

  技术图片技术图片

以上是关于63.实现一个拖拽排序的主要内容,如果未能解决你的问题,请参考以下文章

图片上传拖拽排序

帆软怎么实现报表的排序,隐藏,拖拽

ztree使用系列四(ztree实现同级拖拽排序并将排序结果保存数据库)

Java 后台接口实现同级拖拽排序代码示例

jQuery拖拽插件制作拖拽排序特效

Android:Flutter之300行代码实现拖拽排序?这次就来挑战一下 —— 挑战失败,我赌5毛没人能300行做到