Reputation: 311
I would like to put a jQuery function which contain php in my main.js page. How can I do ?
<script type="text/javascript">
var r = parseInt('<?php the_field('red'); ?>');
var g = parseInt('<?php the_field('green'); ?>');
var b = parseInt('<?php the_field('blue'); ?>');
$("body").css('background-color', 'rgb(' + r + ',' + g + ',' + b + ')');
var alpha = Math.min($(this).scrollTop() / 2000, 1);
var r = Math.round((230-parseInt('<?php the_field('red'); ?>')) * alpha + parseInt('<?php the_field('red'); ?>'));
/* red : (fin - début + début) */
var g = Math.round((230-parseInt('<?php the_field('green'); ?>')) * alpha + parseInt('<?php the_field('green'); ?>'));
/* red : (fin - début + début) */
var b = Math.round((230-parseInt('<?php the_field('blue'); ?>')) * alpha + parseInt('<?php the_field('blue'); ?>'));
$(document).scroll(function() {
/* red : (fin - début + début) */
$("body").css('background-color', 'rgb(' + r + ',' + g + ',' + b + ')');
});
</script>
Upvotes: 0
Views: 46
Reputation: 3310
You can't. PHP is processed in the backend by the PHP server, while JS is processed in the frontend by the browser (JS can be processed in the backend also with NodeJS, but that's a different topic)
Now, to accomplish what you are trying to do, create a <script>
tag in your header before you load main.js, in which you add a global var that contains the colors:
<script type="text/javascript">
var _COLORS_ = {
r: parseInt('<?php the_field('red'); ?>'),
g: parseInt('<?php the_field('green'); ?>'),
b: parseInt('<?php the_field('blue'); ?>')
};
</script>
Then, in your main.js file, read from that variable: _COLORS.r
or _COLORS.g
, or _COLORS.b
.
Upvotes: 1