简体   繁体   English

当用户跨页面移动 div 时显示 div 的原始 position

[英]Show div's original position as user moves div across page

I have a sortable list.我有一个可排序的列表。

When the user drags a list item, I'd like to stylize the item's original position until the user stops dragging.当用户拖动列表项时,我想对该项目的原始 position 进行样式化,直到用户停止拖动。

Along the lines of this:沿着这条线: 在此处输入图像描述

In this case, there is a border around a blank space in the list, indicating Poland Spring's original position.在这种情况下,列表中的空白区域周围有一个边框,表示波兰春天的原始position。

My original idea was to place a container div directly behind each li item.我最初的想法是在每个li项目后面直接放置一个容器 div。 However, for that to work, the container div would need to be position:relative and the li items would need to be position:absolute but for the purpose of a list, li items need to be position:relative .但是,要使其正常工作,容器 div 需要为position:relative并且li项需要为position:absolute但出于列表的目的, li项需要为position:relative

Here is my relevant code:这是我的相关代码:

import styled from '@emotion/styled';

const App = styled('div')`
    font-family: sans-serif;
    font-size: 1.5rem;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;

    ul {
        margin: 0;
        padding: 0;
        list-style: none; !important
    }

    ul li {
        background-color: #D3D3D3;
        padding: 10px 20px;
        position: relative;
        //the line below does not work
        //position: absolute;
        display: flex;
        line-height: 1;
        list-style-type: none;
        border-style: solid;
        margin-top:10px;
        cursor: pointer 

    }
`;
const Container = styled('div')`
    background: red;
    border-color: coral;
    position: relative;
`;
class DragDropList extends React.Component {
*/ ....*/
onDragStart = (e, index) => {
        this.draggedItem = this.state.items[index];
        e.dataTransfer.effectAllowed = 'move';
        e.dataTransfer.setData('text/html', e.target.parentNode);
        //e.dataTransfer.setDragImage(e.target.parentNode, 20, 20);
    };

    onDragOver = index => {
        const draggedOverItem = this.state.items[index];

        // if the item is dragged over itself, ignore
        if (this.draggedItem === draggedOverItem) {
            return;
        }

        // filter out the currently dragged item
        let order = this.state.order.filter(item => item !== this.draggedItem);

        // add the dragged item after the dragged over item
        order.splice(index, 0, this.draggedItem);

        this.setState({ order });
    };

    onDragEnd = index => {
        this.draggedIdx = null;
        // filter out the currently dragged item
        let items = this.state.order;
        this.setState({ items });
    };

    render() {
        return (
            <App>
                <main>
                    <ul>
                        {this.state.items.map((item, idx) => (
                            <div>
                                <Container>
                                    <li
                                        key={item + `idx`}
                                        onDragOver={() => this.onDragOver(idx)}
                                        draggable
                                        onDragStart={e =>
                                            this.onDragStart(e, idx)
                                        }
                                        onDragEnd={() => this.onDragEnd(idx)}
                                    >
                                        <div
                                            draggable
                                            onDragStart={e =>
                                                this.onDragStart(e, idx)
                                            }
                                            onDragEnd={() =>
                                                this.onDragEnd(idx)
                                            }
                                        >
                                            <span
                                                className="content"
                                                style={{ cursor: 'pointer' }}
                                            >
                                                {item}
                                            </span>
                                        </div>
                                    </li>
                                </Container>
                            </div>
                        ))}
                    </ul>
                </main>
            </App>
        );
    }
}

Here is a sandbox: https://codesandbox.io/s/black-dust-hqm2t?fontsize=14这是一个沙箱: https://codesandbox.io/s/black-dust-hqm2t?fontsize=14

Not sure if this is the best way to do it, but I changed the CSS of the dragged item by storing the dragged index in the state and adding a simple conditional to change the className if it matches the dragged index.不确定这是否是最好的方法,但我通过将拖动的索引存储在 state 中并添加一个简单的条件来更改拖动项的 CSS 并添加一个简单的条件来更改className如果它与拖动的索引匹配。 To achieve the exact effect you posted may require a bit more CSS trickery or other conditional rendering, but this should cover the basic functionality.为了达到您发布的确切效果,可能需要更多的 CSS 技巧或其他条件渲染,但这应该涵盖基本功能。

Forked sandbox . 分叉的沙箱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM