2017-02-01 2 views
0

J'ai une télécommande comme curseur (matériel).Comment soumettre un clic via l'orientation x et y?

Matériel externe via SDK à Javascript je reçois un curseur. Où j'obtiens x, x, y, y position et cliquez sur commande.

Mais, Comment soumettre cliquez sur cette position? (peu importe ce que l'élément est là cette position, je dois soumettre le clic sur cette position)

if (_label == rs.cursor.GestureType.CURSOR_CLICK) { 
        // I have: x, x, y, y     
/* 
Following method works: 
but i have too many buttons, how to apply dynamic click without tracking which element? 

        var myButton = document.getElementById("mediaplayer"); 
        var rect = myButton.getBoundingClientRect(); 
        if (x >= rect.left && 
         x <= rect.right && 
         y >= rect.top && 
         y <= rect.bottom){ 
        myButton.click(); 
        } 
*/ 
} 

EDIT:

 // 
     // 1 - Find Active DIV/MAP/Container?   
     // 
     var layerID = $('#layoutmain').attr('usemap');    
     if (_label == rs.cursor.GestureType.CURSOR_CLICK) {  

      // 
      // 2 - Loop each buttons of that container 
      // 
      $('#' + layerID).find('area').each(function(){     
      var onclick_function = $(this).attr('onclick') ; 
      var coords   = $(this).attr('coords'); 
       coords   = coords.split(' ').join(''); 
      var coords_array  = coords.split(','); 
      console.log('>>> we have: ', 
         onclick_function , 
         coords, 
         coords_array);        
      var rectleft = coords_array[0]; 
      var recttop = coords_array[1]; 
      var rectright = coords_array[2]; 
      var rectbottom = coords_array[3]; 

      // 
      // 3 - Match the RANGE of buttons with the coordinates 
      // 
      if (x >= rectleft && y >= recttop && 
       x <= rectright && y <= rectbottom){     
       $(this).trigger('click'); 
      } 
      });     
     } 
+1

Pourquoi devez-vous créer un bouton? Ne pouvez-vous pas déclencher un événement de soumission? –

+1

Et vous pouvez utiliser jQuery, non? –

+2

Copie possible de [Déclenchement d'un clic JavaScript() sur des coordonnées spécifiques] (http://stackoverflow.com/questions/2845178/triggering-a-javascript-click-event-at-specific-coordinates) – Andrey

Répondre

1

Méthode 1: Lorsque l'événement CURSOR_CLICK est déclenché, parcourez tous les boutons cliquables et vérifiez à quel bouton le clic est "lié".

$buttons = $('.clickable-button'); 
var rect; 

if (_label == rs.cursor.GestureType.CURSOR_CLICK) { 

    $buttons.each(function(){ 

     rect = $(this).getBoundingClientRect(); 
     if (x >= rect.left && 
      x <= rect.right && 
      y >= rect.top && 
      y <= rect.bottom){ 
         $(this).click(); 
      } 
    }); 
} 

Méthode 2: Créer un tableau avec tous les boutons de la page et leurs cordes. Lorsque l'événement click est déclenché, comparez simplement les cordons. Avantage - Moins de temps est passé dans la boucle.

+1

Ne pas effectuer une différence, rappelez-vous juste d'ajouter cette classe .cickable-button (ou renommez-le comme vous le souhaitez) –

+1

Mettez-moi juste à jour si ça marche/pas. –

+1

Ma solution fonctionne-t-elle pour vous? –