Every element has a back face, which is a mirror image of its front face. back-face are stacked up onto each other, because they are absolutely positioned. So, we got the card to 3D flip, yay! But why isn’t the card face showing up? Right now, both. Now, a transition has to be applied to the transform property to produce the movement effect: memory-card elements let’s add transform-style: preserve-3d, to position them in the 3D space created in the parent, instead of flattening it to the z = 0 plane ( transform-style). The lower the value the bigger the perspective effect. That property sets how far in the z plane the object is from the user. To produce the 3D flip effect, we will add the perspective property to. In the CSS the flip class rotates the card 180deg: The function accesses the element’s classList and toggles the flip class: ![]() The this variable represents the card that was clicked. Every time a card gets clicked flipCard function will be fired. Then loop through them with forEach and attach an event listener. For that, let’s select all memory-card elements with document.querySelectorAll. To flip the card when clicked, a class flip is added to the element. The :active pseudo class will be triggered every time the element gets clicked. The template should be looking like this: The property position: absolute set to both front-face and back-face, will remove the elements from the original position, and stack them on top of each other. memory-card children, let’s add position: relative so we can position the children absolutely, relative to it. Let’s make three rows, four card each by setting width to 25% and height to 33.333% minus 10px from margin. By setting flex-wrap to wrap, flex-items wrap along multiple lines, accordingly to their size.Įach card width and height is calculated with calc() CSS function. By default, the items are set to shrink in width to fit the container. memory-game will also be a flex-container. memory-game container, it will be centered both vertically and horizontally. The box-sizing: border-box property includes padding and border values into element’s total width and height, so we can skip the math.īy setting display: flex to the body and margin: auto to the. We will use a simple but yet very useful reset, applied to all items: The set of cards will be wrapped in a section container element. You can download the assets for this project at: Memory Game Repo. The first one represents the card front-face and the second its back-face. ![]() memory-card, which holds two img elements. Each card consists of a container div named. The initial template linking both css and js files. Let’s start creating the files in the terminal: ? mkdir memory-game ? cd memory-game ? touch index.html styles.css scripts.js ? mkdir img HTML If you know what HTML, CSS and JS are for, it’s more than enough! ![]() You are not expected to have much prior knowledge in programming. We will discuss data attribute, positioning, perspective, transitions, flexbox, event handling, timeouts and ternaries. This tutorial explains some basic HTML5, CSS3 and JavaScript concepts. By Marina Ferreira Memory Game in Vanilla JavaScript Learn JS, CSS and HTML by building a memory game in 30 minutes!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |