Vue implements left and right sliding effect example code

Vue implements left and right sliding effect example code

Preface

The effect problems used in personal actual development are summarized to facilitate future development and review. If it is also applicable to others, please feel free to take it away without criticizing!

Vue.js is one of the popular JS frameworks. Vue is a progressive JavaScript framework for building user interfaces. Unlike other large frameworks, Vue is designed to be applied layer by layer from the bottom up. The core library of Vue only focuses on the view layer, which is not only easy to use, but also easy to integrate with third-party libraries or existing projects. On the other hand, when Vue is used in combination with modern tool chains and various supporting libraries, Vue is also fully capable of providing drivers for complex single-page applications.

Vue.js is a library for building interactive web interfaces. It provides MVVM data binding and a composable component system with a simple and flexible API. Technically speaking, vue.js focuses on the view model layer (viewModel) on the MVVM pattern and connects the view (view) and model (model) through two-way data binding. The actual DOM manipulation and output formatting are abstracted out into directives and filters. Compared with other MVVM frameworks, vue.js is easier to get started. It allows you to create data-driven UI components through a simple and flexible API.

Example Code

HTML Code

<template>
    <div id="SlideBar" class="box">
        <div class="item" ref="slide" :style="slideStyle" @touchstart="start($event)" @touchmove="move($event)" @touchend="end($event)">
          <img src="http://img2.imgtn.bdimg.com/it/u=2555191195,2735808065&fm=26&gp=0.jpg" alt="">
          <div class="right">
            <div class="title">Hello!</div>
            <p class="text">Hahahaha</p>
            <p class="price">Good</p>
          </div>
        </div>
        <div class="btn" ref="btn">
            <button>Edit</button>
            <button style="background:#387ef5;color:#fff">Collect</button>
        </div>
    </div>
</template>

CSS Code

<style>
.box{
    position:relative;
    border-bottom:0.026667rem solid #666666;
}
.btn{
  height:100%;
  position:absolute;
  right:0;
  top:0;
  background:red;
  display:flex;
}
button{
  width:1.6rem;
  height:100%;
  background:#f8f8f8;
  border:none;
}
.item{
  padding:0.266667rem;
  display:flex;
  position:relative;
  background:#fff;
  z-index: 2;
  box-shadow: 0.026667rem 0 0.053333rem #ddd;
}
.item img{
  width:2.133333rem;
  height:2.133333rem;
  margin-right:0.4rem;
  border-radius: 0.133333rem;
}

.item .title{
  font-size:0.48rem;
  float: left;
}
.item .text{
  font-size:0.426667rem;
  color:#888;
  float: left;
  margin: 0 1.33rem;
}
.item .price{
  color:#888;
  float: left;
  margin: 0 1.33rem;
}
</style>

JS code

<script>
    export default {
        name: 'SlideBar',
        props: {

        },
        data (){
            return {
                flag: false,
                startX: 0,
                endX: 0,
                slideStyle: {
                    left: 0,
                    transition: 'none'
                }
            }
        },
        methods: {
            start (e){ //Record the X-axis position of the screen when sliding starts this.flag = true;
                this.startX = e.touches[0].clientX;
                this.endX = this.$refs.slide.offsetLeft;
                this.slideStyle.transition = 'none';
            },
            move (e){
                if(this.flag){
                    // Logic for handling mouse movement var moveX = this.endX + (e.touches[0].clientX - this.startX); //Calculate the sliding distance if (Math.abs(moveX) >= this.$refs.btn.offsetWidth && moveX < 0) { //Judge whether the sliding distance is greater than the width of class:btn moveX = (Math.abs(moveX) - this.$refs.btn.offsetWidth) * 0.1; // 0.3 resistance coefficient this.slideStyle.left = - this.$refs.btn.offsetWidth - moveX + 'px';
                    }else if(moveX >= 0){ //Is the sliding distance greater than or equal to 0?
                        this.slideStyle.left = 0 + 'px'; //greater than or equal to 0 to make class:item equal to 0
                    }else{
                        this.slideStyle.left = moveX + 'px'; //Less than 0 makes class:item equal to the sliding distance}
                }
            },
            end (e){
                if(this.flag){
                    this.flag = false;
                    // endX = slide.offsetLeft;
                    var moveX = e.changedTouches[0].clientX - this.startX; //Calculate the sliding distance this.slideStyle.transition = 'left .3s';
                    var btnWidth = this.$refs.btn.offsetWidth; //class:btn widthif(moveX < 0){
                        if(Math.abs(moveX) >= btnWidth / 2 || Math.abs(this.$refs.slide.offsetLeft) >= this.$refs.btn.offsetWidth){ //Is it greater than half the width of class:btn this.slideStyle.left = - btnWidth + 'px'; //If you slide left more than half the width of class:btn, slide back}else if(Math.abs(moveX) < btnWidth / 2){ //Small than half the width of class:btn this.slideStyle.left = 0 + 'px'; //If you slide left without exceeding half the width of class:btn, return to the original position}
                    }else if(moveX > 0 && this.endX != 0){
                        if (Math.abs(moveX) >= btnWidth / 2) {
                            this.slideStyle.left = 0 + 'px'; //Slide right more than half of the width of class:btn and slide back}else if(Math.abs(moveX) < btnWidth / 2){
                            this.slideStyle.left = - btnWidth + 'px'; //Slide right without exceeding half of class:btn width and return to original position}
                    }
                }
            }
        },
        mounted (){
            var _this = this;
            // Use js's modern event listener transition transition ends this.$refs.slide.addEventListener('transitionend',function(){
                _this.endX = this.offsetLeft;
            })
        }
    }
</script>

Summarize

This is the end of this article about how to achieve left and right sliding effects with Vue. For more relevant Vue left and right sliding content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements top left and right sliding navigation
  • Vue implements tab navigation bar and supports left and right sliding function
  • Vue+swiper implements the test question function of sliding left and right
  • Vue uses swiper to switch pictures by sliding left and right
  • Vue uses better-scroll to achieve sliding and left-right linkage
  • Detailed explanation of left and right sliding events on Vue mobile terminal
  • Vue mobile terminal realizes the mobile phone sliding left and right entry animation
  • How to realize left and right sliding effect on mobile terminal in Vue
  • Implementing left and right sliding effect of page switching based on Vue
  • Detailed explanation of the use of Vue's left and right sliding button group component

<<:  Detailed explanation of how to install MySQL on Alibaba Cloud

>>:  Implementation of Redis one master, two slaves and three sentinels based on Docker

Recommend

Pure CSS3 code to implement a running clock

Operation effectCode Implementation html <div ...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

MySQL subqueries and grouped queries

Table of contents Overview Subqueries Subquery Cl...

Solution to the problem that the docker container cannot be stopped

The solution is as follows: 1. Force delete conta...

IE6 distortion problem

question: <input type="hidden" name=...

MySQL variable declaration and stored procedure analysis

Declaring variables Setting Global Variables set ...

Detailed explanation of the use of MySQL group links

Grouping and linking in MYSQL are the two most co...

Differences and comparisons of storage engines in MySQL

MyISAM storage engine MyISAM is based on the ISAM...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

How to import js configuration file on Vue server

Table of contents background accomplish Supplemen...