When hovering, the :after element will transition to the current div

How can I make the pink color div move vertically in front of the currently hovered div when we hover on it? Any suggestions on how to achieve this effect?

https://i.sstatic.net/yfwgf.png

$(document).ready(function() {
  $(".timeline-entry").hover(function(e) {
    e.preventDefault();
    $('.timeline-entry.current').removeClass('current');
    $(this).addClass('current');
  });
});
.timeline {
  position: relative;
  display: flex;
  flex-direction: column;
}

article {
  width: 100px;
  height: 50px;
  background: red;
}

.timeline-entry.right-aligned {
  align-self: flex-end;
  background: blue;
}

.timeline-entry.left-aligned {
  float: left;
}

.timeline:after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 20px;
  height: 50px;
  background: pink;
}


/* .timeline-entry:hover .timeline:nth-child(1) .timeline:after {
  top: 100px;
} */

.timeline-entry.current {
  background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div class="timeline">
  <article class="timeline-entry right-aligned"></article>
  <article class="timeline-entry left-aligned"></article>
  <article class="timeline-entry right-aligned"></article>
  <article class="timeline-entry left-aligned"></article>
</div>

Answer №1

If you're looking to show an element on hover without the need for JavaScript, this CSS solution might be what you need. The code snippet below demonstrates how you can achieve this effect using only CSS.

.timeline {
  position: relative;
  display: flex;
  flex-direction: column;
}
article {
  width: 100px;
  height: 50px;
  background: red;
  position: relative;
}
.timeline-entry.right-aligned {
  align-self: flex-end;
  background: blue;
}
.timeline-entry.left-aligned {
  float: left;
}
.timeline-entry:hover {
  background: yellow;
}
.timeline-entry:hover:after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 20px;
  height: 100%;
  background: pink;
}
<div class="timeline">
  <article class="timeline-entry right-aligned"></article>
  <article class="timeline-entry left-aligned"></article>
  <article class="timeline-entry right-aligned"></article>
  <article class="timeline-entry left-aligned"></article>
</div>

The pseudo-element's height has been adjusted to fit within the article element properly. For further customization and variations, different approaches can also yield similar outcomes while maintaining a CSS-exclusive method. Explore the possibilities!

UPDATE

Below is an alternative approach to achieving the same hover effect with pure CSS, tailored to adapt to changes in the question itself.

article {
  height: 50px;
  position: relative;
}
article div {
  width: 100px;
  height: 100%;
}
.timeline-entry.right-aligned div {
  float: right;
  background: blue;
}
.timeline-entry.left-aligned div {
  float: left;
  background: red;
}
.timeline-entry div:hover {
  background: yellow;
}
.timeline-entry div:hover::after {
  content: '';
  position: absolute;
  top: 0;
  left: calc(50% - 10px);
  width: 20px;
  height: 100%;
  background: pink;
}
<div class="timeline">
  <article class="timeline-entry right-aligned"><div></div></article>
  <article class="timeline-entry left-aligned"><div></div></article>
  <article class="timeline-entry right-aligned"><div></div></article>
  <article class="timeline-entry left-aligned"><div></div></article>
</div>

While there are multiple ways to accomplish this visual effect, sticking with a CSS-only strategy provides simplicity and effectiveness in implementation. Enjoy design experimentation and discover unique solutions!

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

Delay the script for a specific duration

I'm facing an issue where I have a function that smoothly scrolls to the top of an element instead of just jumping there. However, I need to include an option to pause the execution while the scroll event is in progress. The problem I'm encounte ...

How can I troubleshoot an image not appearing in Bootstrap within a Laravel Blade file while using PHPStorm?

Forgive me if this sounds like a silly question: I attempted to use the following src attribute in an img tag to show an image on a website created with Bootstrap, while using phpstorm with a laravel blade file: src="C:\Users\MAHE\Pictures&b ...

Tips for addressing lag problems in my Three.js game as time progresses

My game in Three.js doesn't start off with any lag, but after a few minutes, the performance begins to slow down on computers running it. I've tried reviewing my code and checking my arrays, adjusting values to troubleshoot, but so far, nothing s ...

CSS: Centralize content and use a div to hide images that overflow the container

