Animating the background color of a div vertically from the center using CSS3 or jQuery

I am attempting to achieve a similar effect to the right navigation on the following website:

Using jQuery to create the effect where the color expands from the center is what I am aiming for. However, since that particular site was built using Flash, I am unable to analyze how it was done. I am unsure of what exactly to search for. I was thinking of terms like 'background radial animation jquery' or 'background color animation from center jquery.'

I also considered trying a CSS3 ease-in effect like the one explained here (Expand background from center (CSS / Javascript)). The issue with this solution is that it only demonstrates the CSS3 transition working horizontally, while I require it to work vertically. I have experimented with the JSFiddle provided in the answer (http://jsfiddle.net/SNzgs/), but I can only manage to animate the transition downwards from the top, not expanding from the center. The code snippet they used is:

.redline {background:red;height:10px;width:0;margin:auto;}

.container:hover .redline {
    width:200px;
    -webkit-transition: all 0.3s ease-out;
    transition:all 0.3s ease-out;
}

The code I experimented with is:

.redline {background:red;height:0px;width:10px;margin:auto;}

.container:hover .redline {
    height:200px;
    -webkit-transition: all 0.3s ease-out;
    transition:all 0.3s ease-out;
}

Any assistance you can provide would be greatly appreciated!

Answer №1

My approach shares similarities with matewka's solution, but incorporates both :before and :after pseudo-elements. Here is an example of the HTML markup:

<nav>
    <ul>
        <li><a href="#">Link</a></li>
    </ul>
</nav>

Here is the CSS code:

nav ul {
    list-style: none;
    width: 6em;
}
nav ul li {
    background-color: #eee;
    position: relative;
}
    nav ul li:before,
    nav ul li:after {
        background-color: #333;
        content: "";
        display: block;
        position: absolute;
        left: 0;
        right: 0;
        top: 50%;       /* Positioned vertically in the center */
        bottom: 50%;    /* Positioned vertically in the center */
        transition: all .125s ease-in-out;
        z-index: 50;
    }
nav ul li a {
    color: #333;
    display: block;
    padding: .5em 1em;
    position: relative;
    z-index: 100;
    transition: all .125s ease-in-out;
}
nav ul li a:hover {
    color: #eee;
}
    nav ul li:hover:before {
        top: 0;    /* Expands :before to cover top half */
    }
    nav ul li:hover:after {
        bottom: 0; /* Expands :after to cover bottom half */
    }

http://jsfiddle.net/teddyrised/rRtmB/

Answer №2

You can achieve this effect by utilizing the :after pseudo element along with absolute positioning. By adjusting the height and top properties of the overlay box, you can create the desired shading effect.
I have created a brand new demo: http://jsfiddle.net/abc123/9/ as your existing one was designed for horizontal shading.

.wrapper:after {
    content: "";
    display: block;
    background: #ccc;
    width: 100%;
    height: 0;
    top: 0.5em;
    position: absolute;
    left: 0;

    transition: all 1s ease-out;
}
.wrapper:hover:after {
    top: 0;
    height: 100%;
}

Answer №3

To add an animated background, you can utilize the :after pseudo element:

CSS

ul {
    margin: 0;
    padding: 0;
}
ul > li {
    list-style: none;
    margin: 0;
    padding: 0;
    position: relative;
}
ul > li > a {
    display: block;
    padding: 10px;
    color: #000000;
    text-decoration: none;
    position: relative;
    z-index: 10;
    -webkit-transition: all 400ms;
    transition: all 400ms;
}
ul > li:hover > a {
    color: #ffffff;
}
ul > li:after {
    content: " ";
    position: absolute;
    top: 50%;
    bottom: 50%;
    left: 0;
    right: 0;
    background: #353535;
    z-index: 0;
    -webkit-transition: all 400ms;
    transition: all 400ms;
}
ul > li:hover:after {
    top: 0;
    bottom: 0;
}

HTML

<ul>
    <li><a href="">Hello</a></li>
    <li><a href="">Hello</a></li>
    <li><a href="">Hello</a></li>
    <li><a href="">Hello</a></li>
</ul>

Check out the demo

Answer №4

This solution should work for your needs, but keep in mind that it may require some adjustments:

.box {height:120px;width:220px;background:#f2f2f2;position:relative;}
.box p {display:block;}
.horizontal-line {background:#999;height:12px;width:220px;position:absolute;bottom:0;}
.vertical-line {background:blue;height:0;width:220px;margin:auto; top:5px;position:relative;}
.box:hover .vertical-line {
    height:12px;
    top: 0px;
    -webkit-transition: all 0.3s ease-out;
    transition:all 0.3s ease-out;
}

Answer №5

To achieve a similar effect, you can make a few adjustments to your code on the fiddle:

<div class="box">
    <span class="vertical-gray">
        <span class="vertical-red-line"></span>        
    </span>
    <span class="gray-line">
        <span class="red-line"></span>
    </span>
</div>

Here is the updated CSS:

.box {height:100px;width:200px;background:#eee;position:relative;}
.box span {display:block;}
.gray-line {background:#ccc;height:10px;width:200px;position:absolute;bottom:45px;}
.red-line, .vertical-red-line {background:red;height:10px;width:0;margin:auto;}
.vertical-gray {
    background: #CCC;
    height: 100%;
    position: absolute;
    width: 10px;
    left: 90px;
}
.box:hover .red-line {
    width:200px;
    -webkit-transition: all 0.3s ease-out;
    transition:all 0.3s ease-out;
}
.box:hover .vertical-red-line {
    height:100%;
    width: 10px;
    -webkit-transition: all 0.3s ease-out;
    transition:all 0.3s ease-out;
}

You can view the updated fiddle here.

Answer №6

Allow me to offer you a potential solution:

JSFiddle Example: http://jsfiddle.net/username/example/

Here is the CSS code:

.box {height:200px;width:300px;background:#ccc;position:absolute;}
.blackline {display:block;position:relative;background:#000;height:0%;width:100%;}
.redline {display:block;position:relative;background:#f00;height:0%;width:100%;margin-top:80px;}
.box:hover .redline {
    margin-top:0px;
    height:200px;
    -webkit-transition: all 0.5s ease-out;
    transition:all 0.5s ease-out;
}

Here is the HTML code:

<div class="box">
    <div class="blackline">
       <div class="redline"></div>
    </div>
</div>

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

Dynamically populate 7 select boxes with options using JQuery

I have a webpage that includes 14 different dropdown menus, one for each day of the week (Monday to Sunday). Each day has two dropdowns: one for opening time and one for closing time. I used jQuery to populate all 14 dropdowns with a pre-defined list of ho ...

Having trouble grasping the purpose of app.use('/') in the Express framework

Below is the code snippet I am currently working with: // Initializing express, body-parser, mongodb, http, and cors var app = express(); app.use(cors()); app.use(express.urlencoded()); // Parse incoming JSON data from API clients app.use(express.json()); ...

Why is it not possible to pass references when utilizing a self-invoking function?

I have been experimenting with the IIFE pattern for some of my modules lately and encountered a problem that has stumped me. In my current project, I need to pass a few global variables for usage. One of these is the global googletag variable which initial ...

I possess a pair of arrays, and I aim to transfer certain elements from Array1 to Array2 while maintaining their references

Looking to implement two JavaScript arrays using AngularJS. My goal is to transfer elements from Ar1 to Ar2, and then have any changes made to the values in Ar2 automatically update the values in Ar1 as well. ...

Executing a JavaScript function

I am currently working on an ASP webpage and I am facing an issue with writing JavaScript within it. My goal is to assign a variable to the response of a function that is stored in the code behind (page.aspx.vb). Here is an example of what I am attemptin ...

Converting a base64 string to a PNG format for uploading to an S3 bucket from the frontend

Feeling a bit overwhelmed here, hoping this isn't just a repeat issue. I've come across similar problems but none of the solutions seem to be working for me at the moment. I'm currently utilizing react-signature-canvas for allowing users to ...

Display the homepage if a user accesses a page intended for an ajax request

Currently, I am utilizing jQuery to create a straightforward website. The main page is called 'index.html' and it has the capability to load different content (such as 'info1.html' or 'info2.html') through jQuery ajax requests ...

Is there a way to preserve text content prior to activating the text-overflow feature with

Is there a way to extract the last sentence or word, or even the entire text displayed before the ellipsis in a container with limited height where the content is inside a p-tag with a long content using text ellipsis and overflow? .grid-container > ...

Console does not display AJAX response

Currently, I am utilizing AJAX to fetch form data from a Django application and my objective is to display the response in the console. $.ajax({ type: 'GET' , url: url, data: {'PUITS': PUITS ...

Instructions for including a scroll bar in an HTML side menu

Can anyone help me figure out how to add a scroll bar specifically for the left side navigation menu? I've tried adding overflow-y: scroll; in different places, but it hasn't worked so far. I'm still learning web design and have been piecing ...

Can a background image be displayed within a DIV using position:absolute?

I have scoured every corner, yet the answer still eludes me. In my style sheet, I have a DIV with absolute positioning and a background image. Within that DIV, I want to place a paragraph that appears underneath the background. I attempted to use z-inde ...

Nodejs is utilized to alter the data format as it is transferred from the client to the server

I'm encountering an issue with transmitting my data to the server using Node.js. I have a feeling that there are discussions on this topic already, but I'm unsure of what to search for to locate them... Here's a brief overview of my applica ...

Moving from traditional web pages to a mobile application using NextJS has brought about the error "rest.status is

Currently, I am in the process of upgrading from Next 13.2.5 to version 14.1.0 and switching to using app/api/example/route.js instead of pages/api/example.js. After making these changes, I encountered an error stating TypeError: res.status is not a funct ...

Encountering an issue with setting up Pinia in Vue3, as it is showing an error message stating "

I am facing a challenge with my Vue app where the normal reactive store is not working correctly and does not retain the values I have set. Hence, I would like to switch to a Pinia store. After installing Pinia, my main.js file looks like this: import { cr ...

Node-powered Angular

Currently working on setting up client-side routing with AngularJS and Node. Ran into some issues along the way. EDIT After making changes to my code based on recommendations from @PareshGami, following https://github.com/scotch-io/starter-node-angular, I ...

Having trouble with constructing cascading dropdown menus in ASP.NET Core 7 MVC through the use of JQuery and the ajax.googleapis.com platform

As I've been delving into the world of cascading dropdown lists, my search for examples using ASP.NET Core 7 led me to stumble upon an interesting example on a different StackOverflow post, which referred to this site. However, I encountered a roadbl ...

Issue with displaying record attribute as a text variable

I'm currently attempting to retrieve the attribute odata.type from a record so that I can adjust my EditForm based on its value. Strangely, when I utilize a constant to achieve this and print it with console.log, it appears as follows: ƒ HWType(_ref ...

Updating values using jQuery when tags in a single table row are changed

In my current setup, I have a table structured as follows: <table> <tbody> <tr> <td> <input type="text" class="identifier" /> </td> <td class="name"> Some value < ...

Installing from a GitHub repository does not always retrieve all of the necessary files

After making some modifications to a specific NPM package by forking the GitHub repository, I am now facing challenges when trying to install it in my project. The command I used to install this modified package is: npm install --save git+https://github.c ...

To restore the position of the chosen object in Three.js after clicking reset

I am facing an issue with resetting the position of my latest Object in three.js. Initially, my code consists of the following: function onDocumentMouseDown( event ) { event.preventDefault(); var vector = new THREE.Vector3( mouse ...