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

网站模板设计教程程序员培训机构排名

网站模板设计教程,程序员培训机构排名,做我男朋友好不好网站,湖北网中建设工程有限公司图片叠加拖拽对比展示效果实现——Vue版 项目中遇见一个需求:2张图片按竖线分割,左右两侧分别展示对应图片,通过滚动条拖动对应展示图片区域;; 网上搜索了下,没有找到直接可用的组件,这里自己封装了一个次功…

图片叠加拖拽对比展示效果实现——Vue版

项目中遇见一个需求:2张图片按竖线分割,左右两侧分别展示对应图片,通过滚动条拖动对应展示图片区域;;
网上搜索了下,没有找到直接可用的组件,这里自己封装了一个次功能组件;后面会附上完整代码。,希望可以帮助到由此需求的小伙伴;

文章目录

  • 图片叠加拖拽对比展示效果实现——Vue版
    • 一、实现效果预览
    • 二、HTML 部分
      • 1. 组件包含:头部插槽、底部插槽、切换比较图片组
      • 2. 代码示例
    • 三、JS 部分
      • 1. 包含组件传参,左侧图片地址、右侧图片地址、事件处理等
      • 2. JS 部分代码如下:
    • 四、 CSS 样式代码
    • 六、完整使用示例

一、实现效果预览

在这里插入图片描述

二、HTML 部分

1. 组件包含:头部插槽、底部插槽、切换比较图片组

2. 代码示例

<template><div class="image-comparator"><!--  头部插槽  --><slot name="header"></slot><!--   切换对比图片  下一组、上一组 --><div class="prev" @click="prevFun"><i class="el-icon-arrow-left"></i></div><div class="next" @click="nextFun"><i class="el-icon-arrow-right"></i></div><!-- 核心标签代码   --><div class="container" @mouseenter="startDrag" @mousemove="onDrag" @mouseup="endDrag" @mouseleave="endDrag"><div class="image-wrapper":style="{ clipPath: `inset(0 ${100 - (dividerPosition / containerWidth * 100)}% 0 0)` }"><img src="https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg" class="image"/></div><div class="divider" :style="{ left: `${dividerPosition}px` }"></div><div class="image-wrapper" :style="{ clipPath: `inset(0 0 0 ${(dividerPosition / containerWidth * 100)}%)` }"><img src="https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg" class="image"/></div></div><!--  底部插槽  --><slot name="footer"></slot></div>
</template>

三、JS 部分

1. 包含组件传参,左侧图片地址、右侧图片地址、事件处理等

2. JS 部分代码如下:

<script>export default {name: 'imageComparator',props: {leftImgUrl: {type: String,},rightImgUrl: {type: String,},},data() {return {containerWidth: 500,dividerPosition: 250,dragging: false,containerRect: null,};},methods: {updateContainerWidth() {this.containerWidth = this.$el.querySelector('.container').clientWidth;},startDrag(event) {this.dragging = true;this.containerRect = this.$el.querySelector('.container').getBoundingClientRect();},onDrag(event) {if (this.dragging && this.containerRect) {let newPosition = event.clientX - this.containerRect.left;if (newPosition < 0) newPosition = 0;if (newPosition > this.containerRect.width) newPosition = this.containerRect.width;this.dividerPosition = newPosition;}},endDrag() {this.dragging = false;this.containerRect = null;},prevFun() {this.$emit('prevFun');},nextFun() {this.$emit('nextFun');},},mounted() {this.updateContainerWidth();window.addEventListener('resize', this.updateContainerWidth);},beforeDestroy() {window.removeEventListener('resize', this.updateContainerWidth);},};</script>

四、 CSS 样式代码

