2

I have several linked photos that are set to be (opacity: 0.45) except on hover and when clicked (opacity:1). I also have a JQuery function listening for clicks, which will then change the css of the clicked photo to be (opacity:1) and the rest back to (opacity: 0.45). However once the function runs the hover effect is disabled somehow. Any ideas?

HTML:

<a class="img_thumbs"><img src="someimage.jpg"></a>

CSS:

.img_thumbs:hover {
      opacity:1;
}

.img_thumbs {
      opacity:0.45;
}

JQuery:

$('.img_thumbs').click(function(){
    event.preventDefault();
    $('.img_thumbs').css({'opacity': '0.45'});
    $(this).css({'opacity': '1'});
}):

I have tried:

  • adding the JQuery .hover() effect in and outside of the function
  • changing the selectors, css, html to a.img_thumbs, individually and together
  • looking for where the code fails

That function also runs an AJAX call and several other listeners, but ive determined they do not have any adverse effects (commented them out), so the problem lies only in the code I've provided.

An alternate solution would also be gratefully accepted, Thanks in advance.

7
  • Possible to provide jsfiddle? Thank you. Commented May 6, 2014 at 1:57
  • jsfiddle.net/C8R7f its not working as i have it.. Commented May 6, 2014 at 2:06
  • So currently your problem is the hover opacity not working after clicking on the image? Your jsfiddle seems working though. Commented May 6, 2014 at 2:10
  • well in my actual project when i select a photo it changes the opacity to 1 and the other photos remain opaque, but then the hover effect no longer works... jsfiddle.net/C8R7f/1 Commented May 6, 2014 at 2:12
  • Wondering why are you are doing the prevent default on clicking an img? Maybe that is messing with it, could try taking it out. Commented May 6, 2014 at 2:12

1 Answer 1

2

use this:

CSS:

.img_thumbs:hover {
  opacity:1;
}
.active {
      opacity:1 !important;
}
.img_thumbs {
      opacity:0.45;
}

JS:

$('.img_thumbs').click(function(event){
    event.preventDefault();
    $('.img_thumbs').removeClass( "active" )
    $(this).addClass( "active" );
});

The tips is use other active class, and add or remove this class, when click.

Sign up to request clarification or add additional context in comments.

2 Comments

In your code the state of click element was not registred, therefore the strange behavior
"!important" overwrites the other css created by jQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.