h5上下滑动动画效果(vue)

news/2025/2/4 23:24:32/

1.详情介绍

  1. 图片可以使用网络图片,根据请求过来的图片来获取高度要控制滑动的位置
  2. 可以换成视频,要实现滑动播放视频的效果,并且可以在上面添加一些其他的功能、
  3. 白色背景区域可以展示对应的数据
  4. 具体效果看文章末尾

2.编码介绍

template部分

<template><div class="share-code" ref="shares"><div class="swiper" id="box" ref="dragBox" @transitionend="animateEnd" @touchstart="touchstartHandle('dragBox',$event)" @touchmove="touchmoveHandle('dragBox',$event)" @touchend="toucendveHandle('dragBox',$event)"><div class="swipe-item swipe-item-1" :style="{height:imgHeight+'px'}" style="position: relative"><div class="img-item img-item-1" style="height:100%"><img ref="imgs" src="../../assets/haidao.png" alt="" @load='imgOnError'><!-- 可以换视频 --><!-- <video ref="videos" width="414" height="700" src=""></video> --></div><div class="list" v-show="pageY==0"><div class="titles">链接</div><div class="titles back">链接</div></div><div class="icons" @click="topClick" v-show="pageY!=0 && pageY!=2"><van-icon name="arrow-down" color="#fff" /></div><div ref="bgs" class="img-item img-item-1" :style="{height:imgHeight / 2 +'px'}" style="transition:all 0.5s"><img src="../../assets/haidao1.png" alt=""></div></div><div class="swipe-item" style="position: relative; top: -39px;z-index: 100;"><div class="img-item" :style="{height:imgHeight+'px'}"><img src="../../assets/share-bg-9.png" alt=""></div></div></div></div>
</template>

js部分

