3

I'm getting my head around JS/Jquery slowly and I need help/guidance on how to detect when a "specific" image is "visible", so I can then remove a class on a different element. Simple I'm sure...just trying to understand the logic.

JS..

$('.my-big-hero').find('my-image-here.jpg:visible').removeClass('class-i-want-removed');

This is probably a simple (and wrong way) to display this, but it was to give you an idea of what I'm trying to achieve. I've tried various ways to write this and not winning.

Appreciate any help you can give....thank you in advanced.

ambtlv
  • 45
  • 1
  • 5
  • 1
    `if($('.my-big-hero').find('my-image-here.jpg').is(":visible")){ $(this).removeClass('class-i-want-removed'); }` – Alive to die - Anant May 26 '17 at 08:40
  • 1
    Here is the answer https://stackoverflow.com/questions/8613844/how-to-check-image-is-visible-or-not – Constantine May 26 '17 at 08:42
  • 1
    I like this question - you're likely to get a few answers that all do the same thing. I'll give my own shortly :-) – David Wilkinson May 26 '17 at 08:54
  • ...everyone who has replied so far...thank you!!! I'm going through the different solutions now to see which one best suits my logic. Brilliant...thank you all – ambtlv May 26 '17 at 08:58

2 Answers2

1

You can use the .is("visible") and the toggleClass functions, this way:

$('.my-big-hero').find('my-image-here.jpg').toggleClass('class-i-want-removed', !$(this).is(':visible'));

Then the element will have the class class-i-want-removed if it is not visible, and the class will be removed once it is visible.

amhev
  • 313
  • 1
  • 3
  • 12
  • ..thank you @amhev. It's not quite working the way I want to but just seeing your logic is helping me understand jquery a little more....will keep trying to see if I can use your logic and the others I get a response from :-) – ambtlv May 26 '17 at 08:57
0

Using JQuery, you might be able to do something like this?:

$(".my-big-hero").find("my.image.here.jpg").on("load", function() {
   $(this).removeClass("class-i-want-removed");
});
Xariez
  • 759
  • 7
  • 24