The art of bringing a pseudo class to life through animation

Seeking assistance with achieving a unique effect on click event. I have set up a slanted or razor-blade style div using a parent div element and a pseudo-element :after (as shown below). My goal is to change the skew angle when the user clicks on the parent element. However, I am facing issues in getting the animation to work whether I target the parent or the pseudo element on click. Any guidance in the right direction?

$(document).ready(function() {

$('.slantedDiv').click(function() {
$('.slantedDiv .slantedDiv:after').toggleClass('active');
});
});
* {
padding: 0;
margin: 0;
}

.slantedDiv {
position: relative;
width: 100%;
height: 300px;
background-color: yellow;
}

.slantedDiv:after {
position: absolute;
width: 100%;
height: 100%;
content: '';
background: inherit;
top: 0;
right: 0;
bottom: 0;
left: 0;
transform-origin: top left;
transform: skewY(10deg);
transition: all .3s ease-in-out;
}

.active {
transform-origin: top right;
transform: skewY(-10deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slantedDiv"></div>

Answer №1

The problem lies with the selector used in the click function. The element being targeted is simply .slantedDiv, so duplicating the class name will not match any elements. This can be resolved by directly referencing this within the jQuery object. Additionally, jQuery cannot select pseudo elements like :after, so including it in the selector will not yield any results.

Instead, you should toggle the active class on the .slantedtDiv itself and then utilize the :after pseudo-element in your CSS based on the presence of the active class. Here's an example:

$(document).ready(function() {
  $('.slantedDiv').click(function() {
    $(this).toggleClass('active');
  });
});
* {
  padding: 0;
  margin: 0;
}

.slantedDiv {
  position: relative;
  width: 100%;
  height: 300px;
  background-color: yellow;
}

.slantedDiv:after {
  position: absolute;
  width: 100%;
  height: 100%;
  content: '';
  background: inherit;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform-origin: top left;
  transform: skewY(10deg);
  transition: all .3s ease-in-out;
}

.slantedDiv.active:after {
  transform-origin: top right;
  transform: skewY(-10deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slantedDiv"></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

Is there a way to utilize a cookie in order for the website to recognize that I have already agreed to the terms?

It's common for websites to ask for cookie and privacy acceptance upon loading, especially in the EU. I'm looking for a way to automatically accept these cookies so I don't have to constantly click "accept all" every time I open Chrome. My ...

Guide on parsing a JavaScript file and converting the default export module to JSON with Node.js

What I'm trying to accomplish in my Node.js project is reading a sample.js file with ES Module syntax and extracting the default export from it. sample.js import foo from "foo"; const bar = [ { name: "Homer", }, { n ...

How can I dynamically update the content of the right side of the side navbar by clicking on navbar links?

I have a side-navigation bar with content on the right side. How can I display specific content on the right side when clicking on a navigation link? For example, only show the name when clicking on the name link and only show the password field when click ...

The Ajax autocomplete feature was unable to find a matching HTTP resource for the requested URI

Seeking assistance with implementing autocomplete functionality on a textbox using data from a web method. The webmethod's browser operation is as follows: http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers?SearchString=e The resu ...

Is there a way to initiate an AJAX post request with the Authorization header for the initial request using Windows Authentication?

I'm working on a web application that has a video page and a Web API for logging purposes. The events are triggered using an ajax post request: function logAction(actionToLog) { $.ajax({ type: 'POST', url: "/api/v/" + cu ...

Can you explain how I can declare a variable to store a scraped element in Puppeteer?

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ headless: false, defaultViewport: null }) const page = await browser.newPage() await page.goto('https://www.supre ...

Launch a new email window in Outlook from a server using C#

Currently, I am working on a feature where users can select multiple product links and have the option to email those links to someone. The functionality works perfectly fine on my local machine, but when deployed, it throws an exception as shown below: ...

The innerHTML of the p tag remains unaffected when using document.getElementsById("")

HTML {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'lessons/style.css' %}" /> <script> function openNav() { document.getElementById("mySidenav").style.width = "20%"; document.getElementById("main" ...

When using ODataConventionModelBuilder in Breeze js, the AutoGeneratedKeyType will consistently be set to 'none'

I am working with a straightforward entityframework poco object public partial class Location: Entity { [Key] public int Id { get; set; } public string Description { get; set; } } The baseClass Entity is structured as below public abstract c ...

Ways to avoid divs overlapping on a mobile device?

If you visit this page: The recent searches section displays correctly on a computer, but it overlaps with the footer when viewed on an iPhone 6. What steps can I take to avoid this issue? ...

Modifying source dynamically in Javascript file through web.config during runtime

I have the following code snippet: <script type="text/javascript" src="path?key=1234567890"> </script> Additionally, I included the following in my web.config file: <appSettings> <add key="key" value="1234567890"/> Now, ho ...

Troubles with Placing Image on WordPress

Working on a Wordpress component for a website where I placed a "SOLD OUT" image over text. However, the issue arises when viewing the site on mobile devices, as the images are smaller and not in their correct positions due to using absolute positioning. ...

Wrap the chosen text within tags in a particular element

Is there a way to wrap <span> tags around selected text within an element? For instance, if someone highlights the word "John", I would like to enclose it with span tags. Sample Code in HTML <p>My name is Jimmy John, and I hate sandwiches. M ...

Using jQuery to check if the input time is empty

Why is this code not functioning properly? I have filled the input but it still triggers an alert saying 'empty'. <label for="">New ETA:</label> <input type="time" class="neweta"> <input ty ...

Is there a way to use jQuery to automatically make a field stand out visually?

I am looking for a way to make a text field automatically underline as the user types. The line should follow along with the characters in real-time, and when the user moves away from the field, I want the line to fill up the entire width. This signifies t ...

What steps can I take to avoid triggering a delayed binding on a jQuery autocompleter?

My jQuery autocomplete code is quite straightforward. I have set up an autocomplete feature on an HTML text input identified as #search. When a user enters something into the input field, a drop-down list displays suggestions retrieved from the server. If ...

Tips for solving the "jQuery needs a window with a document" error when using import statements

I'm encountering the "jQuery requires a window with a document" error, and it appears that I require guidance similar to this solution: Error: jQuery requires a window with a document My quest is to find the precise syntax for addressing this using i ...

Modify the color of an Ionic button for a single button, rather than changing the color for all buttons

How can I modify this code so that only the clicked button changes its color, not all of them? Here is the current code: .html: <ion-col size="6" *ngFor="let data of dataArray" > <ion-card> <ion-card-header> ...

Displaying multiple categories of articles on a single page with Node.js

Seeking a solution to limit the number of posts displayed for each category using a .limit() function, but uncertain on how to proceed. Currently utilizing Mongoose and Express in my code setup. The existing code snippet is outlined below: router.get(&a ...

Tips for invoking both a typescript arrow function and a regular javascript function within one event

Is it possible to call both a JavaScript function and a TypeScript function from the same onClick event in a chart? I am new to TypeScript and Angular, so I'm not sure if this is achievable. The issue at hand is that I need to invoke a JavaScript fun ...