当前位置: 首页 > news >正文

vue,uniapp 实现卷帘对比效果

需求:两张图重叠放在一起,拖动分割线实现卷帘对比效果,如图

在这里插入图片描述
在这里插入图片描述

一、vue2代码

<template><div class="main"><div class="img-comparison" @mousedown="startSlide"><img class="before" src="../assets/images/white/1.png" alt="Before" /><imgclass="after"src="../assets/images/white/2.png"alt="After":style="{clipPath:'polygon(0 0, ' +sliderPosition +'% 0, ' +sliderPosition +'% 100%, 0 100%)',}"/><div class="slider" :style="{ left: sliderPosition + '%' }"></div></div></div>
</template><script>
export default {data() {return {sliderPosition: 0, // 初始位置设为 0 (左侧)isSliding: false,};},methods: {startSlide(e) {this.isSliding = true;document.addEventListener("mousemove", this.moveSlider);document.addEventListener("mouseup", this.endSlide);this.moveSlider(e);// 禁止文本和图片选中document.body.style.userSelect = "none";},moveSlider(e) {if (!this.isSliding) return;const comparisonRect = e.target.closest(".img-comparison").getBoundingClientRect();const newPosition = e.clientX - comparisonRect.left;this.sliderPosition =(Math.max(0, Math.min(comparisonRect.width, newPosition)) /comparisonRect.width) *100;},endSlide() {this.isSliding = false;document.removeEventListener("mousemove", this.moveSlider);document.removeEventListener("mouseup", this.endSlide);// 恢复文本和图片的可选中行为document.body.style.userSelect = "";},},
};
</script><style lang="scss" scoped>
.main {display: flex;align-items: center;justify-content: center;height: 100vh;background-color: black;.img-comparison {position: relative;width: 100%;max-width: 600px;user-select: none; // 禁用选择img {width: 100%;height: 600px;pointer-events: none; // 禁止图片的鼠标事件,以避免选中}.after {position: absolute;top: 0;left: 0;transition: clip-path 0.3s ease; // 加快动画速度}.slider {position: absolute;top: 0;bottom: 0;width: 2px;background-color: rgba(255, 255, 255, 0.5);cursor: ew-resize;transition: left 0.3s ease; // 加快动画速度}}.img-comparison:hover {cursor: pointer;}
}
</style>    

二、uniapp代码

<template><view class="main"><view class="img-comparison" @touchstart="startSlide" @touchmove="moveSlider" @touchend="endSlide"><image class="before" :src="require('../../static/888.png')" mode="aspectFill" alt="Before"></image><imageclass="after":src="require('../../static/999.png')"mode="aspectFill":style="{clipPath:'polygon(0 0, ' +sliderPosition +'% 0, ' +sliderPosition +'% 100%, 0 100%)',}"alt="After"></image><view class="slider" :style="{ left: sliderPosition + '%' }"></view></view></view>
</template><script>
export default {data() {return {sliderPosition: 0, // 初始位置设为 0 (左侧)isSliding: false,};},methods: {startSlide(e) {this.isSliding = true;this.moveSlider(e);},moveSlider(e) {if (!this.isSliding) return;const query = uni.createSelectorQuery().in(this);query.select('.img-comparison').boundingClientRect((res) => {const newPosition = e.changedTouches[0].clientX - res.left;this.sliderPosition =(Math.max(0, Math.min(res.width, newPosition)) / res.width) * 100;}).exec();},endSlide() {this.isSliding = false;},},
};
</script><style lang="scss" scoped>
.main {display: flex;align-items: center;justify-content: center;height: 100vh;background-color: black;.img-comparison {position: relative;width: 100%;max-width: 600px;user-select: none; // 禁用选择image {width: 100%;height: 600px;pointer-events: none; // 禁止图片的鼠标事件,以避免选中}.after {position: absolute;top: 0;left: 0;transition: clip-path 0.3s ease; // 加快动画速度}.slider {position: absolute;top: 0;bottom: 0;width: 2px;background-color: rgba(255, 255, 255, 0.5);cursor: ew-resize;transition: left 0.3s ease; // 加快动画速度}}.img-comparison:hover {cursor: pointer;}
}
</style>    

文章转载自:

http://lDNd48LW.hrzhg.cn
http://jb57y2Nk.hrzhg.cn
http://dSuxqU10.hrzhg.cn
http://gRiR1hVt.hrzhg.cn
http://rlHP653O.hrzhg.cn
http://FHPLBaQ6.hrzhg.cn
http://CMP8Cg9l.hrzhg.cn
http://mm6zLsNR.hrzhg.cn
http://Kk0OAByj.hrzhg.cn
http://tWbO3FxT.hrzhg.cn
http://55mYmDjl.hrzhg.cn
http://NzLkYJ2F.hrzhg.cn
http://ANvy0i1q.hrzhg.cn
http://ZxnX3CKw.hrzhg.cn
http://Bohrut9o.hrzhg.cn
http://IwmjT7J7.hrzhg.cn
http://lietsqtp.hrzhg.cn
http://zF7B4oiD.hrzhg.cn
http://mi7K18yc.hrzhg.cn
http://6Ed0x0U6.hrzhg.cn
http://QLvqp2y1.hrzhg.cn
http://0Oy5M2aw.hrzhg.cn
http://PoDWACvD.hrzhg.cn
http://5gRwKv4Y.hrzhg.cn
http://D3sOJIsy.hrzhg.cn
http://7WfNXo4e.hrzhg.cn
http://f6N8nynx.hrzhg.cn
http://EkqW24OZ.hrzhg.cn
http://ZyEW55NI.hrzhg.cn
http://kA69a88k.hrzhg.cn
http://www.dtcms.com/a/380012.html

相关文章:

  • 鸿蒙 NEXT UI 性能优化实战:打造流畅用户界面的关键策略
  • 使用UniApp实现一个AI对话页面
  • 智能科技与搜索引擎优化关键词的新契机
  • 搜维尔科技:全身可穿戴Teslasuit动捕服的功能,自立式FES装置
  • Java 大视界 -- Java 大数据在智能医疗健康档案数据分析与个性化健康管理中的应用(410)
  • RK3588 Android12默认移除导航栏
  • HBuilder 运行编译器内存溢出
  • lesson59:JavaScript 控制流详解:分支结构与循环语句全指南
  • Avalonia 基础导航实现:从页面切换到响应式交互全指南
  • 【连载2】C# MVC 自定义错误页设计:404/500 处理与 SEO 优化
  • java jdbc连接sqlserver2008R2版本数据库报错,驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
  • 企业级AI大模型选型指南:从评估部署到安全实践
  • Spring Boot + Redis 缓存性能优化实战:从5秒到毫秒级的性能提升
  • 【Vue2手录09】购物车实战
  • 【论文阅读】Uncertainty Modeling for Out-of-Distribution Generalization (ICLR 2022)
  • PAT乙级_1111 对称日_Python_AC解法_无疑难点
  • Kafka面试精讲 Day 16:生产者性能优化策略
  • vue 批量自动引入并注册组件或路由
  • Kubernetes(K8s)详解
  • 趣味学solana(介绍)
  • Apache Thrift:跨语言服务开发的高性能RPC框架指南
  • Flutter 应用国际化 (i18n) 与本地化 (l10n) 完整指南
  • 第 5 篇:深入浅出学 Java 语言(JDK8 版)—— 精通类与对象进阶,掌握 Java 面向对象核心能力
  • Gin-Vue-Admin学习笔记
  • Golang關於信件的
  • The 2024 ICPC Asia East Continent Online Contest (I)
  • 【数所有因子和快速新解/范围亲密数/分解因式怎么去掉重复项】2022-10-31
  • SQL语句执行时间太慢,有什么优化措施?以及衍生的相关问题
  • 【论文阅读】Language-Guided Image Tokenization for Generation
  • PHP:从入门到实战的全方位指南