Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1357 connectés 

  FORUM HardWare.fr
  Programmation
  HTML/CSS

  [JS] Trouver la position du curseur sur une image

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[JS] Trouver la position du curseur sur une image

n°1618717
nero27
Posté le 04-10-2007 à 10:30:40  profilanswer
 

Bonjour à tous,
 
Je suis débutant en JS et j'ai une fonction à programmer qui me pose problème. Je dois pouvoir récupérer la position du curseur lors d'un clic sur l'image mais pas en fonction du bord haut/gauche de la page mais en fonction du bord haut/gauche de l'image : est-ce possible ?
Si oui, pourriez-vous me mettre sur la voie ?
 
Merci d'avance :jap:

mood
Publicité
Posté le 04-10-2007 à 10:30:40  profilanswer
 

n°1624623
nero27
Posté le 16-10-2007 à 12:40:43  profilanswer
 

Voici ce que j'ai fait : ça fonctionne sous IE, mais pas sous FF :/
 

Code :
  1. <script type="text/javascript">
  2. var cpt=1;
  3. var bool=false;
  4. var isIE = false;
  5. //on détermine le navigateur
  6. if (navigator.appName == "Microsoft Internet Explorer" ) {
  7.  isIE = true;
  8. }
  9. function coords()
  10. {
  11.  //On gère la croix courante
  12.  if (cpt<7)
  13.  {
  14.   bool=true;
  15.  }
  16.  else
  17.  {
  18.   bool=false;
  19.   alert("Vous avez déjà coché 7 erreurs" );
  20.  }
  21.  cpt++;
  22.  if (bool)
  23.  {
  24.   var div1=document.getElementById("croix1"+cpt);
  25.   var div2=document.getElementById("croix2"+cpt);
  26.   var A=document.getElementById("A" );
  27.   div1.style.visibility="visible";
  28.   div2.style.visibility="visible";
  29.   if (isIE)
  30.   {
  31.    div1.style.left=window.event.offsetX-5;
  32.    div1.style.top=window.event.offsetY-5;
  33.    div2.style.left=window.event.offsetX-5;
  34.    div2.style.top=window.event.offsetY-5;
  35.   }
  36.   else
  37.   {
  38.    alert(A.offsetTop);
  39.    /*div1.style.left= A.offsetLeft - 5;
  40.    div1.style.top= A.offsetTop - 5;
  41.    div2.style.left= A.offsetLeft - 5;
  42.    div2.style.top= A.offsetTop - 5;*/
  43.   }
  44.  }
  45. }
  46. </script>
  47. <div style="position: relative; width: 300px; height:300px;">
  48. <img src="img/1A.jpg" style="position: absolute; left:0; top:0;" onclick="coords(this)" id="A"/>
  49. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix11" />
  50. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix12" />
  51. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix13" />
  52. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix14" />
  53. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix15" />
  54. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix16" />
  55. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix17" />
  56. </div>
  57. <div style="position: relative; width: 300px; height:300px;">
  58. <img src="img/1B.jpg" style="position: absolute; left:0; top:0;" onclick="coords(this)" id="B"/>
  59. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix21" />
  60. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix22" />
  61. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix23" />
  62. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix24" />
  63. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix25" />
  64. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix26" />
  65. <img src="img/croix.gif" style="position: absolute; left:10px; top:10px; width: 10px; height: 10px; visibility:hidden;" id="croix27" />
  66. </div>


 
PS: c'est un jeu des 7 erreurs et je voudrais que les croix se placent sur les endroits cliqués sur l'image.

n°1625116
mIRROR
Chevreuillobolchévik
Posté le 17-10-2007 à 04:08:49  profilanswer
 

les evenements ne sont pas gérés de la meme facon sur ie et ff
 
enleve le this ici
 
onclick="coords(this)"
 
dans ta fonction fous un nom pour ton objet event, du genre
function coords(evt)
 
ensuite tu ourras appeler tes evenemens dans firefox avec evt.offsetSkeTuVeux
 
je suis aps trop sur de moi par contre :D
verifie sur la doc de mozilla


---------------
« The enemy is the gramophone mind, whether or not one agrees with the record that is being played at the moment. » — George Orwell

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  HTML/CSS

  [JS] Trouver la position du curseur sur une image

 

Sujets relatifs
trouver le fichier le plus récentRecherche de composant HTML WYSIWYG + Image manager
Code de de réduction image auto en HTMLfonction(quelle image est dans mon clip)
débutant: trouver une chaîne dans une ligne, en remplacer une autrerécupérer la position d'un clip son connaitre son nom
Comment positionner un texte au dessus d'une image (CSS)Cic sur image = Texte dans formulaire barré
Ajouter image dans un fichier pdf existant[RESOLU] Probleme pour augmenter un DIV en fonction d'un autre
Plus de sujets relatifs à : [JS] Trouver la position du curseur sur une image


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR