Problem with Hover event in Firefox

Asked

Viewed 135 times

3

The following code is to use the event Hover in the first image and change the class of the second. But after testing, it did not work in the Firefox browser, working only in Chrome, Safari and Opera browsers.

Below is an example of the code:

HTML

<img id="work1" src="/images/summer.jpg" alt="" width= /><a href="#um"></a>
<img id="work2" src="/images/winter.jpg" alt="" width= /><a href="#um"></a>

CSS

 #work1{
   cursor:pointer;
 }

 #work2{
   cursor:pointer;
 }

 #work2.tone{
  content:url(/images/click.svg);
 }

JAVASCRIPT

$('#work1').hover(function(){
  $("#work2").toggleClass("tone");
});

What would be the solutions to this malfunction?

  • Before a doubt: why use javascript to do Hover, if you can do this using css?

  • I couldn’t find a solution where I did Hover in image 1 (#work1) and change image 2 (#work2) in CSS. I’m open to suggestions. Thank you for the reply.

  • @Ivanferrer, could have used the image as the background of a div instead of an img tag

  • Hello @Tobymosque, there are several ways to do, the best would surely be using a background, however, I am presenting a simple solution, another example to solve would be doing this: .attr('style','background:url(/images/click.svg)')... but the important thing is to work, right.

  • @Ivanferrer, I added an answer with an example using background-image.

3 answers

1


Regarding the bug:

It may be something from the Jquery library itself, something from the browser, or something in your code that caused the error, there are many possibilities and to find the solution of this code you will only be able to debug it.


Regarding the solutions:

SOLUTION I:

I advise to do this effect using CSS, besides being the function of it to do this type of interaction, you avoid doing two manipulations directly in the DOM with Jquery, saving memory.

In this case you will use a selector CSS3 that assigns interaction to the element immediately proceeding to it represented by the sum signal (+).

Example:

/*Style default*/
img {
  border: 1px solid black;
  height: 100px;
  width: 100px;
}


/* Seletor e exemplo de interação */
#work1:hover + a + #work2 {
  border-color: red;
}
<img id="work1" src="/images/summer.jpg" alt="Imagem I"><a href="#um"></a>
<img id="work2" src="/images/winter.jpg" alt="Imagem II"><a href="#um"></a>

Explanation:

I copied your HTML and with CSS assigns the event from Hover at the #work1 then I passed the path until I reached the #work2, adding the selector + between each element.


SOLUTION II:

If you choose to do so using derivatives from javascript, I suggest you use the following code:

document.querySelector('#work1').addEventListener('mouseover', function(){
  document.querySelector('#work2').classList.add('tone');
});
document.querySelector('#work1').addEventListener('mouseout', function(){
  document.querySelector('#work2').classList.remove('tone');
});
img {
 border: 1px solid black;
 height: 100px;
 width: 100px;
}
img.tone {
  border-color: red;
}
<img id="work1" src="/images/summer.jpg" alt="Imagem I"><a href="#um"></a>
<img id="work2" src="/images/winter.jpg" alt="Imagem II"><a href="#um"></a>

Explanation:

I select the elements using the querySelector, where the former hosts the mouseover and the second to classList, in this example it serves as the native equivalent of toggleClass but the two have a small difference.


Remarks:

  • If you choose to do it with CSS, try using the selector for directly followed elements, i.e., removing the element <a> between them to avoid autos levels of specificity of selectors.
  • For the element <img> remember to add value to attribute alt the same is important for SEO and accessibility and at the end you can remove the bar (/) before closing the tag, if you are using HTML5.
  • In the case of javascript, you can change directly the value of src, without having to use the "tone" class, replacing the classList for .src="image path". Example: document.querySelector('#work2').src="/images/click.svg";
  • Thank you so much for your help

  • Thanks for the feedback @Ostinha, any questions, you may ask!

  • They advised to use the image as BG, advised to use CSS 3, etc... but would have to help him with what he had. And if he has to support IE8-??

  • @Danylosantoro, as you are new here, may not know that when the answer is marked as correct it is because it helped the user. What was requested in the question, was helped, besides I did not find anything in the question that speaks in relation to support to IE8 and as far as I know, using what the user already has is not a mandatory goal is to propose solutions, but if you can prove me the opposite thank you, because I’d like to know more about it, too. Now, if you found the answer unsatisfying, feel free to devise a new.

  • I appreciate all your time and all your words. By the way, the images I have to exchange with Hover are several elements ahead and some behind the first element. Is there any solution for this through other selectors or we only have the selector (+) ?

  • @Costinha, you tried the solution in Javascript? it does not depend on the positioning of the elements =]

  • Yes and it worked. I was just checking to see if there was a better css solution to save memory. Thanks again!

  • In this case there is no way, you will need to access the two elements via DOM same! hehe

Show 3 more comments

1

Follow a suggestion using CSS only, in this case use a div instead of a img, in this case it will be important to inform the image size as well as the url of the same right in the CSS.

.img {
  width: 256px;
  height: 256px;
  background-color: whitesmoke;
  background-repeat: no-repeat;
  border: 1px solid gainsboro; 
  border-radius: 10px;

  transition-property: background-image;
  transition-duration: 0.5s;
  transition-timing-function: linear;
}

.img {    
  background-image: url('http://cdn.flaticon.com/png/256/97199.png');

}

.img:hover {  
  background-image: url('http://cdn.flaticon.com/png/256/63511.png');
}
<div class="img"></div>

0

  • Thanks works, but when removing the mouse the image does not return to the original, something that did previously, except in firefox clear.

  • You want me to come back.

  • Thanks for the help

Browser other questions tagged

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