Jump to content
uomopalese

Toggle opacity by clicking on button

Recommended Posts

Hi,

I'm really new to all this and this is my second post.

I'm trying to do something I thought was simple, but something does not work.

I have two div (#red and #blue) and two buttons (#opacity1 and #opacity2), clicking the first button will increase the opacity of the first div while decreasing the opacity of the second div and vice versa.

The code I'm using doesn't seems to work, can someone help?

Thanks

 

UPDATE:

There's an error in the code, sorry!!!

.to("#blue", 0.1, { opcity: "+=0.1" }, "0");

should be

.to("#blue", 0.1, { opacity: "+=0.1" }, "0");

(opacity instead of opcity) and now seems to works.

Anyway, am I doing this correctly? or there is a better way?

Thanks.

$('#opacity1').click(function() {
  var tl1 = new TimelineMax();
  
  tl1.to("#red", 0.1, {opacity: "-=0.1" }, "0")
  .to("#blue", 0.1, { opacity: "+=0.1" }, "0");
});
$('#opacity2').click(function() {

  var tl2 = new TimelineMax();

  tl2.to("#red", 0.1, {opacity: "+=0.1" }, "0")
  .to("#blue", 0.1, { opacity: "-=0.1" }, "0");
});

See the Pen VLKjzL?editors=011 by uomopalese (@uomopalese) on CodePen

Share this post


Link to post
Share on other sites

Hi uomopalese  :)

 

try this :

 

var Op = 5 ;
$('#opacity1').click(function() {
  if(Op<10){ Op +=1;
    TweenLite.to("#red", 0.1, {opacity: "+=0.1" });    TweenLite.to("#blue", 0.1, { opacity: "-=0.1" });
  }
});

$('#opacity2').click(function() {
  if(Op>0){ Op -=1;
    TweenLite.to("#red", 0.1, {opacity: "-=0.1" });    TweenLite.to("#blue", 0.1, { opacity: "+=0.1" });
  }
});
  • Like 1

Share this post


Link to post
Share on other sites

Hi Diaco.AW :)

thanks for your answer, this forum is unbelievable!

I tried your code and it works fine, but if I increase the duration of the tween, eg:

TweenLite.to("#red", 0.5, {opacity: "+=0.1" });

the behaviour changes, the divs doesn't desapear completely (if i click quickly).

Is there a reason for this? I wouuld like to add some easing effects on the clicks (i updated the codepen with your code).

Thanks a lot!

Share this post


Link to post
Share on other sites

HI,

thanks again, it works even if it's  more complicated than i thought, i guess i need to improve my knowledge both on JS and TweenMax.

Thanks a lot for your time, i really appreciate your kindness.

 

gio

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.