Looking to generate flipcards dynamically through javascript or jquery when a button or link is clicked?

I've created flipping cards that flip on hover, but I'm struggling to add a button/link that generates a new flipcard when clicked. Any help would be greatly appreciated - plus, I'm new here! Here's my HTML and CSS code for the flipcards:

$(document).ready(function() {
  $("#new").click(function() {
    $("card effect__hover").clone().insertAfter(thi);
  });
});
html {
  background: url(index_bg.jpg) no-repeat;
  background-size: cover;
}
.card {
  position: relative;
  float: left;
  width: 200px;
  height: 200px;
  text-align: center;
  margin-top: 100px;
}
.card:nth-child(1) {
  margin-top: 100px;
  margin-left: 100px;
  margin-right: 8px;
}
.card:nth-child(2) {
  margin-top: 100px;
  margin-right: 8px;
}
.card.effect__ADD {
  margin-left: 8px;
}
.card__front,
.card__back {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.card__front,
.card__back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
}
.card__front {
  background-color: blue;
}
.card__back {
  background-color: orange;
  transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__front {
  -webkit-transform: rotateY(-180deg);
  transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__back {
  -webkit-transform: rotateY(0);
  transform: rotateY(0);
}
.card.effect__ADD:hover .card__front {
  -webkit-transform: rotateX(-180deg);
  transform: rotateX(-180deg);
}
.card.effect__ADD:hover .card__back {
  -webkit-transform: rotateX(0);
  transform: rotateX(0);
}
span > p {
  margin-top: 80px;
  font-family: Arial;
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <title>Read</title>
  <script type="text/javascript" src="jquery.min.js"></script>
  <script type="text/javascript" src="custom.js"></script>
  <link href="read.css" rel="stylesheet">
</head>

<body>
  <div class="card effect__hover">
    <div class="card__front">
      <span class="card__text"></span>
    </div>
    <div class="card__back">
      <span class="card__text"></span>
    </div>
  </div>
  </div>

  <div class="card effect__hover">
    <div class="card__front">
      <span class="card__text"></span>
    </div>
    <div class="card__back">
      <span class="card__text"></span>
    </div>
  </div>

  <div class="card effect__hover">
    <div class="card__front">
      <span class="card__text"></span>
    </div>
    <div class="card__back">
      <span class="card__text"></span>
    </div>
  </div>

  <a href="" class="new">
    <div class="card effect__ADD">
      <div class="card__front">
        <span class="card__text"></span>
      </div>
      <div class="card__back">
        <span class="card__text"><p>Add Your Story</p></span>
      </div>
    </div>
  </a>


</body>

</html>

Any assistance in achieving this with jQuery would be much appreciated.

Answer №1

Within this JSFiddle, I implemented an event listener on the <a class="new"> element to capture clicks and prevent the default action. Subsequently, I duplicated the initial card element and inserted it before the (add your story) link to always maintain that link at the bottom.

$(document).ready(function(){ 
$('.new').on('click', function(event){
        event.preventDefault();
       $('.card').first().clone().insertBefore($('.new'));    
    });
});
.card {
    margin:5px;
    position: relative;
    float: left;
    width: 200px;
    height: 200px;
    text-align: center;
    margin-top: 50px;
}
.card:nth-child(1) {
    margin-left: 8px;
    margin-right: 8px;
}
.card:nth-child(2) {
    margin-right: 8px;
}
.card.effect__ADD {
    margin-left: 8px;
}
.card__front, .card__back {
    position: absolute;
    top:0;
    left:0;
    width: 100%;
    height: 100%;
}
.card__front, .card__back {
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-transition: -webkit-transform 0.3s;
    transition: transform 0.3s;
}
.card__front {
    background-color:blue;
}
.card__back {
    background-color:orange;
    transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__front {
    transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__back {
    transform: rotateY(0);
}
.card.effect__ADD:hover .card__front {
    transform: rotateX(-180deg);
}
.card.effect__ADD:hover .card__back {
    transform: rotateX(0);
}
span > p {
    margin-top: 80px;
    font-family:Arial;
    font-weight:bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="card effect__hover">
  <div class="card__front"> <span class="card__text"></span>

  </div>
  <div class="card__back"> <span class="card__text"></span>

  </div>
</div>
</div>
<div class="card effect__hover">
  <div class="card__front"> <span class="card__text"></span>

  </div>
  <div class="card__back"> <span class="card__text"></span>

  </div>
</div>
<div class="card effect__hover">
  <div class="card__front"> <span class="card__text"></span>

  </div>
  <div class="card__back"> <span class="card__text"></span>

  </div>
</div>
<a href="" class="new">
  <div class="card effect__ADD">
    <div class="card__front">
      <span class="card__text"></span>
    </div>
    <div class="card__back">
      <span class="card__text"><p>Add Your Story</p></span>
    </div>
  </div>
</a>

It's important to note that you may need to adjust the margins of the flip cards to create spacing between them.

Regarding a comment:

The disorderly layout of the flip cards is due to:

  1. In the .card CSS, the property float: left; should be removed and replaced with display:inline-block; to align them all on the same line.
  2. In the .card:nth-child(1) CSS, removing margin-top: 100px; and margin-left: 100px; will eliminate the unwanted gap before the first card. After making these adjustments, the result will resemble what's shown in this JSFiddle.

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

Guide on sending AJAX Request in Yii2 and retrieving JSON

I am facing an issue with my Yii2 web project where I need to return JSON to AJAX. I have tried using json_encode() and Yii::$app->response->format = Response::FORMAT_JSON;, but it doesn't seem to work as expected. Below is the action code: pu ...

Encountering a compilation error when attempting to import a shader from a file in a project using THREE.js, Vue.js

I've encountered an error and spent hours searching for a solution, but unfortunately found nothing. The correct way to import shader code from a file is: import {ColourShader} from '../shaders/ColourShader.js' Here is the content of my &a ...

Error encountered while compiling a method within a .vue component due to a syntax issue

I have been closely following a tutorial on Vue.js app development from this link. The guide instructed me to add a login() function in the block of the Login.vue file. Here is the snippet of code provided: login() { fb.auth.signInWithEmailAndPa ...

Creating a tailored regular expression for validating email addresses

Up to this point, I have crafted a regex pattern as Pattern="^(?=.*[a-zA-Z].*)([a-zA-Z0-9._%+-]+@([a-zA-Z0-9-]+[a-zA-Z0-9-]+(\.([a-zA-Z0-9-]+[a-zA-Z0-9-])+)?){1,4})$" that meets the following criteria: Not all characters should be numbers, ...

Adjusting the height of a container dynamically in React while flex items wrap using CSS

In my React project, I have a container called "answers-container" with multiple buttons inside it, laid out using flexbox with flex-wrap: wrap to allow them to wrap onto the next line when the container width is exceeded. However, I'm facing an issu ...

AngularJS: accessing remote systems - a guide

I am looking to explain my objective clearly I need guidance on how to establish a remote desktop connection from my Angular.js application to a windows application running system. The server I am using is Google App Engine. My current ideas: The Windo ...

Tips for resolving a blank screen issue when attempting to render components using the `:is="component"` attribute

Working with NativeScript-Vue for Android, my goal is to display a component based on button taps. To achieve this, I am utilizing this plugin which helps in integrating a global SideDrawer for smooth navigation. The buttons within the SideDrawer are used ...

When including the JQuery definition in a TypeScript code, it is not being included in the transcompiled JavaScript file as expected

After learning about importing TypeScript libraries and their compilation to JS for build output, I encountered a script that did not behave as expected. The issue remains unclear. main.ts /// <reference path="../typings/globals/jquery/index.d.ts" /&g ...

Are there any tools available in Jquery for generating a visual representation of an organizational hierarchy?

Currently, I am working on a genealogy project and searching for a suitable jQuery plugin to help me create a family tree. ...

What is the best way to align vertically a horizontal list that includes both headers and icon images?

My goal is to create a horizontal navigation bar that features menu options with headings and icons below them. Upon hovering over each option, the block's background color should change and slightly increase in size. I initially used percentages for ...

TypeScript: restrict access to field to exclusive classes only

Here's an interesting dilemma I am facing. In my project, I adhere to the MVVM architecture pattern where I have separate Views for display logic and ViewModels for functional logic. The ViewModels contain methods and fields that can be accessed by ot ...

Is there a way to determine the exported functions from a node/npm package?

After posting my previous question on accessing functions through imported packages and researching the import statement according to Mozilla's documentation, I have realized that I need to follow these steps to utilize the features in a module: imp ...

What is the best way to ensure that each box remains separate and does not overlap with the box next to it?

Just an FYI - styling in React using CSS is quite similar to regular CSS. Each individual box has its own code snippet: export const CardWrapper = styled.div` display: flex; flex-direction: column; align-items: center; border-radius: 4px; backg ...

JavaScript counter unexpectedly resetting to zero instead of the specified value

After gathering feedback from voters: My current issue revolves around a Java counter I created. Ideally, the numbers should start at 0 and increment to the specified number upon page load. You can see an example of this functionality here: https://codepe ...

Utilizing props to define the background-color of the '&:hover' state

I'm adapting Material UI's Chip component to accept custom values for the colors prop, rather than being limited to "primary" and "secondary". Additionally, I want the chip to exhibit a hover effect when clicked, transitioning to a different colo ...

Navigating the realm of JavaScript and managing the uncertainties of floating point precision

I am exploring the development of a browser multiplayer game utilizing rollback netcode that operates a deterministic simulation on the clients. I successfully prototyped the netcode in Flash, but encountered a floating point roadblock along the way. As f ...

Is there a way to calculate the total of three input values and display it within a span using either JavaScript or jQuery?

I have a unique challenge where I need to only deal with positive values as input. var input = $('[name="1"],[name="2"],[name="3"]'), input1 = $('[name="1"]'), input2 = $('[name="2"]'), input3 = $('[name=" ...

Retrieving the data-id attribute from dynamically rendered div elements

How can I modify my checkPair function to retrieve the data-id attribute when clicking on a dynamically created card? Currently, I am encountering an error stating "The name 'getAttribute' could not be found." function createHTMLElementFromHtml( ...

React Warning: Every child component within a list must contain a distinct key property

Can you spot the issue in the following code snippet: <List> {sections.map(section => ( <> {section.header && <ListSubheader key={section.header}>{section.header}</ListSubheader>} {section.items ...

` Time Running Out: A Classy Countdown Timer`

Here are the lines of code I have included in my webpage - example/demo. HTML: <p class="countdown-timer">10:00</p> <p class="countdown-timer">10:00</p> JavaScript: I am looking for guidance as a beginner in JavaScript and JQue ...