2017-02-01 6 views
0

私はカーソル(ハードウェア)としてリモコンペンを持っています。xとyの向きでクリックを送信するには?

SDKからJavascript経由の外部ハードウェアカーソルを受信しました。 ここでは、x、x、y、yの位置とクリックコマンドを取得します。

しかし、その位置をクリックして送信する方法は? (関係なく要素がその位置に存在しているものは何でも、私はその位置にクリックを提出する必要はありません)

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

なぜボタンを作成する必要がありますか?送信イベントをトリガーできませんか? –

+1

jQueryを使用することはできますか? –

+2

[特定の座標でのJavaScriptクリック()イベントのトリガー]の可能な複製(http://stackoverflow.com/questions/2845178/triggering-a-javascript-click-event-at-specific-coordinates) – Andrey

答えて

1

方法1: CURSOR_CLICKイベントがありますすべてのクリック可能なボタンを反復し、どのボタンにクリックが「関連」しているかをチェックします。

$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(); 
      } 
    }); 
} 

方法2:すべてのページ上のボタンとそのコードを持つ配列を作成します。 クリックイベントがトリガーされているときは、コードを比較するだけです。 利点 - ループに費やされる時間が短縮されます。

+1

すべきではありません違いは、その.cickable-buttonクラスを追加することを覚えておいてください(または名前を変更してください) –

+1

動作するかどうかを確認してください。 –

+1

私のソリューションはあなたのために働いていますか? –

関連する問題