Learn how to create a stunning effect by combining two half images and revealing a full image upon hover with a smooth animation

I am struggling with implementing a specific feature using jQuery. I have designed a page hero with two sections (red and black):

My goal is to have the black section expand over the red section when hovering, creating a full black box. I want the same effect for the red section as well:

How can I achieve this functionality?

var redSection = $('#red');
var blackSection = $('#black');

redSection.on('mouseover', function() { 
    // Add code here to overlay the other section
});

The HTML code for the sections is as follows:

<section id="hero">
        <figure id="urbandesign">
            <a href=“#" target="_blank">
                <img src="images/urbandesign.jpg" alt="Urban Design">
            </a>
        </figure><!-- End figure -->

        <figure id="photography">
            <a href=“#" target="_blank">
                <img src="images/photography.jpg" alt="Photography">
            </a>
        </figure><!-- End figure -->

    &...

And here is the CSS styles used:

#hero {
    height: 480px; /* Default 500px */
    padding: 0;
    position: relative;
    overflow: hidden;
    z-index: 1;
    background: url(../images/hero.jpg) no-repeat center; /* remove */

    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover; 
}

#hero figure {
    position: absolute;
    top: 0;
    background: #FFF;
}

#hero img {
    width: 100%;
    max-width: none;
    position: relative;
    opacity: 0.4;
}

I aim to replace the red and black sections with images in the final design. Thank you for your assistance!

Answer №1

To solve this issue, a combination of CSS3 and jQuery with Graceful Degradation is recommended.

Styling with CSS

.page {
    position:fixed;
    width:100%;
    height:100%;
    overflow:hidden;
}
.black {
    background:#000;
    width:50%;
    height:100%;
    position:absolute;
    width:100%;
    left:-50%;
    transform:skew(30deg,0);
    transition:0.5s ease-in-out;
    z-index:1;
}
.red {
    background:#ff0000;
    width:50%;
    height:100%;
    position:absolute;
    width:100%;
    right:-50%;
    transform:skew(30deg,0);
    transition:0.5s ease-in-out;
}
.red:hover {
    transform:skew(0);
    transform:translate(-50%,0);
}
.black:hover {
    transform:skew(0);
    transform:translate(50%,0);   
}
.inactive {
    z-index:-1
}

Structuring HTML

<div class="page">
    <div class="black"></div>
    <div class="red"></div>
</div>

Integrating jQuery

In order to address a z-index issue affecting the last element in the DOM tree and disrupting the smooth animation, jQuery intervention is necessary.

$(document).ready(function(){
    $('.black').hover(function(){
        $('.red').addClass('inactive');
    },function(){
        $('.red').removeClass('inactive');
    });
     $('.red').hover(function(){
        $('.black').addClass('inactive');
    },function(){
        $('.black').removeClass('inactive');
    });
});

Note that if you add content to the two divs, an inner div must be included along with resetting the skew using 'transform:skew(-30deg,0);'. Additionally, consider including the prefixed versions of transition and transform properties.

JSFiddle Reference

Answer №2

To achieve this effect, you can utilize the svg element's path attribute for defining the shape, pattern for displaying an image within the shape, and a touch of JavaScript to handle the mouseover and mouseleave events.

var hero = document.getElementById('hero');
var animLeft = document.getElementById('anim-left');
var animRight = document.getElementById('anim-right');
hero.addEventListener('mouseover', function(e) {
  (e.target.id == 'left') ? animRight.beginElement() : animLeft.beginElement();
})
hero.addEventListener('mouseleave', function(e) {
  (e.target.id == 'left') ? animRight.endElement() : animLeft.endElement();
})
<svg id="hero" width="600" height="200" viewBox="0 0 600 200">
  <defs>
    <pattern id="image-left" patternUnits="userSpaceOnUse" width="600" height="200" viewBox="0 0 600 200">
      <image xlink:href="http://dummyimage.com/600x200/40000c/000" width="600" height="200" />
    </pattern>
    <pattern id="image-right" patternUnits="userSpaceOnUse" width="600" height="200" viewBox="0 0 600 200">
      <image xlink:href="http://dummyimage.com/600x200/002a33/fff" width="600" height="200" />
    </pattern>
  </defs>
  <a xlink:href="#">
    <path id="right" d="M0,0 h600 v200 h-600z" fill="url(#image-right)" />
  </a>
  <a xlink:href="#">
    <path id="left" d="M0,0 h350 l-100,200 h-250z" fill="url(#image-left)" />
    <animate id="anim-left" xlink:href="#left" attributeType="XML" attributeName="d" from="M0,0 h350 l-100,200 h-250z" to="M0,0 h0 l-100,200 h0z" dur="1" begin="indefinite" repeatCount="1" fill="freeze" />
    <animate id="anim-right" xlink:href="#left" attributeType="XML" attributeName="d" from="M0,0 h350 l-100,200 h-250z" to="M0,0 h700 l-100,200 h-600z" dur="1" begin="indefinite" repeatCount="1" fill="freeze" />

  </a>
</svg>

Answer №3

Here is a straightforward CSS-only solution that eliminates the need for any extra re-paints:

.parent {
  overflow: hidden;
  position: absolute;
  width: 90%;
  height: 90%;
}
.item {
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  transition: transform 1s, z-index 1s;
  z-index: 1;
  overflow: hidden;
}

.item .image {
    transition: transform 1s;
}

.item:hover {
  transform: translate3d(0px, 0px, 0px);
  z-index: 100;
}

.item:hover .image {
  transform: skewX(0deg);
}

.red {
  background: #f00;
  transform: translate3d(-50%, 0px, 0px) skewX(-10deg);
}

