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

Conflicting styles between Bootstrap and Formstack are causing radio buttons to appear only partially visible

Encountering a conflict between Bootstrap (4.1.3) and Formstack CSS when trying to embed a form in a website. The radio buttons are not fully visible, with the issue located in the "label" class ("fsOptionLabel"). Adjusting the line height provides a parti ...

Create the correct structure for an AWS S3 bucket

My list consists of paths or locations that mirror the contents found in an AWS S3 bucket: const keysS3 = [ 'platform-tests/', 'platform-tests/datasets/', 'platform-tests/datasets/ra ...

modification of class into hooks, receiving error message 'then' property is not found in type '(dispatch: any) => Promise<void>'

As a newcomer to React hooks, I have been converting code from class components to hooks. However, I am encountering an error message when trying to use 'then' in hooks that says 'Property 'then' does not exist on type '(dispa ...

Hiding Div with JavaScript (Quick and Easy)

Hey there, I'm looking to make regStart and regPage alternate visibility based on a click event. I'm still getting the hang of JavaScript so any simple answers would be appreciated. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/x ...

Instantaneous data refresh using Firebase Cloud Functions

Currently, I am working on developing a chat feature in React using Firebase cloud functions. However, I am facing an issue where I do not receive updates when a user sends a message. Below is the code snippet that I have been working with: const app = a ...

Plot data points from geojson onto a leaflet map using markers

How can I efficiently import geoJson data (containing over 2000 coordinates) into a leaflet map? Below is a brief snippet of geo json: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { ...

Is the navigation bar offset causing an issue?

After struggling with my navigation menu in Google Chrome, I finally found a solution. However, the nav bar is now offset on one page but not on another. For the aligned main site view, visit , and for the left-offset forum view, visit . This is the HTML ...

What is the significance of [Function: bound ] in the context of node debugging?

So I was trying to debug my .js file using node debug and when I used catch(error) { It only displayed [Function: bound ] when I tried console.dir(error) What is causing this issue? How can I access the full error object? And how do I retrieve the stack ...

Show text upon hovering using jQuery

I am currently exploring jquery and trying to implement a functionality where text is displayed upon hovering over a div element. My basic html page consists of squares, with one of them rotating when a button is clicked. I now want to show some text withi ...

What is the best way to break out of a function halfway through?

What are your thoughts on using nested if statements? $scope.addToCart = function () { if (flagA) { if (flagB) { if (flagC) { alert('nononono!'); return; } } } e ...

Troubleshooting: Issues with CSS fixed positioning

Whenever I try to scroll down, my navbar moves along with the page. Even when I resize the window, it keeps shifting despite using position:fixed. What mistake am I making? nav li:nth-child(1) { position: fixed; border: 1px solid #15317E; font-si ...

When using videojs, I have the ability to include a Text Track event handler, however, there is currently no option to remove it

I implemented a listener for the 'cuechange' event on a Text Track and it's functioning correctly. However, I am unable to figure out how to remove this listener. I have attempted the instructions below to remove the listener, but it continu ...

Showing the information obtained from an API request once the user submits the form without the need to reload the page

I am currently working on a form that will take search query requests from users upon submission and then display the results by making an API call. My goal is to show these results without the page having to refresh, using AJAX. The backend connection to ...

Using Angularjs: Trigger Directive Execution Post Completion of ng-init Function

I'm facing an issue with calling a function in ng-init within my HTML file. The function is responsible for making an API call and collecting data, which is then assigned to a scope variable and passed to a directive. Initially, the controller gets e ...

In a responsive layout, a floating div refuses to adhere to the left alignment

I am working on a dynamic list using ASP:REPEATER with floating divs aligned to the left. The current layout is as follows: +---------------------+ +---------------------+ +---------------------+ | | | | | ...

How to choose the desired day within a specific month when creating a calendar from scratch?

Hi there! I need some assistance. I'm currently working on enhancing a calendar by adding an extra feature. The idea is that when the user selects one or more days, those specific day(s) should be highlighted. However, I'm facing an issue where s ...

Challenges of relative positioning with dynamic height based on content

Hello! I'm trying to achieve this https://i.stack.imgur.com/Q6lXP.png In this case, there are 3 divs. The top one will have a width of 100% and a height of auto, with relative positioning. The second div is similar, but I plan to add some dummy text ...

After a successful login, learn how to implement a bottom tab menu in React Native

I'm diving into the world of react-native and finding myself a bit lost when it comes to routing and navigation. I have 4 screens - Login, Register, Home, and Links. The Register and Login screens are all set up, with the ability to navigate back usin ...

The value of the checkbox model in AngularJS is not defined

I'm encountering an issue where I am trying to send the value of a checkbox from my model to the server. Since the checkbox has not been interacted with on the form, Angular does not assign it a value and returns undefined when I request the value. B ...

A comprehensive guide on iterating through an array in JavaScript

Currently, I am facing an issue while trying to iterate over an array of Objects in React that have been fetched from Django. The object is stored as an array, but when attempting to map it, I encounter a typeerror stating "cannot read property map of unde ...