2

jqueryドラッグ可能をDIVコンテナにアタッチし、その各子要素にクリックイベントがあります。メインの親コンテナをドラッグしたときに、子イベントが発生しないようにするにはどうすればよいですか。

子イベントを発生させるのは、クリックしたときだけで、全体をドラッグしたときではありません。

デモを作成しました:http://jsfiddle.net/sygad/RgdPq/1/

助けていただければ幸いです。

4

3 に答える 3

3

このjsFiddleの例のように、一時変数を使用してドラッグステータスを追跡し、クリックイベントからマウスアップイベントに変更できます。

jQuery

var bodyWidth = $(window).width();
var bodyHeight = $(window).height();
var dragging = false;
$("#container").draggable({
    containment: "window",
    start: function() {
        dragging = true;
        $(this).addClass('dragged')
    },
    stop: function() {
        dragging = false;
        $(this).removeClass('dragged')
    }
});

$('.box').mouseup(function() {
    if (!dragging) {
        //If there is already a new box present, remove it
        if ($('.newBox').length) {
            $('.newBox').remove();
        }

        //Get the offset of THIS box we just clicked
        var thisOffset = getOffset(this)

        //Get its left & top value
        var newBoxLeft = thisOffset.left;
        var newBoxTop = thisOffset.top;

        //Full size box will be 75% width & height of viewport
        var newBoxFinalWidth = bodyWidth * 0.75;
        var newBoxFinalHeight = bodyHeight * 0.75;

        //Full size box will be positioned center/center on the screen
        var newBoxDestLeft = (bodyWidth - newBoxFinalWidth) / 2;
        var newBoxDestTop = (bodyHeight - newBoxFinalHeight) / 2;

        //Create our new box
        var newBox = '<div class="newBox clickOut"></div>'

        //Add it to the DOM
        $('body').append(newBox);

        //Position it to overlay the box we just clicked
        $('.newBox').css({
            'left': newBoxLeft,
            'top': newBoxTop
        })

        //Animate it from the orig box position to its final width/height & left/top
        $('.newBox').delay(100).animate({
            'width': newBoxFinalWidth,
            'height': newBoxFinalHeight,
            'left': newBoxDestLeft,
            'top': newBoxDestTop
        }, 2000, 'easeOutExpo')
    }
})

//Clickout function
$(document).click(function(e) { /*if parent of this click is NOT clickout AND this click is NOT clickout then hide stuff*/
    if ($(e.target).parents().is('.clickOut') == false && $(e.target).is('.clickOut') == false) {
        clickOut();
    }
});

function clickOut() {
    $('.newBox').remove()
}

function getOffset(item) {
    var cssOffset = $(item).offset();
    var cssLeft = parseInt(cssOffset.left);
    var cssTop = parseInt(cssOffset.top);

    return {
        'left': cssLeft,
        'top': cssTop
    };
}​
于 2012-08-03T19:19:09.287 に答える
0

私はこれが古い投稿であることを知っています。私は一日中私を悩ませた同様の何かに取り組みました!より良い解決策、つまりグローバル変数を保存し、オブジェクトを自己完結型にする:

$('#somethingDraggableToBeClicked').mouseup(function() {
  if ( $("#someParentOrChildElementBeingDragged").is('.ui-draggable-dragging') ) 
    return;
    //your onClick code here...
}
于 2013-05-31T14:54:16.290 に答える
0

この問題も数時間私を悩ませました。最後に、私は次のことを行いました。

var dragged = false; // Flag

$( '#parent' ).draggable({
    start: function () {
        dragged = true;
    }
});

$( '#child' ).click( function () {
    if ( dragged ) {
        dragged = true; // Reset the flag
        return;
    }
    // Your click code here
});
于 2016-08-13T19:27:27.567 に答える