Create Multiple Events using Variables to Specify Event for Function in Jquery

Asked

Viewed 59 times

1

I need to conduct tests for events Touch and Mouse in Jquery, and focused on reducing the cost of performing and trying to simplify the code to stay visible, simple and fast to handle choices, follow the code:

jQuery(document).on({
  'mousedown': function() {
    alert("Mouse Click");
  },
  'touchstart': function() {
    alert("Touch Click");
  }
}, ".element");
div {
  width: 150px;
  height: 150px;
  background-color: #00bcd4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<div class="element">Clica Aqui</div>

In this example all right works but I repeat all code within the mousedown to the touchstart I find it unnecessary. I’ve seen some codes doing so:

jQuery(document).on({
  'mousedown touchstart': function(event) {
    console.log(event.type);
  }
}, ".element");
div {
  width: 150px;
  height: 150px;
  background-color: #00bcd4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<div class="element">Clica Aqui</div>

It works but if I hold too much in the Event Touch he shows the mousedown. Then I saw some codes to test that who clicks is a device with touch or not:

// Detect Mobile
var mobile = /Windows Phone|Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent.toLowerCase());

var events = {
  startClick: (mobile ? "touchstart" : "mousedown"),
  moveClick: (mobile ? "touchmove" : "mousemove"),
  stopClick: (mobile ? "touchend" : "mouseup")
};

$(document).on(events.startClick, '.element', function(event) {
  console.log(event.type);
});
div {
  width: 150px;
  height: 150px;
  background-color: #00bcd4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<div class="element">Clica Aqui</div>

This solution looks great and helps but want for multiple events with Device checking but when I do it does not perform. See the code:

// Detect Mobile
var mobile = /Windows Phone|Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent.toLowerCase());

var events = {
  startClick: (mobile ? "touchstart" : "mousedown"),
  moveClick: (mobile ? "touchmove" : "mousemove"),
  stopClick: (mobile ? "touchend" : "mouseup")
};

jQuery(document).on({
  'events.startClick': function(event) {
    alert("Clicou: " + event.type);
  },
  'events.stopClick': function(event) {
    alert("Saltou: " + event.type);
  }
}, ".element");
div {
  width: 150px;
  height: 150px;
  background-color: #00bcd4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<div class="element">Clica Aqui</div>

My problem is this, I do not want to create many functions to solve problem of not repeating the code, simply already at the beginning I can indicate the Event that I need help a lot. I use the Jshint to clean the code well and it gives attention not to use much to the functions for the sake of memory use.

  • The events of "down" and "up" are not very problematic as they are called a few times. "move" are more important to optimize. You can give an example of the tests you want to do?

  • well the intention is to create an effect Ripple from Google, thank you for @Tobymosque for helping me on Decrease the Elements feature in the DOM applied in the Jquery that generates Ripple and modified the code but I need these events: mousedown touchstart and mouseup touchend the mousemove touchmove works but conflict when I hold the touch on the button.

1 answer

0

If you do not want to write the same function several times, then create before:

function callback(e) {
    alert(e.type);
    /*[....]*/
}

$(document).on({
    click: callback,
    keydown: callback,
    touch: callback
});
  • I liked it but it does not address the issue of holding the button on touch because if I hold it he carries the mousedown and with this solution check the Device you have Touch he can fix, I did a test on jsfiddle.net.

Browser other questions tagged

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