手写 vue 点击图片弹窗放大

先看效果:
在这里插入图片描述

组件代码:

<template>
  <div class="transport-marker-pop" v-if="isShow">
    <div class="pop-container">
      <div class="head">
        <div class="title">图片</div>
        <div class="close" @click="hide"></div>
      </div>
      <div class="mod">
        <div class="image">
          <img width="100%" height="100%" :src="url"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "MarkerPop",
  data() {
    return {
      isShow: false,
      url: "",
    };
  },
  methods: {
    // 打开弹窗
    show(item) {
      this.url = item;
      this.isShow = true;
    },
    // 关闭弹窗
    hide() {
      this.isShow = false;
    },
  },

};
</script>

<style lang="less" scoped>
.transport-marker-pop {
  .pop-container {
    width: 800px;
    height: 400px;
    background-image: url("./assets/video-bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-position: center;
    position: fixed;
    top: 290px;
    right: 585px;
    z-index: 120;
    animation: showModScale 0.3s linear;
    -webkit-animation: showModScale 0.3s linear;
    word-break: break-all;
  }
  .head {
    display: flex;
    justify-content: space-between;

    width: 96%;
    align-items: center;
    line-height: 40px;
    margin: 5px auto;
    border-bottom: 1px solid #7d7d7d;
    .title {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #00dfff;
      margin-left: 390px;
    }
    .close {
      width: 14px;
      height: 14px;
      background-image: url("./assets/close.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center;
      cursor: pointer;
      margin-right: 13px;
    }
  }
  .mod {
    width: 96%;
    margin: 5px auto;
  }
}

@keyframes showModScale {
  0% {
    -webkit-transform: scale(0);
    transform: scale(0);
    opacity: 0;
  }
  100% {
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 1;
  }
}
</style>

使用方法:
也可以放到全局,自行选择。

<template>
  <div class="hello">
    <img :src="img" alt="" @click="openImg(img)" style="width:200px;height:200px">
    <img-pop ref="imgPop"></img-pop>
  </div>
</template>

<script>
import imgPop from "./imgPop.vue";
export default {
  name: 'HelloWorld',
  components: {imgPop},
  data() {
    return {
      img: "https://img0.baidu.com/it/u=1514002029,2035215441&fm=26&fmt=auto&gp=0.jpg"
    }
  },
  methods: {
    openImg(url) {
      this.$refs.imgPop.show(url);
    },
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个功能,你可以使用第三方库来帮助你完成。以下是一个示例,使用了Vue.js和Draggable.js: 1. 首先,使用npm安装Draggable.js:`npm install draggable --save` 2. 在你的Vue组件中引入Draggable.js:`import Draggable from 'draggable'` 3. 在Vue组件的mounted()生命周期钩子函数中初始化Draggable.js: ```javascript mounted() { const that = this; Draggable.create(this.$refs.popup, { type: 'x,y', bounds: 'body', edgeResistance: 0.65, onClick: function() { that.$emit('popup-click'); } }); } ``` 在这个示例中,我们创建了一个可拖动的元素,它的范围限制为整个页面,水平和垂直方向都可以移动。`edgeResistance`属性用于控制拖放边缘的阻力,`onClick`回调函数用于在点击弹窗时触发Vue组件的自定义事件。 4. 为了实现弹窗放大和缩小功能,你可以使用Vue的动态样式绑定。例如,你可以添加一个按钮来触发放大和缩小: ```html <template> <div ref="popup" class="popup" :style="{ width: width + 'px', height: height + 'px' }"> <div class="popup-header"> <span class="popup-title">{{ title }}</span> <button class="popup-btn" @click="toggleSize">{{ icon }}</button> </div> <div class="popup-body"> <slot></slot> </div> </div> </template> <script> export default { data() { return { title: '弹窗', icon: '放大', width: 400, height: 300 } }, methods: { toggleSize() { if (this.width === 400) { this.width = 800; this.height = 600; this.icon = '缩小'; } else { this.width = 400; this.height = 300; this.icon = '放大'; } } } } </script> ``` 在这个示例中,我们添加了一个按钮,通过点击按钮来切换弹窗的大小。我们使用`data`属性来控制弹窗的宽度和高度,当点击按钮时,我们更新这些属性,从而实现弹窗放大和缩小。 综上所述,你可以使用上述步骤来实现Vue中的弹窗可以放大缩小拖放的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值