.red .image {
  transform: skewX(10deg);
}

.black {
  background: #000;
  transform: translate3d(50%, 0px, 0px) skewX(-10deg);
}

.black img {
  transform: skewX(10deg);
}
<section class="parent">
  <div class="red item">
    <img class="image" src="http://placehold.it/450/ff0000/000000" />
  </div>
  <div class="black item">
    <img class="image" src="http://placehold.it/450/000000/ffffff" />
  </div>
</section>

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

Utilizing JQuery for Next and Previous Navigation Buttons

Currently, I am in the process of developing a website where users can click on project images to view detailed information and additional images in a full-screen overlay. I am also implementing next and previous buttons to navigate through the projects. A ...

Tips on how to align several divs within another div

I've been attempting to center multiple div blocks within another div or HTML document, but I haven't had any success with the methods I've found on StOv. Here's an image of what I'm aiming for: https://i.stack.imgur.com/DB7uQ.jp ...

Issue with the close button on ngb-alert not functioning properly

As I develop my website, I have incorporated an ngb-alert component to display an alert message to users upon login. While the alert itself is functioning properly, I have encountered an issue with the close button being misaligned, and I am struggling to ...

Tips on removing either the date or time when input type date and input type time share the same ng-model

In my Ionic app built with AngularJS, I have a form where the date and time are displayed separately but share the same data-ng-model: <input type="date" id ="actualVisitDate" data-ng-model="actualVisitDate" required> <input type="time" id ="actu ...

Tips for implementing an automated date picker in Selenium using Node.js

I attempted to automate a date picker like the one shown in this screenshot: Below is the code I used to automate it using Selenium with NodeJS: const { By, Key, Builder, WebElement, Alert } = require('selenium-webdriver'); require('chro ...

Using PHP to create an HTML table that can have variable rows and columns, with the data in each row depending on

My latest project involves an assessment system that evaluates each student's performance in every lesson. I have a form set up to generate a table with the following headers: Student name Student ID Lesson title Lesson ID Lesson grade In order to ...

What's better in React: using pure components or non-pure components? Is it okay to fetch data in componentDidMount or

Exploring React in Meteor has led me to observe two distinct approaches... Take the meteor leaderboard example, where a list of players displays their names and scores. The pure approach involves fetching all players and passing them into the playersList ...

A step-by-step guide on selecting a checkbox within an alert popup using Selenium with Java

Hello everyone, I am struggling to find a solution for checking and unchecking a checkbox located in an alert window or modal pop-ups. We have three types of pop-ups: alert, confirm, and prompt. Specifically, in the confirm popup, there is a checkbox t ...

Understanding the expectations for REST responses in HTML, JSON, and XML

When sending an HTML response compared to JSON or XML for the same data, what is typically expected? For example, if I have an array of USER information, converting it to JSON or XML is straightforward. But when dealing with HTML, should I simply convert ...

Combining MarkDown elements into a DIV or a custom HTML tag

I have utilized the Jeykll tool to convert mark down content into HTML. I am looking to group the following mark down elements within a div element or another custom HTML tag. Markdown #Multiple Axis {:.title} Different types of data can be vis ...

The pop-up menu appears in a location different from where the anchor element is positioned

Having an issue with the menu placement when clicking on an Avatar. The menu is appearing in the wrong position: The avatar button "OB" on the right side is where the issue occurs. No console errors present and inspecting the Popover element shows that it ...

The basic jQuery script seems to be malfunctioning

I am trying to attach an on click event to an li element using jQuery. I have written a simple jQuery code within the document ready function, but for some reason it is not functioning as expected. I have checked in both Chrome and Firefox, and there are n ...

Counting the visible elements in Slick carousel

I have implemented slick.js to display a grid of 6 items (2 rows, 3 columns) per slide. I am looking for a way to include both the standard prev and next arrow navigation as well as an indication of the active item count for pagination assistance. For exa ...

having difficulty sorting items by tag groups in mongodb using $and and $in operators

I'm currently trying to execute this find() function: Item.find({'tags.id': { $and: [ { $in: [ '530f728706fa296e0a00000a', '5351d9df3412a38110000013' ] }, { $in: [ ...

Using Typescript to implement a conditional return type and ensuring that the value types are consistent together

I am working with a useSelectedToggle hook that helps in connecting the UI state to the open/closed status of a dialog where it will be displayed. The toggle defines the value as (T) when it is open, and null when it is closed. How can I enforce stricter ...

Angular onscroll event creating a parallax effect

I attempted to create a parallax effect using Angular and the OnScroll event, however, while scrolling, the text seems to be flickering. Is there a way to make the smooth rendering onscroll? Maybe through CSS alone? Here is the script I used: https://sta ...

Enclose an image with a <div> element while maintaining the <div> height at

Encountering a challenge where positioning an element on top of an image at specific coordinates (20% from the top and 30% from the left) is required. Initially, the solution involved wrapping the image in a div with a relative position and using absolute ...

Organize intricate JavaScript Arrays

Similar Question: How to sort an array of javascript objects? I'm in the process of transitioning some of my older ActionScript 2.0 code to JavaScript. While most things are running smoothly, I've hit a roadblock when trying to numerically s ...

Establish a connection between the Discord Bot and a different channel

I need help with my Discord bot that should redirect someone to a different channel when they mention certain trigger word(s). I feel like there might be a missing line or two of code that I need to add to make it work properly. bot.on("message", messag ...

Angular's table data display feature is unfortunately lacking

Below is a simple HTML code snippet: <div class="dialogs"> <div id="wrapper" > <p>{{createTestingConstant()}}</p> <ng-container *ngFor="let one of contacts"> <p>{{one ...