1

I figured out how to change my background color with Jquery upon clicking a button, but I don't know how to ease the transition. I have tried the .css method in order to add the transition. It doesn't work.

Here is my Jquery to add the new color to the background, it works:

$("#playbtn").click(function(){
  $("#container").css("background", "#3953A0");
  $("#container").css("background", "-webkit-linear-gradient(to left, #3953A0, #2A3C70)");
  $("#container").css("background", "linear-gradient(to left, #3953A0, #2A3C70)");
  $("#container").css("background", "-moz-linear-gradient(to left, #3953A0, #2A3C70)");
  $("#container").css("filter", "progid:DXImageTransform.Microsoft.gradient( startColorstr='#3953A0', endColorstr='#2A3C70',GradientType=1 )");
}); 

my basic #container css:

#container {
  width: 100%;
  min-height: 100%;
  position: fixed;
  background: #00C9FF; /* fallback for old browsers */
  background: -webkit-linear-gradient(to left, #2A3C70 , #718DE2); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to left, #2A3C70 , #718DE2); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ 
  background: -moz-linear-gradient(left, #2A3C70 , #718DE2); /* For Firefox 3.6 to 15 */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2A3C70', endColorstr='#718DE2',GradientType=1 ); /* IE6-9 */
}

Hope you guys can help me!

imtheman
  • 4,713
  • 1
  • 30
  • 30
Tripduc
  • 31
  • 1
  • 11

2 Answers2

0

Pretty sure this is a duplicate of this: jQuery animate backgroundColor, but I can't mark the question.

Basically, you're wanting .animate()

Community
  • 1
  • 1
Charles
  • 640
  • 5
  • 21
0

To transition CSS, you will need the transition property on the element. I recommend using CSS to handle all the changes via classes, and use jQuery to toggle the css class. This is easier to work with and gives you more control!

CSS:

#container {
  width: 100%;
  min-height: 100%;
  position: fixed;
  background: #00C9FF; /* fallback for old browsers */
  background: -webkit-linear-gradient(to left, #2A3C70 , #718DE2); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to left, #2A3C70 , #718DE2); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  background: -moz-linear-gradient(left, #2A3C70 , #718DE2); /* For Firefox 3.6 to 15 */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2A3C70', endColorstr='#718DE2',GradientType=1 ); /* IE6-9 */

  /*
  Play with transition settings (time, ease type)
  */
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -ms-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

#container.is-active {
  background: #3953A0; /* fallback for old browsers */
  background: -webkit-linear-gradient(to left, #3953A0, #2A3C70); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to left, #3953A0, #2A3C70); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  background: -moz-linear-gradient(to left, #3953A0, #2A3C70); /* For Firefox 3.6 to 15 */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#3953A0', endColorstr='#2A3C70',GradientType=1 ); /* IE6-9 */
}

JS:

$("#playbtn").click(function(){
  $("#container").addClass("is-active");
});
hellojebus
  • 3,267
  • 1
  • 21
  • 23