Creating a dynamic number animation and generating content with Vue.js and SCSS

While exploring different methods for creating a number scrolling animation, I stumbled upon the following code snippet (written in SCSS and Vue):

new Vue({
  el: ".app",
  data: {
    number: 0
  },
  methods: {
    setNumber(e) {
      this.number = e.target.value;
    }
  },
  computed: {
    numberString() {
      return String(this.number).padStart(this.total, "0");
    }
  },
  created() {
    this.total = 9;
  }
});
* {
  margin: 0;
  padding: 0;
}
// CSS styles here
HTML elements and script tags here...

(Link to Original Codepen: https://codepen.io/lpjc/pen/MWpMxYa)

Though familiar with Vue and Sass basics, I found two parts of the code confusing:

  1. HTML: The usage of 'i' in the style binding section puzzled me. Where does it come from?
<div class="item__digit" :style="transform: `translateY(${Number(numberString.charAt(i - 1)) * -100}%)`">
  1. SCSS: I was unsure about the purpose of this section in relation to displaying changing numbers. How is the content updated? I know #{} signifies interpolation and $var indicates variables.
&-list {
      &:before {
        display: block;
        // SCSS styling explanation here
      }
    } 

Despite researching official documentation and online resources, I struggled to find detailed explanations on these specific queries.

Answer №1

1. The i variable originates from the Vue for loop.

Within Vue templates, loops and other logical operations can be defined using element properties known as Vue directives. A for loop specifically is implemented with the directive v-for. In the snippet of code provided in your query, the outermost element of the loop contains v-for="i in total".

<div class="item" v-for="i in total" :key="i">
    <div class="item__inner">
        <div class="item__digit" :style="{
            transform: `translateY(${Number(numberString.charAt(i - 1)) * -100}%)`
        }">
            <div class="item__digit-list"></div>
        </div>
    </div>
</div>

Consequently, within that specified element, the i variable is accessible for usage. To put it into perspective with regular JavaScript logic, it would resemble:

for ( const i in total ) {
    // This is where the i variable can be utilized
}

If you require further insights on the functionality of the v-for directive, refer to https://vuejs.org/guide/essentials/list

2. Utilization of SASS for generating a numerical list.

To visualize how the SCSS translates into CSS output, navigating to facilitates this process. Upon compilation, you will observe that the content attribute transforms into

content: "0\a  1\a  2\a  3\a  4\a  5\a  6\a  7\a  8\a  9\a  ";

(note that \a denotes a newline character).

With SASS, the expression #{$i} dynamically inserts the value of $i within that string. Rather than manually inputting each number like

"0\a  1\a  2\a  3\a  4\a  5\a  6\a  7\a  8\a  9\a  "
, they opted to implement a concise @for loop to achieve the same outcome. Through this loop, ranging from 0 to 9, each iteration appends the current number accordingly.

In practical terms, this results in a ::before pseudo-element housing a vertical enumeration from 0 to 9. The parent element conceals any overflow content, showcasing one digit at a time.

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

Customize the formatting of linked locale messages in Vue's internationalization (i18n) feature using parameters

