如何使元素鼠标不可点击,但可以通过javascript单击

How to make an element mouse unclickable but clickable through javascript?

本文关键字:可以通过 javascript 单击 元素 何使 鼠标      更新时间:2023-09-26

我在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");

它也不起作用。

$('.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 
   }
});

演示(请参阅控制台)

为您的代码

$('.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 
   }
});

演示(请参阅控制台)

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

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

要快速解决问题,请尝试删除以下行:

 // 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)