-1

Hello i am trying to animate a div background color this way with php and javascript html.

<div background="<?=for($step = 0; $step < 256; $step++)
    echo "rgb($step, $step, $step);"; ?>" /> contents </div>

So the code will make change the div from black to white as my example clearly shows. But it is not working, any ideas?

i want to implement it in my personal website http://www.nickersonweb.com/ buttons

Cory
  • 732
  • 3
  • 7
  • 22

3 Answers3

4

Once your page is generated by PHP and sent to the client, your PHP code can no longer change the content on the client side.

That's where client-side code (Javascript) comes in.

To quickly achieve what you're trying to do, have a look at this question: jQuery animate backgroundColor , which recommends using jQuery with the jquery-color plugin. Here's a quick demo: http://jsfiddle.net/MCwxG/

p.s. I'm sure it's possible to do it with pure javascript, but my js-fu is not accomplished enough to show you how.

Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
2

You can't do it with php, it's server side.

But you can do it with the color plugin with jQuery.

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
2
  • Well, you're using <?= with a for loop statement, when it should only be used with an expression. You need to change that to <?php (or <? if your server supports it).
  • <div>s don't have a background attribute. You'd need to modify their style.
  • You're writing all of the style changes to the page before you're writing the content.
  • The browser can't even try to render the <div> before it's closed with >, and all of your styles will be interpreted at once, and only the last one will be visible.
Jeremy
  • 1
  • 85
  • 340
  • 366