This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

CSS opacity control onclick
#1
Hello,

We have buttons that we want to change opacity to 0 onclick and after 3s they go back to opacity 1 (with fade or some animation).

We want to do it only with CSS. Without using javascript.

Is this posible?

Thanks!
Reply
#2
It's not possible to do this without JavaScript. Animation will work only until the mouse button is released. So only very short animations can be done this way.

JavaScript:
Code:
$(function() {
  $('body').on('click touchstart', '.item-control', function() {
    this.classList.remove('active');
    this.offsetHeight;
    this.classList.add('active');
  });
});

CSS:
Code:
.item-control.active {
  animation: clickanim 3s ease-out;
}

@keyframes clickanim {
  0% {
    opacity: 1;
  }
  10% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
Reply
#3
(03.01.2022, 06:20)admin Wrote: It's not possible to do this without JavaScript. Animation will work only until the mouse button is released. So only very short animations can be done this way.

JavaScript:
Code:
$(function() {
  $('body').on('click', '.item-control', function() {
    this.classList.remove('active');
    this.offsetHeight;
    this.classList.add('active');
  });
});

CSS:
Code:
.item-control.active {
  animation: clickanim 3s ease-out;
}

@keyframes clickanim {
  0% {
    opacity: 1;
  }
  10% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

Hello admin!

Thanks for the reply.
It works on my laptop but not working with Smartphone. Tried on my Iphone and with Android.

Any suggestion?

Thanks!
Reply
#4
Replace click with click touchstart so it handles both mouse and touch events.
Reply
#5
(03.01.2022, 10:51)admin Wrote: Replace click with click touchstart so it handles both mouse and touch events.

Okai, now it works on both systems but not as we want.

When we click a button it does the animation but if we click another, the animation occurs on this button and the previous button.

If we click a third button the animation occurs on the 3 buttons.

Suggestion? Thanks admin!
Reply
#6
If you want to cancel previous animations then replace Custom JavaScript with this:
Code:
$(function() {
  $('body').on('click touchstart', '.item-control', function() {
    $('.item-control.active').removeClass('active');
    this.offsetHeight;
    this.classList.add('active');
  });
});
Reply
#7
(03.01.2022, 11:42)admin Wrote: If you want to cancel previous animations then replace Custom JavaScript with this:
Code:
$(function() {
  $('body').on('click touchstart', '.item-control', function() {
    $('.item-control.active').removeClass('active');
    this.offsetHeight;
    this.classList.add('active');
  });
});

Perfect admin!

Work like a charm!

Thanks.
Reply


Forum Jump: