Develop a dynamic animation using gradient and opacity properties

I'm still getting the hang of HTML, JavaScript, and CSS but I recently made some changes to someone else's code to animate a gradient. The original code used background: rgb, but I switched it to background: rgba. It seems to be working fine, but now I can't seem to get the opacity to show up in the gradient.

Does anyone know how I can add opacity to the gradient as well?

The code behaves differently here compared to Chrome, so I'm trying to figure out where the issue lies.

JS

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

var step = 0;
//color table indices for:
// current color left
// next color left
// current color right
// next color right
var colorIndices = [0,1,2,3];

//transition speed
var gradientSpeed = 0.002;

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 a1 = Math.round(istep * c0_0[3] + step * c0_1[3]);
    var color1 = "rgba("+r1+","+g1+","+b1+","+a1+")";

    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 a2 = Math.round(istep * c1_0[3] + step * c1_1[3]);
    var color2 = "rgba("+r2+","+g2+","+b2+","+a2+")";

    $('#colorstrip').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];

        //pick two new target color indices
        //do not pick the same as the current one
        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);
@import url('https://fonts.googleapis.com/css?family=Pangolin');
/*Hoofdpagina.html styles*/
#colorstrip{
    width: 100%; height: 6%;
    position: fixed;
    top: -5px;
    left:0;
    background-color: rgba(10,10,10,0.5);
    border-bottom-right-radius: 5%;
    border-bottom-left-radius: 5%;


}
#colorstrip:hover{
    background-color: rgba(207, 254, 255, 0.9);
}
body{
    background: url('images/Farm background.png');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
    background-size: cover;
    background-blend-mode: darken;
}
nav{
    width: 80%;
}
nav ul{
    text-align: center;
    margin: 0 10% 0 0;
    padding-left: 0;
}
nav ul li{
    width: 25%;
    float: left;
}
/*Styles for everything in <a>*/
nav ul li a{
    list-style-type: none;
    text-decoration: none;

    letter-spacing: 1px;
    font-size: 33px;
    opacity: 0.75;
    filter: alpha(opacity=50);
    font-family: 'Pangolin',cursive;

    color: #000000;

    -o-transition: .2s;
    -moz-transition: .2s;
    -webkit-transition: .2s;
    transition: .2s linear;
}
/*<a> hovering makes some changes*/
 nav ul li a:hover {
     color: #279afe;
     font-size: 36px;
     text-shadow: 1px 3px rgba(0,0,0,0.3);
}

