javascript - 删除子项时 jQuery touchmove 停止工作

标签 javascript jquery gesture

我有两个 div:

<div class="main">
    <div class="child" />
</div>

我为主元素创建了一个事件监听器,它使用户能够拖动子元素

map.bind('touchmove', function(f) {
                if (map.pinch) {
                    return;
                }
                f.preventDefault();
                f.originalEvent.preventDefault();
                f = f.originalEvent.touches[0];
                map.handleMoveEvent(f);
            });

map.handleMoveEvent = function(f) {
                mover.css('left', map.getX() + (f.clientX - map.lastmouse.clientX) / scaler.scale);
                mover.css('top', map.getY() + (f.clientY - map.lastmouse.clientY) / scaler.scale);
                map.lastmouse = f;
                map.moverMoved();
        }

map.moverMoved = function() {

            if (map.getX() > 0) {
                map.tileX--;
                mover.moveX(-TILE_WIDTH);
                map.find(".child").moveX(TILE_WIDTH);
                map.updateVisibleTiles();
            }
            if (map.getX() < -TILE_WIDTH) {
                map.tileX++;
                mover.moveX(TILE_WIDTH);
                map.find(".child").moveX(-TILE_WIDTH);
                map.updateVisibleTiles();
            }
            if (map.getY() > 0) {
                map.tileY--;
                mover.moveY(-TILE_HEIGHT);
                map.find(".child").moveY(TILE_HEIGHT);
                map.updateVisibleTiles();
            }
            if (map.getY() < -TILE_HEIGHT) {
                map.tileY++;
                mover.moveY(TILE_HEIGHT);
                map.find(".child").moveY(-TILE_HEIGHT);
                map.updateVisibleTiles();
            }

            $('#console').html(map.tileX + " " + map.tileY);
            //$('#console').html(map.getX() + " " + map.getY());
        };

map.updateVisibleTiles = function() {
                mover.find(".child").remove();
                for (x = Math.max(map.tileX - tiledistance, 0); x <= Math.min(map.tileX + tiledistance, map.getTileCount() - 1); x++) {
                    for (y = Math.max(map.tileY - tiledistance, 0); y <= Math.min(map.tileY + tiledistance, map.getTileCount() - 1); y++) {
                        var child = $(document.createElement('div'));
                        child.width(TILE_WIDTH);
                        child.height(TILE_HEIGHT);
                        var farbe = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
                        child.css("background-color", farbe);
                        child.css('position', 'absolute');
                        child.addClass('child');
                        child.css('left', (x - map.tileX) * TILE_WIDTH);
                        child.css('top', (y - map.tileY) * TILE_HEIGHT);
                        mover.append(child);
                    }
                }
            }

在某些情况下,我必须在拖动时交换 child

$('.main').find(".child").remove();

但是移除后 touchmove 不再触发。仅当我通过点击子元素启动 touchmove 时才会发生这种情况...这个问题对我来说很明显,但是解决方案是什么?

最佳答案

$().remove() 删除 dom 元素及其事件监听器。使用 $().detach() 保留事件监听器。

http://api.jquery.com/detach/

The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

关于javascript - 删除子项时 jQuery touchmove 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24126738/

相关文章:

javascript - 在 React Button onClick 上,启动和停止函数(方法)

javascript - 我写了一个选项卡控件,但我无法让 css 在所有浏览器中工作,我该如何解决?

javascript - nodejs 中的 for 循环不适用于 redis hexists

javascript - 单击键盘选项卡按钮时显示下拉菜单

javascript - JsTree 的节点未使用在 IE 中的类型插件中指定的图标

javascript - 如何根据div计算自定义滚动条的宽度?

android - 如何将 OnTouchListener 添加到 ListView 项?

ios - 平移手势 - 滑动手势冲突

android - 确定多个 View 中哪个VIEW有手势(双击、向左滑动、向右滑动等)?

jquery - 如何使用 CSS 和 jQuery 将文本区域扩展到其内容并禁用手动调整大小