如何使元素鼠标无法点击但可通过javascript点击?

时间:2012-06-02 13:53:10

标签: javascript javascript-events jquery

我在 jQuery 中使用翻转插件,它会翻转给定的div。

代码(重用某人的代码),点击后翻转一个元素。

我有几张图片,通过使用此功能,我正在尝试制作动画。我使用 jQuery 点击这些图片。然而问题是,即使用户可以点击图像并再次翻转我不想要的图像。

我尝试使用 CSS 属性:

    body{pointer-events:none;}

这可行,但在完成动画后,我无法使用 jQuery 禁用此功能。我试过了:

    $('body').css('pointer-events','normal');

我正在重用的代码是:

$(document).ready(function() { /* The following code is executed once the DOM is loaded */


    $('.sponsorFlip').bind("click", function() {

        // $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
        var elem = $(this);

        // data('flipped') is a flag we set when we flip the element:
        if (elem.data('flipped')) {
            // If the element has already been flipped, use the revertFlip method
            // defined by the plug-in to revert to the default state automatically:
            elem.revertFlip();

            // Unsetting the flag:
            elem.data('flipped', false)
        }
        else {
            // Using the flip method defined by the plugin:
            $(this).unbind("click");
            $(this).unbind("click");
            $(this).unbind("click");
            elem.flip({
                direction: 'lr',
                speed: 350,
                onBefore: function() {
                    // Insert the contents of the .sponsorData div (hidden from view with display:none)
                    // into the clicked .sponsorFlip div before the flipping animation starts:

                    elem.html(elem.siblings('.sponsorData').html());
                }
            });

            // Setting the flag:
            elem.data('flipped', true);


        }
    });

});

我尝试添加

    $('.sponsorFlip').unbind("click");

它也无效。

3 个答案:

答案 0 :(得分:3)

$('.sponsorFlip').on('click', function(e) {
   // for a click event by mouse has e.clienX/e.clientY 
   if(e.clientX){
      // then click by mouse
   } else {
      // triggered 
   }
});

<强> DEMO (see console)

代码

$('.sponsorFlip').bind('click', function(e) {
   // for a click event by mouse has e.clienX/e.clientY 
   if(e.clientX){
      // then click by mouse
   } else {
      // triggered 
   }
});

<强> DEMO (see console)

答案 1 :(得分:0)

将它分配给点击事件,然后模拟点击,然后取消分配点击事件似乎有点乱。为什么不直接创建一个翻转元素的函数?

var options = {...};
$(element).flip(options);

答案 2 :(得分:0)

要快速解决方案,只需尝试删除这些行:

 // If the element has already been flipped, use the revertFlip method
        // defined by the plug-in to revert to the default state automatically:
        elem.revertFlip();

        // Unsetting the flag:
        elem.data('flipped', false)