javascript - JQuery Draggable/Droppable - 将克隆更改为新的 div

标签 javascript jquery jquery-ui draggable droppable

我创建了一个网站,用户可以将小部件从弹出模式拉到主放置区域并创建小部件。最初,小部件只是图像,例如:BBC。当拖到可放置区域时,它们变成从 API 提取信息的小部件。我已经使用本地存储设置了位置保存,以便当用户刷新页面时,小部件保留在其位置。

The problem I am having, is that the widget will not save its location until the user has refreshed once, then it will continue to save from here

我想我知道为什么,我只是不知道如何解决它!请参阅下面有关我收集的信息。

我的拖放代码:

$(function () {
        $("#techcrunch").draggable({ revert: 'invalid', helper:"clone",
        containment:"#dropZonetc",snap: '#dropZonetc',addClasses: false});
        $( "#dropZonetc" ).droppable({

            accept: '#techcrunch',
            activeClass: 'ui-state-hover',
            hoverClass: 'ui-state-active',
            drop: function( event, ui ) {
                var offset = ui.helper.offset();
                var dropElem = ui.helper.clone()
                $(dropElem).html("<div id='techcrunchwidget' class='widgets'><img src='http://i.newsapi.org/techcrunch-s.png' alt='Tech Crunch' height='22' width='100'><button class='closewidget' onclick='destroytechcrunch()'>X</button><div class='techCrunchContent'><ul id='techcontent'></ul><script>retrieveTechCrunchNews();</script></div></div>");
                dropElem.appendTo( this ).offset( offset ).hide().fadeIn(1500);
                localStorage.setItem("techcrunch", "true");
                $( "#techcrunch" ).remove();
                $("div.widgets").draggable();
            }
        });
    });

我的保存位置的代码:

$(function () {


        var widget3 = $("#techcrunchwidget");

        updatePosition3(widget3);

        widget3.draggable({ stop: function () {

            var left = this.offsetLeft;
            var top = this.offsetTop;

            console.log(left);
            console.log(top);
            localStorage.setItem("lefttech", left);
            localStorage.setItem("toptech", top);
            $("div.widgets").draggable();   

        }
        });

        window.addEventListener("storage", function (e) {
            updatePosition3(widget3);
        }, 

        false);


    });

        function updatePosition3(widget3) {

            var left = localStorage.getItem("lefttech");
            var top = localStorage.getItem("toptech") - 20;
            widget3.css({ left: left + "px", top: top + "px" });
            widget3[0].offsetTop = top;
            widget3[0].offsetLeft = left
        }

**

I am 80% sure it is because I am appending the dropElem which is a clone, therefore the save script is not finding the "techcrunchwidget" as it is the clone until refresh, this can be seen in the image below

**

Chrome Element Inspecter

一旦刷新,它就不会被包装在那个奇怪的类中。

感谢您的宝贵时间!

最佳答案

我会尝试简单地将新的 HTML 附加到 drop 中,然后删除助手。

drop: function( event, ui ) {
  var offset = ui.helper.offset();
  var $widget = $("<div id='techcrunchwidget' class='widgets'><img src='http://i.newsapi.org/techcrunch-s.png' alt='Tech Crunch' height='22' width='100'><button class='closewidget' onclick='destroytechcrunch()'>X</button><div class='techCrunchContent'><ul id='techcontent'></ul><script>retrieveTechCrunchNews();</script></div></div>");
  $widget.appendTo(this).offset(offset).hide().fadeIn(1500);
  localStorage.setItem("techcrunch", "true");
  ui.helper.remove();
  $("#techcrunch").remove();
  $("div.widgets").draggable();
}

未经测试的解决方案。

关于javascript - JQuery Draggable/Droppable - 将克隆更改为新的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922899/

相关文章:

jquery - jquery中如何设置图像元素的背景颜色

jQuery 每 30 秒刷新一次

javascript - 自然窗口调整大小事件会使 Chrome 中的布局无效,而强制窗口调整大小则不会。

jQuery 在 5 秒后自动隐藏元素

javascript - 使用 JQuery UI 拖放以填充空白

javascript - 什么情况下可以卸载根组件?

javascript - 如何在angularjs中将int null解析为0

javascript - react 日期选择器。如何节省状态时间?

javascript - VisJs - 获取特定节点的边数

javascript - 我如何动态更改 jquery ui Accordion 的内容选项卡