<style scoped lang="less">
.image-comparator {width: 90%;/*max-width: 600px;*/margin: 0 auto;.container {position: relative;width: 100%;height: 600px;overflow: hidden;}.image-wrapper {position: absolute;top: 0;left: 0;width: 100%;height: 100%;overflow: hidden;}.image {width: 100%;height: 100%;object-fit: cover;}.divider {position: absolute;top: 0;bottom: 0;width: 2px;background-color: black;cursor: ew-resize;z-index: 10;}.prev, .next {position: absolute;top: 50%;transform: translateY(-50%);z-index: 100;cursor: pointer;font-size: 40px;color: white;background-color: rgba(0, 0, 0, 0.5);padding: 10px;border-radius: 50%;transition: all 0.3s ease;left: 10px;&:hover {background-color: rgba(0, 0, 0, 0.8);color: #0bf4cb;}&.next {right: 10px;left: auto;}}
}</style>

## 五、总结

本文着重介绍了Vue2 封装的图片分割展示组件,如果小伙伴项目是V3 可以自行修改Vue 组件即可,还有写组件传参地方,需要的话自行改为动态参数即可,这里父组件不做过多介绍,以免浪费小伙伴看代码时间;

六、完整使用示例

以下是一个完整的 代码示例

<template><div class="image-comparator"><!--  头部插槽  --><slot name="header"></slot><!--   切换对比图片  下一组、上一组 --><div class="prev" @click="prevFun"><i class="el-icon-arrow-left"></i></div><div class="next" @click="nextFun"><i class="el-icon-arrow-right"></i></div><!-- 核心标签代码   --><div class="container" @mouseenter="startDrag" @mousemove="onDrag" @mouseup="endDrag" @mouseleave="endDrag"><div class="image-wrapper":style="{ clipPath: `inset(0 ${100 - (dividerPosition / containerWidth * 100)}% 0 0)` }"><img src="https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg" class="image"/></div><div class="divider" :style="{ left: `${dividerPosition}px` }"></div><div class="image-wrapper" :style="{ clipPath: `inset(0 0 0 ${(dividerPosition / containerWidth * 100)}%)` }"><img src="https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg" class="image"/></div></div><!--  底部插槽  --><slot name="footer"></slot></div>
</template><script>
export default {name: 'imageComparator',props: {leftImgUrl: {type: String,},rightImgUrl: {type: String,},},data() {return {containerWidth: 500,dividerPosition: 250,dragging: false,containerRect: null,};},methods: {updateContainerWidth() {this.containerWidth = this.$el.querySelector('.container').clientWidth;},startDrag(event) {this.dragging = true;this.containerRect = this.$el.querySelector('.container').getBoundingClientRect();},onDrag(event) {if (this.dragging && this.containerRect) {let newPosition = event.clientX - this.containerRect.left;if (newPosition < 0) newPosition = 0;if (newPosition > this.containerRect.width) newPosition = this.containerRect.width;this.dividerPosition = newPosition;}},endDrag() {this.dragging = false;this.containerRect = null;},prevFun() {this.$emit('prevFun');},nextFun() {this.$emit('nextFun');},},mounted() {this.updateContainerWidth();window.addEventListener('resize', this.updateContainerWidth);},beforeDestroy() {window.removeEventListener('resize', this.updateContainerWidth);},};
</script>
<style scoped lang="less">
.image-comparator {width: 90%;/*max-width: 600px;*/margin: 0 auto;.container {position: relative;width: 100%;height: 600px;overflow: hidden;}.image-wrapper {position: absolute;top: 0;left: 0;width: 100%;height: 100%;overflow: hidden;}.image {width: 100%;height: 100%;object-fit: cover;}.divider {position: absolute;top: 0;bottom: 0;width: 2px;background-color: black;cursor: ew-resize;z-index: 10;}.prev, .next {position: absolute;top: 50%;transform: translateY(-50%);z-index: 100;cursor: pointer;font-size: 40px;color: white;background-color: rgba(0, 0, 0, 0.5);padding: 10px;border-radius: 50%;transition: all 0.3s ease;left: 10px;&:hover {background-color: rgba(0, 0, 0, 0.8);color: #0bf4cb;}&.next {right: 10px;left: auto;}}
}</style>

看到这里的小伙伴,欢迎点赞、评论,收藏!

如有前端相关疑问,请评论区留言,博主会在第一时间解答!!!

http://www.khdw.cn/news/23283.html

相关文章:

  • 0元首充的手游平台襄阳seo培训
  • 深圳制作网站制作公司外贸高端网站设计公司
  • 中达建设网站进入百度搜索网站
  • 网页制作 公司网站优化培训学校
  • 做招投标网站如何做免费网站推广
  • 网站 制作价格企业网络推广的方法
  • 网站建设主机广东省广州市佛山市
  • 免费开源视频cms系统枫树seo
  • 数据来源于网站怎么做参考文献怎么找网站
  • 能够做一镜到底的网站资源搜索神器
  • 个人网站怎么备案可以做哪些成人零基础学电脑培训班
  • 郑州小程序开发公司排名百度有专做优化的没
  • 提供企业网站建设定制站长工具大全
  • wordpress RSS怎么用网站seo博客
  • 网站开发一般用什么技术seo什么意思简单来说
  • 佛山有那几家做网站淘宝付费推广有几种方式
  • 网站建设及优化重要性百度账号怎么注销
  • 南京制作网站速成班百度灰色关键词代发
  • 济南自助建站系统百度云网盘资源分享网站
  • 网站建设用户调查建站之星官方网站
  • 网站开发的技术支持360竞价推广客服电话
  • 外贸网站如何做推广搜索引擎技术基础
  • 望都网站建设网络服务提供者收集和使用个人信息应当符合的条件有
  • 网站上动画视频怎么做西昌seo快速排名
  • 阿里巴巴怎么做不花钱的网站自有品牌如何推广
  • 四川铁科建设监理有限公司官方网站百度网盘电脑版下载
  • wordpress 标签添加 图标的方法网站seo入门基础教程书籍
  • 营销神器官方网站网络推广有多少种方法
  • 专做定制旅游网站有哪些谷歌收录查询工具
  • 网站全面推广方案seo技术快速网站排名