Vue uses calculated properties to complete the production of dynamic sliders

Vue uses calculated properties to complete the production of dynamic sliders

Layout part:

<div id="slider">
        <!-- Main animation effects: font, progress bar and expression change with the percentage of emotion level-->
        <label for="range" :style="{'color':getHappinessColor}">Emotional level: {{val}}%</label>
<!-- The color of the slider should be bound to the pre-set color, and the color can be changed at will-->
        <!-- The value of the emotion level should also change with the val value-->
        <input type="range" name="" id="range" min="0" max="100" v-model="val">
 
<!-- The value of the slider should be bound to val, which is written to the calculated property in order to synchronize with the range of the slider fill color below-->
        <div class="slider outer">
 <!-- Let's make the width of label equal to the width of our data center val, so that the label will move with the movement of the slider, achieving the effect of changing the expression-->
        <label for="" class="slider inner" :style="{'width':val+'%',
        'border-radius':greaterThanFifty">
                <span :style="{'right':getPlacement}">{{getHappiness}}</span>
            </label>
        </div>
    </div>


Style part:

*{
    padding: 0;
    margin: 0;
    list-style: none;
}
:root {
    /* Global CSS variables */
    --yellow: #ffd100;
    --orange: #ff6a13;
    --darkGray: #53565a;
    --midGray: #888b8d;
    --white: #fff;
  }
*,*::after,*::before{
    color: var(--darkGray);
    box-sizing: border-box;
}
html,body {
      width: 100%;
      height: 100%;
}
body{
      min-height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: var(--white);
}
#slider{
    /* Local CSS variables */
    --roundness: 20px;
    width: 100%;
    max-width: 600px;
    outline: 1px dashed red;
    padding: 4vh;
 
    /* Grid layout */
    display: grid;
    justify-content: stretch;
}
#slider>label{
    width: 100%;
    font-size: 1.5em;
    padding: 0 0 0.5em;
    margin: auto;
}
#slider input{
    grid-row: 2 / 3;
    grid-column: 1 / 2;
    width: 100%;
    position: relative;
    z-index: 1;
    opacity: 0;
    height: calc(var(--roundness) * 2);
    cursor: pointer;
}
#slider .outer{
    width: 100%;
    height: var(--roundness);
    background-color: var(--midGray);
    border-radius: var(--roundness);
    display: flex;
    align-items: center;
    align-content: center;
    position: relative;
    z-index: 0;
    margin: auto;
    grid-row: 2/3;
    grid-column: 1/2;
}
 
#slider input:focus + .outer {
    outline: 1px dashed var(--orange);
  }
 
#slider label.inner {
    position: absolute;
    width: 100%;
    height: 100%;
    background: var(--white);
    background: linear-gradient(to left, var(--yellow), var(--orange));
    border-top-left-radius: var(--roundness) !important;
    border-bottom-left-radius: var(--roundness) !important;
    position: absolute;
    transition: all 0.3s cubic-bezier(0.5, 0.4, 0.2, 1);
    text-align: right;
    font-size: calc(var(--roundness) * 2);
    line-height: 1;
  }
  #slider label.inner span {
    position: absolute;
    right: -2px;
    top: calc(50% - var(--roundness) * 2);
    top: calc(var(--roundness) * -.3);
    transition: inherit;
  }


Vue part:

<script src="./js/vue.js"></script>
    <script>
        let a = new Vue({
            el:"#slider",
            data() {
                return {
                    val: 70,
                    //Key points, also used to dynamically associate 1: emotion percentage, 2: displayed text expression}
            },
            mounted() {
                this.val = Math.floor(Math.random() * 101)
            },
            computed: {
        getPlacement: function () {
            return `${(-0.009 * ((this.val * -1) + 104))}em`;
            // Get the position. Because it is a calculated property, it is equivalent to binding to val. After binding to the bottom span, it is equal to the width bound to val above.
        },
        greaterThanFifty: function () {
            return this.val > 50 ? `var(--roundness)` : `0`;
            // When the val value is greater than 50, the border changes. It can be omitted or not bound. It is not critical.},
        getHappinessColor: function () {
            return `rgba(255, ${106 + (103 / 100 * this.val)}, ${(Math.floor(this.val * -1 / 7.692)) + 13}`;
            // Function to get color, you can change the value at will, but the color transition above is more natural},
        getHappiness: function () {
            let mood;
            // "Physically bind" the val value to all expressions
            if (this.val == 0) {
                mood = "đŸ¤Ŧ"
            } else if (this.val < 10) {
                mood = "😡"
            } else if (this.val < 20) {
                mood = "😠"
            } else if (this.val < 30) {
                mood = "đŸ˜Ļ"
            } else if (this.val < 40) {
                mood = "â˜šī¸"
            } else if (this.val < 50) {
                mood = "🙁"
            } else if (this.val < 60) {
                mood = "😐"
            } else if (this.val < 70) {
                mood = "🙂"
            } else if (this.val < 80) {
                mood = "😊"
            } else if (this.val < 90) {
                mood = "😄"
            } else if (this.val < 100) {
                mood = "😃"
            } else if (this.val == 100) {
                mood = "😍"
            }
            return mood;
        }
    }
        })
    </script>


Final style:

This is the end of this article about how to use calculated properties in Vue to create dynamic sliders. For more information about how to use calculated properties in Vue to create dynamic sliders, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue calculated property implementation transcript
  • A brief talk about calculated properties and property listening in Vue
  • Vue computed properties
  • Introduction to Computed Properties in Vue
  • Vue monitoring properties and calculated properties
  • Computed properties and data acquisition methods in Vue
  • Do you know Vue's computed properties?
  • Three implementation methods of Vue's calculated property name case

<<:  Introduction to the application of HTML tags superscript sup and subscript sub

>>:  Solution to VMware virtual machine no network

Recommend

Analysis of Context application scenarios in React

Context definition and purpose Context provides a...

Improving the effect of hyperlinks in web design and production

Hyperlinks enable people to jump instantly from pa...

How to deploy services in Windows Server 2016 (Graphic Tutorial)

introduction Sometimes, if there are a large numb...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

Analysis of the principle and usage of MySQL custom functions

This article uses examples to illustrate the prin...

Detailed explanation of Linux CPU load and CPU utilization

CPU Load and CPU Utilization Both of these can re...

Steps for docker container exit error code

Sometimes some docker containers exit after a per...

How to set up Spring Boot using Docker layered packaging

The Spring Boot project uses docker containers, j...

Problems with Vue imitating Bibibili's homepage

Engineering Structure The project is divided into...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

jQuery implements font size adjustment case

This article shares the specific code of jQuery t...

Solve the problem of running jupyter notebook on the server

Table of contents The server runs jupyter noteboo...

Examples of correct use of interface and type methods in TypeScript

Table of contents Preface interface type Appendix...

Use personalized search engines to find the personalized information you need

Many people now live on the Internet, and searchin...