I successfully centered my divs and images both horizontally and vertically using CSS: .center { height: 100% } .center-center { position: absolute; top: 50%; left: 50%; width: 100%; transform: translate(-50%, -50%); text-align: center; ...

Select2 script fails to render properly after returning from Action Result

In my MVC4 application, the index page features JQuery UI tabs that I use to display content updated via Ajax action links. One of the tabs includes a Select2 option box which renders correctly initially but defaults to basic functionality after clicking a ...

The jQuery UI calendar widget fails to include a button on the webpage

I am currently working with a function that I use to add datepickers to dynamically created input elements of type date after wrapping them in $(...). function createDatePicker(inputElement) { inputElement.datepicker({ showOn: "button", buttonIm ...

Issues with JQuery: Cannot display element using its ID

There is a hidden element that should be displayed upon an action trigger. Sample code: <a href="#Comment" class="btn btn-control" id="Commentbtn"> <svg class="olymp-comments-post-icon"><use xlink:hr ...

Jenkins server is encountering failing Protractor test cases, while they successfully pass on a local machine. The error message displayed is "Element is not clickable

In my script, I have developed a functionality to create and delete a form, triggering a modal dialogue box with "Create" & "Delete" buttons. The script works fine on my local machine but fails on the Jenkins server with the following error message: [31mU ...

Pressing the "Contact Us" button is not causing the modal to open

The "Contact Us" button on my Bootstrap 5.2 modal is not opening the Modal. What could I be missing? <!DOCTYPE html> <html lang="html" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="utf-8"&g ...

Vue.js component mismatch in the layout

Attempting to set up a Vue application with vuetify while incorporating layouts. As a newcomer to Vue, I may have made some beginner errors. Here is the structure of my app: main.js // The Vue build version to load with the `import` command // (runtime- ...

Unable to execute the new firebase on node server

I'm currently watching this google talk. However, I am facing an issue trying to create a firebase object. Following my package.json file, I initiated with: npm install firebase --save Then proceeded with the following steps: let Firebase = requir ...

What role does the conditional statement play in the function ExtrudeGeometry.UVGenerator.generateSideWallUV within three.js?

Within three.js's ExtrudeGeometry.UVGenerator.generateSideWallUV function, there is a specific condition being checked: if ( Math.abs( a.y - b.y ) < 0.01 ) { return [ new Vector2( a.x, 1 - a.z ), new Vector2( b.x, ...

The utilization of local storage within Backgridjs

I am creating an app using Backbone.js (Marionette Framework) along with additional functionalities like Backgrid, Backbone local storage, Backbone Radio, epoxy, and Backbone relational model. Encountered Problem: An issue arises when utilizing the local ...

Encountered an error while iterating through an array in Angular: "Cannot access property at index

I've encountered an error while trying to loop through an array of items and pushing them to another array. I am able to access the items at index "0", but still facing the mentioned error. Below is the code snippet for reference: createReportFormu ...

What methods do you suggest for storing the app's state in the browser to reduce the number of requests to the backend?

Recently at work, I encountered an issue with our application that is generating unnecessary requests and causing performance issues. Our technology stack consists of Typescript, React, and Redux (not Redux-Toolkit). I am seeking the following outcomes: ...

Guide to verifying the existence of a specific object mapping in JavaScript

I am currently working with an object mapping called res.payload.data[0].tnxResponse. I need to verify that the res object contains a payload property which in turn has a data property and so on. I attempted to do this using the following code, but it resu ...

Images with scratches visible in Chrome

This puzzle may seem easy to some, but it's been quite challenging for me. I've encountered a discrepancy in how Firefox and Chrome render images (specifically technical drawings). Despite my attempts to use image-rendering: -webkit-optimize-cont ...

Is there a discrepancy in the JSON algorithm?

While using google chrome, I encountered the following scenario: I had an object in javascript that looked like this: var b = { text: 'hello world, you "cool"' }; I then used JSON.stringify to convert it to a string and sent it to the dat ...

"Addressing the issue of ineffective form validation for empty or whitespace inputs

I've been working on creating a form and everything seems to be going well except for the validation aspect. It doesn't highlight when intentionally left blank or filled with spaces. I have included "required" in the HTML which partially achieves ...

Check to see if the ObjecticID is already present in the array or not

In my MongoDB database, I have created a column called history as an array for each user. This column stores the IDs of specific cards that the user has interacted with. Now, I want to implement a condition to check whether a card ID is already present in ...