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

A brief discussion on the use of GROUP BY and HAVING in SQL statements

Before introducing the GROUP BY and HAVING clause...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

About using Alibaba's iconfont vector icon in Vue

There are many import methods on the Internet, an...

Vue parent-child component mutual value transfer and call

Table of contents 1. Parent passes value to child...

Example of creating circular scrolling progress bar animation using CSS3

theme Today I will teach you how to create a circ...

How to redirect URL using nginx rewrite

I often need to change nginx configuration at wor...

Mysql database master-slave separation example code

introduce Setting up read-write separation for th...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

Web project development JS function anti-shake and throttling sample code

Table of contents Stabilization Introduction Anti...

Solution to the bug that IE6 select cannot be covered by div

Use div to create a mask or simulate a pop-up wind...

Alibaba Cloud domain name and IP binding steps and methods

1 Enter the Alibaba Cloud console, find the domai...

What are the benefits of semantic HTML structure?

one: 1. Semantic tags are just HTML, there is no ...

A brief discussion on Linux virtual memory

Table of contents origin Virtual Memory Paging an...

How to capture exceptions gracefully in React

Table of contents Preface ErrorBoundary Beyond Er...