Identify a long click with jquery

Asked

Viewed 232 times

2

Galera I set up a context menu that works like this, when the user right-click on a 'tr' table it opens.

I do so using jQuery:

// Verifica se abre o menu
$("tr").mousedown(function (e) {
  //AQUI FICA AS FUNÇÕES DO MENU
});

Well, the problem is, when it’s opened on a cell phone, you don’t have the mouse.

I was wondering if you have any way to identify a click for 2 seconds, and open the menu.

Then it should open either with the right button or with a long click.

1 answer

3


Or you can use the event taphold jQuery Mobile API:

The jQuery Mobile taphold event kicks off after a ringtone event complete and sustained (also known as long pressure).

In your case I’d be more or less:

$("tr").on( "taphold", function( event ) {
  //AQUI FICA AS FUNÇÕES DO MENU
});

Only with jQuery, it is possible to calculate the time between the mousedown and mouseup:

var tempo;

$("div").mouseup(function(){
  
  // diferença entre clicar e soltar
  var total = new Date().getTime() - tempo;
  var segundos = ((total % 60000) / 1000).toFixed(0);
  console.log(total); // total se millis
  console.log(segundos); // total em segundos
  
  if (segundos > 0){
    console.log("Oi");
  }
  
}).mousedown(function(){
  
  // set tempo ao clicar
  tempo = new Date().getTime();
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Teste
</div>

  • A lot with, that’s just what I needed, but how I’ll call the function '$("tr"). on( "taphold", Function( Event ) {' when the second is greater than 2?

  • Precisely, it deals alone this @Hugoborges. So I edited putting time second only with jQuery :)

  • I’m trying to do this check, 'if (seconds > 0) {console.log("hi");}' but it doesn’t work, you know why?

  • I needed to reverse the operators to not go negative @Hugoborges. I edited the answer :)

  • I’m not getting the menu open with a long click (and you have more than 1 second), can you help me? follow my code https://jsfiddle.net/hugoborges/50bh9t1a/2/

  • @Hugoborges, I noticed he’s entering the condition normally, but nothing happens on the screen. Since it’s another matter, I suggest you open up another question if the problem is with the menu, so as not to branch out your question.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.