拖放并在放下的坐标处定位

时间:2013-02-28 12:55:13

标签: javascript jquery html5 css3

我需要将产品列表中的项目拖放到空白容器中。 当用户完成拖放操作后,我必须将位置和使用过的项目存储到db,以便我可以再次显示此配置。

不幸的是,我的问题在早期就开始了。 我成功地将项目从列表拖放到容器,但遗憾的是它们以表格形式显示,而不是在我指定给它们的坐标处显示

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShoppingCart.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var srcElement;

        $(document).ready(function () {

            $("div .product").each(function () {
                this.addEventListener('dragstart', OnDragStart, false);
            });

            $("div .bag").each(function () {
                this.addEventListener('dragenter', OnDragEnter, false);
                this.addEventListener('dragleave', OnDragLeave, false);
                this.addEventListener('dragover', OnDragOver, false);
                this.addEventListener('drop', OnDrop, false);
                this.addEventListener('dragend', OnDragEnd, false);
            });

        })

        function OnDragStart(event) {
            srcElement = this;

        }

        function OnDragOver(e) {
            e.preventDefault();


            return false;
        }

        function OnDragEnter(e) {
            e.preventDefault();
        }

        function OnDragLeave(e) {
            e.preventDefault();
        }

        function OnDrop(e) {
            e.preventDefault();

            var dm = srcElement.cloneNode(true);

            dm.style.Left = e.offsetX;
            dm.style.Top = e.offsetY;
            dm.style.position = "absolute";

            //$(this).append(srcElement);
            e.target.appendChild(dm); 

            e.stopPropagation();
            return false;
        }

        function OnDragEnd(e) {
            e.preventDefault();
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table >
                <tr>
                    <td>
                        <div draggable="true" id="div1" class="product">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/monitor.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                    <td>
                        <div id="div2" class="product" draggable="true">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/Cart.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                    <td>
                        <div id="div3" class="product" draggable="true">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/mouse.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                    <td>
                        <div id="div4" class="product" draggable="true">
                            <header>Rieker Trekkingsandalen braun</header>
                            <img src="images/speaker.jpg" style="height: 150px; width: 150px;" />
                        </div>
                    </td>
                </tr>
            </table>

            <div class="bag">
            </div>
        </div>
    </form>
</body>
</html>

.product {
    height: 300px;
    width: 150px;
    border: 2px solid #666666;
    background-color: white;
    text-align: center;
    cursor: move;
}
.bag {
    background-color: green;
    height: 500px;
    width: 500px;
    position: absolute;
}

有人能指出我这个问题吗?

干杯, 斯蒂芬

1 个答案:

答案 0 :(得分:0)

dm.style.position = "absolut";

应该是

dm.style.position = "absolute";