Is there a way to link locale messages in vue-i18n with a parameter? { "next": "Next step {step+1}: @:steps[{step}]", "steps": [ "Welcome", // 0 "Briefing", // 1 "Finish" // 2 ...

React - The select component has received an invalid value of `undefined` that is out of range

I am working on a material-UI dialogue form that sends data to my API. One of the fields in the backend database is binary and only accepts two possible options. How can I handle this in the dialogue code provided below? Below is the error message: Mate ...

Tips on creating a responsive absolute div

I'm currently working on creating a profile component with Material UI and React.js. I'm having trouble making the Avatar/profile photo and profile name div responsive. Here are some screenshots to illustrate my issue: The specific div that need ...

Ways to align the label at the center of an MUI Textfield

My goal is to center the label and helper text in the middle of the Textfield. I managed to achieve this by adding padding to MuiInputLabel-root, but it's not responsive on different screen sizes (the Textfield changes size and the label loses its cen ...

Is there a way to ensure an AJAX call doesn't complete until my handlebar template has finished loading?

Currently, I am working with a handlebars template to display some information on my navigation bar. This information is retrieved from a Rails controller through an AJAX call. The issue I am facing is that since AJAX is asynchronous, it completes after th ...

Troubleshooting Problem with CSS and Javascript Dropdown Menu

Greetings, fellow web designers! I am relatively new to the world of web design and currently, I am immersed in the process of creating a website. My current project involves implementing a dropdown menu using CSS and Javascript. While I have made signific ...

What is the clarification on AngularJs' ng-options?

In my demo, I have a small setup. Essentially, it consists of a select element with the following data: address: { select: { code: "0", name: "Select proof of address" }, letter: { ...

"Creating a custom navigation bar with full width and navigation corners on both left

I am struggling to make my website's navbar stretch to the edges of the container while maintaining full width. I have added left and right padding to each navigation item, but in smaller laptop resolutions, the items break onto a new line, which is n ...

Issues with Css custom properties on certain webhosting platforms

Seeking assistance with a CSS custom properties issue. Recently purchased a web hosting service from Hostinger and using the default Wordpress template. Our web designer created a website in teleporthq.io, exported the files as HTML and CSS. Deleted the ...

What is the best way to add JSON data to a table?

I have developed a php script to create json data. However, I am facing an issue while trying to display this generated json data in a table. While my php code successfully generates the data, it is unable to insert it into the table. I would appreciate an ...

Error: In nodejs express, the reference to 'next' is not properly defined

My challenge lies in managing the input data received from a form using joi validation. I have implemented a middleware called validateCampground to validate the input, but encountered an issue when attempting to define the next function for the subsequent ...

Modal closes automatically after being clicked twice on data-bs-dismiss

Having an odd issue with Bootstrap 5.0.2 In order to close the modal, I have to double-click the button (data-bs-dismiss) It's worth noting that I use multiple modals on the page, but they only open when needed. <div class="modal" id="open-rat ...

Looking to streamline your design? Find out how to translate multiple elements to the same position seamlessly

Greetings! This is my debut post on this platform. I am currently engrossed in a project that has presented me with a challenge, and I'm reaching out to seek your insights on a potential solution. Here's the scenario: I have a parent div containi ...

Incorrect pathing in express.js

I've encountered an issue with my filter while attempting to redirect packages using two express.js routes: app.get('/billdetails/:year/:month/:phoneId', function (req, res, next) { var db = req.db; var year = req.params.year; v ...

Choosing specific child elements based on their css attribute valuesUnique: "Targeting

I am attempting to customize the appearance of any child divs with the class 'header' that are within parent divs with a class of 'recipes'. I understand that I could simply target .category-recipes .header, but the parent div may vary ...

Utilizing exceptions for specific objects in CSS

My table CSS includes the following code: .table-hover>tbody>tr:hover { background-color: #f36f25; color:#FFFFFF; } I am looking for a way to exclude specific rows from this hover effect. Is there a method to customize which rows trigger t ...

Tips for arranging imports in a Vue file

While Prettier formats my code, it does not organize the order of imports. In Go using VS Code, Gofmt automatically reorders imports in the correct order. How can I achieve a similar functionality for organizing Vue imports in VS Code? ...

What is the best way to define a recursive object type in TypeScript specifically for managing CSS styles?

I have implemented React.CSSProperties to generate css variables, allowing me to define styles like let css: React.CSSProperties = { position: "relative", display: "inline-block", background: "red" } My goal is to build a theme with css variables ...

Validating Input Textbox Content with JQuery Validation Plugin

I am currently utilizing the JQuery Validation plugin found at http://jqueryvalidation.org/ and encountering a specific scenario: Here is an example of the HTML code I am dealing with: <input type="text" name="glass_name" id="glass_name"> &l ...

Why am I struggling to properly install React?

C\Windows\System32\cmd.e X + Microsoft Windows [Version 10.0.22621.2715] (c) Microsoft Corporation. All rights reserved. C:\Users\user\Desktop\React>npx create-react-app employee_app npm ERR! code ENOENT npm ERR! s ...