ul{
    list-style-type: none;
    text-decoration: none;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://use.fontawesome.com/4f69f01663.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    <script src="Gradient.js"></script>
    <script src="gradient.css"></script>
    <link rel="stylesheet" media= "screen" href="styles.css">
    <link rel="icon" href="images/Cocky%20af.png">
    <title>Cocky Clicker</title>
</head>
<body>
    <div id="colorstrip"/>
    <nav>
        <ul>
            <li><a href="Hoofdpagina.html"><i class="fa fa-home" aria-hidden="true"></i>Mainpage!</a></li>
            <li><a href="Clickers.html"><i class="fa fa-gamepad" aria-hidden="true"></i>Play!</a></li>
            <li><a href="Information.html"><i class="fa fa-address-book" aria-hidden="true"></i>About!</a></li>
            <li><a href="Contact.html"><i class="fa fa-address-card" aria-hidden="true"></i>Contact us!</a></li>
        </ul>

    </nav>
</body>
</html>

Answer №1

When generating the RGBA values, the last parameters a2 and a1 are rounded to the nearest digit using the Math.round function. This results in the value of a2 and a1 always being 1. However, removing the rounding of a2 and a1 may yield different results when you try it.

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

Adjusting image width using jQuery

I have been experimenting with creating a Webgl hover effect for an image. The effect works well, but now I am trying to specify a width for the image within jQuery. new hoverEffect({ parent: document.querySelector('.ticket'), intensity1: 0. ...

SVG path tooltips are not functioning properly, unlike in regular HTML where they work perfectly

I have a CSS/HTML code that works perfectly in plain HTML and is also shown as an example at the end of the demo. However, it fails to work properly in SVG. Upon inspecting the element, I found that the :after code is being called but the tooltip remains ...

React Redux - There is an error during rendering as expected props have not been received yet

After retrieving data from an API and storing it in the Redux state, I utilize a helper function within mapStateToProps to filter and modify a portion of that data before passing it along as props. Although everything appears to be functioning correctly b ...

How to shuffle the elements of an array saved in a JSON file using discord.js

I've been working on a Discord bot as a way to learn more about Javascript. While creating a command that pulls random quotes from an array stored in a separate JSON file, I encountered an issue that has me stumped. var config = require("./settings.j ...

Ways to retrieve the response object from an express application

I am currently working on developing a nodejs application with mysql and my objective is to have my controllers and messages separated into different files. Below are examples of what I am aiming for: First, here is a snippet from my auth controller file: ...

Issue with LAMP Stack: HTML table updates are not syncing correctly with the database

Greetings, I am currently working on an HTML form called Index.html <html> <body> <form action="update.php" method="POST"> Department: <input type="text" name="department"><br><br> Subname: <input type="t ...

Sequencing the loading of resources in AngularJS one after the other by utilizing promises

While working in a service, my goal is to load a resource using $http, store it in a variable, load a child resource and store that as well. I understand the concept of promises for this task, but I am struggling with how to properly implement it in my cod ...

how to change the class of a div when clicking on a <p> element

Check out this code snippet: <p class="faq" onclick="toggleVisibility('answer1');"> How can I restrict email sending on my website? </p> <div id="answer1" class="hide"><sometext></div> these are my CSS styles! ...

How can you use a selector to filter Cheerio objects within an `each` loop?

As I work on parsing a basic webpage using Cheerio, I began to wonder about the possibilities at hand: Consider a content structure like this: <tr class="human"> <td class="event"><a>event1</a></td> <td class="nam ...

Adding new data to a Chart.js line graph in vue on form submission - A step-by-step guide

I'm struggling with dynamically updating my line chart with new data. I want the chart to refresh every time a user submits a form with new data. Currently, I can add new data to the datasets array in the data function of App.vue, but the chart doesn& ...

Automatically change a text based on its location

Currently leveraging the amazing flexible-nav to showcase the current subtopic within my post. I am also considering the possibility of extracting this current-string and showcasing it at the top of the page in my main navigation bar. This way, the text w ...

The utilization of "startIcon" and "endIcon" from the <Button/> API of Material-UI is restricted

I've been trying to work with this React code for a single component, but no matter what I do, I keep getting the same warning. I even tried copying and pasting the example, but the warning persists and the icon is not showing up. Can someone please a ...

Error: An unexpected identifier was found within the public players code, causing a SyntaxError

As a newcomer to jasmine and test cases, I am endeavoring to create test cases for my JavaScript code in fiddle. However, I'm encountering an error: Uncaught SyntaxError: Unexpected identifier Could you guide me on how to rectify this issue? Below is ...

The functionality of Node.js middleware is not functioning correctly

My module contains Routes, and I want to verify access before allowing users to proceed. Within the Routes module, there are two routes and two access-check functions that I want to use as middlewares: const checkUser = (req, res, next) => { if (!us ...

The specified property cannot be found within the type 'JSX.IntrinsicElements'. TS2339

Out of the blue, my TypeScript is throwing an error every time I attempt to use header tags in my TSX files. The error message reads: Property 'h1' does not exist on type 'JSX.IntrinsicElements'. TS2339 It seems to accept all other ta ...

Guide to decoding JSONP data sent from a remote server

Working on retrieving data using JSONP. After observing the returned data in Firebug, I can confirm that it is being returned correctly. However, I am facing a challenge in figuring out how to parse it. Is the data returning as a nested array? The callback ...

How to customize the hover and active properties of a checked checkbox in Vue 3 using Tailwind CSS and PUG?

It seems that checkboxes have a level of sophistication that I never anticipated, or perhaps my brain just can't quite grasp the nuances of checkboxes. After spending a significant amount of time researching and experimenting with checkboxes in a Vue ...

Connect a nearby dependency to your project if it has the same name as an npm repository

What is the best way to npm link a local dependency that has the same name as a project in the npm registry, like https://registry.npmjs.org/react-financial-charts? Here is an example: cd ~/projects/react-financial-charts // Step 1: Navigate to the packa ...

Steps for adding a user-glyphicon within an img tag

In the given code, I am trying to create a function where if no photo is uploaded, a default bootstrap glyphicon-user image should be displayed. Once a photo is uploaded, it should overwrite the default image. //Please ensure necessary Bootstrap files are ...

An autocomplete CSS editor with Firebug-like features

Looking for a CSS (or HTML) editor that offers code autocompletion similar to Firebug's features. I believe it's referred to as "autocomplete as you type." Thank you! Edit: I came across a tool called Zen Coding that provides shortcuts for cod ...