I
I
Ilya Korablev2015-10-15 20:07:26
JavaScript
Ilya Korablev, 2015-10-15 20:07:26

Hold event?

How to use hammer.js (well, or in other ways) to make an event that will add a class to the element if there is a hold (like 3d touch) ? Can you give examples?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Inchin ☢, 2015-10-15
@swipeshot

I don’t know about hummer.js , but in general, like this:

function hold(e, f, t, g){
   var holder;
   e.addEventListener("mousedown", function(r){
         holder = setTimeout(function(){
               f.call(e, r);
               holder = true;
         }, t || 2000)
   });
   document.addEventListener("mouseup", function(r){
          holder === true ? g && g.call(e, r) 
            : clearTimeout(holder);
   });
}

hold(
  document.body,  //Целевой элемент
  function(){  //Функция, выполняющаяся при удержании (единожды)
    this.classList.add("active");  
  }, 
  3000,  //Время, через которое сработает «Удержание»
  function(){  //Функция, которая сработает после удержания
     this.classList.remove("active");
  }
);

Usage example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question