代码之家  ›  专栏  ›  技术社区  ›  SnickersAreMyFave

如何在jQuery中监听并保持单击?

  •  110
  • SnickersAreMyFave  · 技术社区  · 15 年前

    是否有jQuery核心功能或已经启用的插件?

    我应该自己滚吗?我应该从哪里开始?

    8 回复  |  直到 13 年前
        1
  •  165
  •   Machado AviD    9 年前
    var timeoutId = 0;
    
    $('#myElement').on('mousedown', function() {
        timeoutId = setTimeout(myFunction, 1000);
    }).on('mouseup mouseleave', function() {
        clearTimeout(timeoutId);
    });
    

    编辑

    编辑2 :对每个gnarf具有相同处理程序的两个事件使用立即绑定

        2
  •  13
  •   gnarf    15 年前

    航空编码(但测试时间 this fiddle )

    (function($) {
        function startTrigger(e) {
            var $elem = $(this);
            $elem.data('mouseheld_timeout', setTimeout(function() {
                $elem.trigger('mouseheld');
            }, e.data));
        }
    
        function stopTrigger() {
            var $elem = $(this);
            clearTimeout($elem.data('mouseheld_timeout'));
        }
    
    
        var mouseheld = $.event.special.mouseheld = {
            setup: function(data) {
                // the first binding of a mouseheld event on an element will trigger this
                // lets bind our event handlers
                var $this = $(this);
                $this.bind('mousedown', +data || mouseheld.time, startTrigger);
                $this.bind('mouseleave mouseup', stopTrigger);
            },
            teardown: function() {
                var $this = $(this);
                $this.unbind('mousedown', startTrigger);
                $this.unbind('mouseleave mouseup', stopTrigger);
            },
            time: 750 // default to 750ms
        };
    })(jQuery);
    
    // usage
    $("div").bind('mouseheld', function(e) {
        console.log('Held', e);
    })
    
        3
  •  8
  •   Tony Smith    12 年前

    如果有人感兴趣的话,我为此制作了一个简单的JQuery插件。

    http://plugins.jquery.com/pressAndHold/

        4
  •  5
  •   VK Da NINJA    10 年前

    setTimeout 召集 mousedown mouseup (如果 松开鼠标 在超时完成之前发生)。

    但是,似乎有一个插件: longclick .

        5
  •  1
  •   SnickersAreMyFave    15 年前

    $.liveClickHold = function(selector, fn) {
    
        $(selector).live("mousedown", function(evt) {
    
            var $this = $(this).data("mousedown", true);
    
            setTimeout(function() {
                if ($this.data("mousedown") === true) {
                    fn(evt);
                }
            }, 500);
    
        });
    
        $(selector).live("mouseup", function(evt) {
            $(this).data("mousedown", false);
        });
    
    }
    
        6
  •  1
  •   kayz1    12 年前
        var _timeoutId = 0;
    
        var _startHoldEvent = function(e) {
          _timeoutId = setInterval(function() {
             myFunction.call(e.target);
          }, 1000);
        };
    
        var _stopHoldEvent = function() {
          clearInterval(_timeoutId );
        };
    
        $('#myElement').on('mousedown', _startHoldEvent).on('mouseup mouseleave', _stopHoldEvent);
    
        7
  •  0
  •   Sergey Semushin Jessica    9 年前

    我写了一些代码来简化

    //Add custom event listener
    $(':root').on('mousedown', '*', function() {
        var el = $(this),
            events = $._data(this, 'events');
        if (events && events.clickHold) {
            el.data(
                'clickHoldTimer',
                setTimeout(
                    function() {
                        el.trigger('clickHold')
                    },
                    el.data('clickHoldTimeout')
                )
            );
        }
    }).on('mouseup mouseleave mousemove', '*', function() {
        clearTimeout($(this).data('clickHoldTimer'));
    });
    
    //Attach it to the element
    $('#HoldListener').data('clickHoldTimeout', 2000); //Time to hold
    $('#HoldListener').on('clickHold', function() {
        console.log('Worked!');
    });
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <img src="http://lorempixel.com/400/200/" id="HoldListener">

    See on JSFiddle

    clickHold 元素上的事件

        8
  •  0
  •   KR Vineeth    8 年前

    试试这个:

    var thumbnailHold;
    
        $(".image_thumb").mousedown(function() {
            thumbnailHold = setTimeout(function(){
                 checkboxOn(); // Your action Here
    
             } , 1000);
         return false;
    });
    
    $(".image_thumb").mouseup(function() {
        clearTimeout(thumbnailHold);
    });