Using jquery to refresh several backgrounds at once

While I have some knowledge of CSS and various JavaScript libraries, I've encountered difficulty in integrating multiple backgrounds into the code that I found on this link: http://codepen.io/quasimondo/pen/lDdrF. My goal was to overlay a transparent .png onto the animated gradient.

I attempted attaching the png to different sections of the website (body, div), but it did not work as expected, resulting in loss of control. Whenever I try adding to the $('#gradient').css function, an error occurs. It seems like a simple syntax issue, but I'm unsure.

Thank you for taking the time to look at my post. This is my first one, and I did browse through previous posts. Apologies if I missed something.

- Jason

HTML

<!DOCTYPE html>
<title>Animated Background Gradient</title>
<body>
<div id="gradient" />
</body>   
<script src="js/index.js"></script>

CSS

body{
background-color: #000000;
padding: 0px;
margin: 0px;
}

#gradient {
width: 100%;
height: 800px;
padding: 0px;
margin: 0px;}

JS

var colors = new Array(
[62,35,255],[60,255,60],[255,35,98],[45,175,230],[255,0,255],[255,128,0]);

var step = 0;
var colorIndices = [0,1,2,3];

var gradientSpeed = 0.0006;

function updateGradient()
{

if ( $===undefined ) return;

var c0_0 = colors[colorIndices[0]];
var c0_1 = colors[colorIndices[1]];
var c1_0 = colors[colorIndices[2]];
var c1_1 = colors[colorIndices[3]];

var istep = 1 - step;
var r1 = Math.round(istep * c0_0[0] + step * c0_1[0]);
var g1 = Math.round(istep * c0_0[1] + step * c0_1[1]);
var b1 = Math.round(istep * c0_0[2] + step * c0_1[2]);
var color1 = "rgb("+r1+","+g1+","+b1+")";

var r2 = Math.round(istep * c1_0[0] + step * c1_1[0]);
var g2 = Math.round(istep * c1_0[1] + step * c1_1[1]);
var b2 = Math.round(istep * c1_0[2] + step * c1_1[2]);
var color2 = "rgb("+r2+","+g2+","+b2+")";

$('#gradient').css({
background: "-webkit-gradient(linear, left top, right top, from("+color1+"), to("+color2+")"}).css({
background: "-moz-linear-gradient(left, "+color1+" 0%, "+color2+" 100%)"});

step += gradientSpeed;
if ( step >= 1 )
{
step %= 1;
colorIndices[0] = colorIndices[1];
colorIndices[2] = colorIndices[3];

colorIndices[1] = ( colorIndices[1] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;
colorIndices[3] = ( colorIndices[3] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;

}
} 

setInterval(updateGradient,10);

https://jsfiddle.net/byjrglass/f7vsLss1/7/

Answer №1

It's important to remember that CSS Gradients work similarly to images. For instance, the code snippet below:

background: linear-gradient(to bottom, blue, white);

achieves the same result as this:

background-image: linear-gradient(to bottom, blue, white);

To layer a PNG image on top of the gradient, you would need to use a syntax like this:

background-image: url(image.png), linear-gradient(to bottom, blue, white)

An illustration using your code can be found here: http://codepen.io/anon/pen/WrJOQL

Alternatively, you could apply the gradient at the body level and place the image within the div. Here's an example utilizing your code: http://codepen.io/anon/pen/gPzWVo

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Progressing through the Material UI LinearProgress bar in steps

How can I split a progress bar into more steps to achieve a design like this? https://i.stack.imgur.com/lRMj6.png I attempted using this code but couldn't find an option for splitting: <LinearProgress variant="determinate" classes={{ ...

extracting data from a div while presenting a confirmation pop-up

I am attempting to extract information from a file on the following website. Specifically, I am trying to download an Excel sheet containing a complete directory of towns in TRIPURA, listed as the first item in grid format. Here is the code snippet I am ...

What is the best way to center text within an input field?

I've been attempting to center the text in the span tag within the input field, but using "text-align": center in my css doesn't seem to be working as expected. Interestingly, when I switched the span tag to a paragraph tag, it caused the input ...

Disable the scroll animation feature for certain IDs

I implemented JavaScript code to animate scrolling for each block with a specific ID. However, when I added Bootstrap's tabs, the animation interfered with the functionality of the tabs. Is there a way to disable the scroll animation specifically for ...

In the readmore.js script, position the "readmore" link within a div instead of outside of it

I have added annotations that vary in length, so I am looking to integrate the readmore.js plugin. To ensure uniform sizing for all annotations (even empty ones), I need to set a minimum height for the div container. <annotation> <div style="wi ...

Tips for locating the initial <body .....> tag within a text using PHP?

Struggling with the preg_match function, especially when it comes to finding the first body tag. I've attempted to locate the tag in various formats: <body class="blah"> <body style="blah: blahblah;"> <body> Successfully found the ...

When the screen width is halved, Bootstrap pushes my content downwards

<div class="container"> <img style="float:left" src="assets/images/logo.png" alt="university logo"> <h1 style="float:left;color:white;padding-top:20px;text-align:center;margin-left:10%"> Welcome To SURE Model</h1> <a ...

Has anyone come across an XSLT that can effectively convert Apple Pages files into high-quality HTML?

When Apple’s word processor/DTP app Pages saves its files, it does so as zipped folders with an XML file containing the content and attachments like images stored as separate files. I am interested in converting this content into HTML format, using < ...

Custom sized box causing Bootstrap Navbar to be unresponsive

I am having trouble loading the Bootstrap 3 navbar inside a .box class with specific CSS rules: .box { height: 600px; width: 320px; position: relative; border: 2px solid #eee; } Unfortunately, it is not displaying re ...

Learn how to gradually make text disappear and reappear using JavaScript or jQuery

I am a beginner in JavaScript and jQuery, and I am trying to achieve a text fade-out effect. Once the text has completely faded out, I want it to return with the same fade-in effect. I have been exploring if statements and fadeIn effects, but I am strugg ...

Using HTML: The trick to obtaining selection offsets in relation to HTML tags

Let's say I have HTML code like this: <div> <p> start<span>span</span>end </p> </div> I am looking for a way to retrieve the offsets when text is selected, while still taking into account the presence of span ...

What causes a fixed position div to extend beyond the boundaries of the main container?

Below is the CSS for the fixed div: .top-container { position: fixed; width: 100%; z-index: 999; } Upon zooming out the screen, the div causes the main container to break on the right side while the left side remains unchanged. Refer to the screenshot be ...

Utilizing jQuery to emphasize a selected table row upon radio button selection

When I have a table (standard markup) with a radio select in each row, I want to highlight the selected radio button. It seems simple, but for some reason it's not triggering as expected. Here is the example of the markup: The Table Row: <tr> ...

Adding an Ajax response to a div in HTML: A step-by-step guide

How can I add Ajax response to a div in my HTML code? Below is my ajax script: $(document).ready(function(){ $('.credit,.debit').change(function(){ var value=$(this).val(); $.ajax({ type:"POST", url:" ...

What is the best method for determining the ID or class of a div element dynamically?

My issue involves a scrolling div that dynamically loads data using ajax and json from an API as you scroll horizontally. Currently, I have multiple scrolling divs on a single page. The problem arises when I need to inform jQuery about the ID of the div b ...

ways to set a background color for a textarea element using bootstrap

How can I add a background color to my text area field in Bootstrap? <div class="form-group"> <div class="col-xs-12"> <textarea class="form-control" id="exampleTextarea" rows="6" placeholder="Message" style="background-color: #f ...

Is there a way to modify my function expression without using any state variables?

Currently working on a jQuery game where the player is generated upon clicking a button. Below is the function expression I'm using to store the player's information: var getPlayerData = function() { return { name: $("#name").val(),/ ...

Accessing and displaying a randomly selected PDF file from a directory using HTML coding

Welcome to my recipe collection website where I document all the delicious recipes I love to eat. One feature I would like to add is a 'random' button, so that a recipe will be selected at random for me without having to make a decision. Althou ...

What causes the change in background when I apply CSS to a div element?

Can anyone explain why the background changes when I style the div element? Here is the code I am using. When I remove the CSS related to the div, the background instantly reverts back to its original state. Any insights would be greatly appreciated. Than ...

What are the best situations to utilize bootstrap and when should you avoid it?

When it comes to web development, there is often a debate on whether to use Bootstrap or custom CSS for designing a website. For my school project, I am tasked with creating a homepage and contact page using Bootstrap primarily. However, we are also requir ...