Vue realizes the sliding cross effect of the ball

Vue realizes the sliding cross effect of the ball

This article example shares the specific code of Vue to achieve the effect of ball sliding and crossing for your reference. The specific content is as follows

Without further ado, let’s get straight to the code!

<template>
  <div class="about">
    <div class="box">
      <!-- Default Line -->
      <div class="Line"></div>
      <!-- Blue line -->
      <div class="slider-Line" ref="slider-Line"></div>
      <!-- Small ball on the left -->
      <div class="ball" @touchstart.prevent="fnstart"></div>
      <!-- Right ball-->
      <div class="ball ac" @touchstart.prevent="fnstart"></div>
      <!-- Numbers above -->
      <div class="num" ref="num">{{ num }}</div>
    </div>
  </div>
</template>

Script code:

<script>
export default {
  data() {
    return {
      num: 0,
    };
  },
  methods: {
    fnstart(ev) {
      //Ball this.oDiv = ev.target;
      // pagx: the distance from the mouse click position to the leftmost side of the page offsetLeft the left side of the current element to the leftmost side of the largest box this.pageX = ev.changedTouches[0].pageX - this.oDiv.offsetLeft;
 
      document.ontouchmove = this.FnMove;
      document.ontouchend = this.FnEnd;
      //Width of the parent element this.parent = ev.target.parentNode.offsetWidth;
      // Subtract the width of the ball this.Width = this.parent - ev.target.offsetWidth;
      //Get the parent element this.parents = ev.target.parentNode;
      //Get child elements this.child = this.parents.children;
 
      // Get the ball on the right this.Right = this.parents.children[0];
      // The ball on the left this.Left = this.parents.children[1];
    },
    FnMove(ev) {
      // Subtract the distance the ball slides from the leftmost side of the element to calculate the edge of the browser this.X = ev.changedTouches[0].pageX - this.pageX;
      // console.log(this.X, 1010101);
 
      // Check if the ball is equal to zero and cannot go out if (this.X <= 0) {
        this.X = 0;
      }
      // Check if it is greater than or equal to prevent the ball from going out if (this.X > this.Width) {
        this.X = this.Width;
      }
      // Let the ball on the left slide, and the line changes color accordingly // The value above changes accordingly, and is divided into 100 parts this.xnum = this.X / 3.7;
      // Get integer this.num = parseInt(this.xnum);
      this.$refs["num"].style.left = this.X + 6 + "px";
 
 
      // Let the balls intersect without affecting each other // Dynamically monitor the left and right for (var i = 0; i < this.child.length; i++) {
        if (this.child[i].classList.contains("ball")) {
          // A total of 5 elements minus 3 is the length of the blue line
          let Len = this.child.length - 3;
          if (i == Len) {
            // The absolute value of the left ball minus the right ball is the width of the line this.dis = Math.abs( this.child[i].offsetLeft - this.child[i + 1].offsetLeft );
            //The width of the ball this.child[1].style.width = this.dis + "px";
 
            // If the value of the left ball minus the right ball is less than 0, the left of the blue line is the offsetLeft value of the left ball if (this.child[i].offsetLeft - this.child[i + 1].offsetLeft < 0) {
              this.child[1].style.left = this.child[i].offsetLeft + "px";
            } else {
              // Otherwise, it is the offsetLeft value of the ball on the right this.child[1].style.left = this.child[i + 1].offsetLeft + "px";
            }
          }
        }
      }
 
 
      this.oDiv.style.left = this.X + "px";
    },
    FnEnd() {
      document.ontouchmove = null;
      document.ontouchend = null;
    },
  },
};
</script>

CSS code:

<style lang="less" scoped>
.box {
  position: relative;
  width: 400px;
  height: 30px;
  background-color: rgb(240, 16, 83);
  top: 50px;
  margin: auto;
  .ball {
    position: absolute;
    width: 30px;
    height: 30px;
    background-color: pink;
    border-radius: 50%;
    z-index: 2;
  }
  .ball.ac {
    right: 0;
    background-color: purple;
  }
  .Line {
    position: absolute;
    top: 14px;
    width: 400px;
    height: 2px;
    line-height: 30px;
    background: #ccc;
  }
  .slider-Line {
    position: absolute;
    top: 14px;
    width: 400px;
    height: 2px;
    line-height: 30px;
    background-color: blue;
  }
  .num {
    position: absolute;
    top: -19px;
    left: 9px;
  }
}
</style>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to realize left and right sliding effect on mobile terminal in Vue
  • Vue custom mobile touch events: click, slide, long press events
  • Implementing left and right sliding effect of page switching based on Vue
  • Example code of vue2.0 mobile sliding event vue-touch
  • Disable screen scrolling and sliding in vue
  • Vue implements sliding to the bottom to load more effects
  • Detailed explanation of left and right sliding events on Vue mobile terminal
  • Using better-scroll in vue to achieve sliding effect and precautions
  • Vue implements page switching sliding effect
  • Vue mobile terminal realizes the mobile phone sliding left and right entry animation

<<:  Detailed explanation of multiple implementation methods of Mysql counting by conditions

>>:  React implements double slider cross sliding

Recommend

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

Detailed explanation of MySQL clustered index and non-clustered index

1. Clustered Index Table data is stored in the or...

Build a server virtual machine in VMware Workstation Pro (graphic tutorial)

The VMware Workstation Pro version I use is: 1. F...

Basic usage knowledge points of mini programs (very comprehensive, recommended!)

Table of contents What to do when registering an ...

Mysql database index interview questions (basic programmer skills)

Table of contents introduction Indexing principle...

Guide to Efficient Use of MySQL Indexes

Preface I believe most people have used MySQL and...

Optimizing JavaScript and CSS to improve website performance

<br /> In the first and second parts, we int...

MySQL v5.7.18 decompression version installation detailed tutorial

Download MySQL https://dev.mysql.com/downloads/my...

How to modify the user and group of a file in Linux

In Linux, when a file is created, the owner of th...

Explain TypeScript enumeration types in detail

Table of contents 1. Digital Enumeration 2. Strin...

Introducing multiple custom fonts in CSS3

Today I found a problem in HTML. There are many d...

How to use webSocket to update real-time weather in Vue

Table of contents Preface About webSocket operati...