<script>
export default {name: "ShareCode",data() {return {showView: true,pageY: 1,startY: 0, // 手指开始触摸位置moveY: 0, // 手指移动距离flag: true,imgHeight: 700, //默认高度};},computed: {translateY() {// 计算图片ul当前距离if (this.pageY == 0) {return 0;} else if (this.pageY == 1) {return -(this.imgHeight / 2);} else if (this.pageY == 2) {return -550;}},},watch: {pageY(newNum) {if (newNum == 0) {this.$refs.dragBox.style.transform = `translateY(0px)`;// this.$refs.videos.play();} else if (newNum == 1) {this.$refs.dragBox.style.transform = `translateY(-${this.imgHeight / 2}px)`;// this.$refs.videos.pause();} else if (newNum == 2) {this.$refs.dragBox.style.transform = `translateY(-550px)`;// this.$refs.videos.pause();}},},methods: {animateEnd() {this.flag = true; // 打开节流阀if (this.pageY == 0) {this.$refs.bgs.style.opacity = 0;} else if (this.pageY == 1) {this.$refs.bgs.style.opacity = 1;}},//开始拖动touchstartHandle(refName, e) {console.log(1);// e.preventDefault();this.startY = e.targetTouches[0].clientY;},//正在拖动touchmoveHandle(refName, e) {e.preventDefault();// console.log(e.targetTouches[0].pageY);// 手指开始移动if (this.flag) {this.moveY = e.targetTouches[0].clientY - this.startY; // 手指移动位置if ((this.moveY < 0 && this.pageY == 2) ||(this.moveY > 0 && this.pageY == 0)) {return;}//根据滑动距离来实现渐变if (this.pageY == 1 && this.moveY > 0) {let opitey = 100 - this.moveY;this.$refs.bgs.style.opacity = opitey / 100;}this.$refs.dragBox.style.transition = "none"; // 图片ul跟随手指移动this.$refs.dragBox.style.transform = `translateY(${this.translateY + this.moveY}px)`;}},//结束拖动toucendveHandle(refName, e) {// 结束触摸if (this.flag) {if (this.moveY > 80) {// 移动距离大于80,图片索引--if (this.pageY == 0) {return;}this.pageY--;} else if (this.moveY < -80) {this.$refs.bgs.style.opacity = 1;// 移动距离小于-80,图片索引++if (this.pageY == 2) {this.$refs.bgs.style.opacity = 1;return;}this.pageY++;}this.$refs.dragBox.style.transition = "all .5s"; // 展示当前索引图片this.$refs.dragBox.style.transform = `translateY(${this.translateY}px)`;}},imgOnError(e) {//网络图片可动态计算高度给滑动距离进行调整console.log(this.$refs.imgs.height);this.imgHeight = this.$refs.imgs.height;初始化默认位置;this.$refs.dragBox.style.transform = `translateY(-${this.imgHeight / 2}px)`;},// 点击按钮下拉topClick() {this.pageY = 0;},},
};
</script>

css部分

<style scoped>
.share-code {height: 100vh;overflow-y: scroll;
}.list {position: absolute;left: 50%;transform: translateX(-50%);bottom: 7%;z-index: 101;transition: all 1s;
}
.icons {position: absolute;left: 50%;transform: translateX(-50%);bottom: 3%;z-index: 101;width: 55px;height: 30px;background: rgba(0, 0, 0, 0.5);border-radius: 30px;line-height: 30px;text-align: center;transition: all 1s;
}
.list .titles {width: 143px;height: 40px;line-height: 40px;text-align: center;border-radius: 20px;border: 1px solid #ffffff;color: #fff;font-size: 16px;
}
.list .titles:last-child {margin-top: 13px;background: rgba(0, 0, 0, 0.2);border: none;
}.imgs {position: absolute;top: 50%;transform: translateY(-50%);
}.img-item {width: 100vw;height: 100%;
}
.img-item img {width: 100%;height: 100%;
}
.swiper {position: relative;/* top: -13.33vw; */top: 0;left: 0;
}
.img-item-1 {width: 100%;/* height: 350px; */position: absolute;bottom: 0;background: #333333;
}
.swipe-item-1 {position: relative;height: 700px;
}
</style>

效果展示图
在这里插入图片描述
本篇文章就介绍到这里,如果想要学习更多vue系列知识,点击关注博主


http://www.ppmy.cn/news/764103.html

相关文章

android 开启屏幕固定,【玩机组】90%安卓用户都不知道的实用功能:屏幕固定

说到打游戏防止误触实体键这个场景,各位玩游戏的朋友一定觉得很实用。 如果刷了第三方的系统,导致没有这个功能了,或者买来的手机官方不支持这功能,此屏幕固定功能了,了解一下。 此外,可能还有这些场景: 1,家长把手机给小孩子看动画片、玩游戏,防止小孩脸滚键盘乱发微…

手机桌面左右滑屏不成功问题log分析

问题背景 滑动测试&#xff0c;单手握持测试机&#xff0c;大拇指在测试机桌面做左右滑屏操作&#xff0c;偶现滑屏不成功。 问题分析步骤 &#xff08;1&#xff09;首先根据“视频”或者“log中关于问题现象的关键log”确定“问题发生的大致时间段”和“问题发生时的具体操作…

adb控制手机屏幕滑动

前言&#xff1a; 这个用的是小米手机&#xff0c;在“开发者选项中”把 “USB调试”和**“USB调试&#xff08;安全设置**&#xff09;”两个都打开&#xff0c; 也可以把 指针位置 打开 来看触控点在屏幕的坐标&#xff1a; 2 adb 划动相册 其中 1.choice /t 1 /d y /n &…

全板子题G划 P2

【模板】最近公共祖先&#xff08;LCA&#xff09; 水温逐渐升高 #include<bits/stdc.h> using namespace std; const int N 5e5 10; int n,m,s; vector<int> ed[N]; int dp[N][21],dep[N];void dfs(int x,int fa){for(int i 1;i < 20;i)dp[x][i] dp[dp[x…

【移动端】滑动验证致使整个屏幕都在动

最近发现一款JQ图片滑动验证的代码。当手机端拖动按钮元素时&#xff0c;会触发手机浏览器自带事件&#xff0c;导致滑动失效或切屏&#xff0c;并且浏览器报错。 [Intervention] Unable to preventDefault inside passive event listener due to target being treated as pas…

移动端滑动验证为什么整个屏幕都在动?

开发手机移动端验证时&#xff0c;发现拖动元素会触发手机浏览器自带事件&#xff0c;导致滑动失效或切屏&#xff0c; 解决方法&#xff1a; html{touch-action:none;touch-action:pan-y; }var startX,startY;document.addEventListener("touchstart",function(e)…

cocos creator 划动屏幕以移动摄像机

本来代码是这么写的&#xff0c;在浏览器中运行也是好的。 cc.Class({extends: cc.Component,properties: {_begin: cc.Integer,_end: cc.Integer,_origin: cc.Integer,_camera: cc.Node,},onLoad () {// 找到摄像机this.camera cc.find("Canvas/Main Camera");// …

【第三章 flutter学习之Dart基础(上)】

文章目录 一、入口方法的定义方式二、Dart变量和常量三、Dart常用数据类型四、Dart运算符与类型转换及循环语句五、自定义方法六、Dart静态成员、操作符、类的继承七、接口八、接口分离写法九、一个类实现多个接口 一、入口方法的定义方式 main(){print(hello world) } //下边…