js实现点击图片在弹窗中放大

提示:本文是在前文的基础之上进行的修改


前言

 前文实现效果(被骂了,说要在弹窗中实现,而不是单纯的放大,影响界面美观)
前文链接:https://blog.csdn.net/weixin_46300114/article/details/131897239


1:首先,在HTML文件中创建一个空的弹窗容器,用于展示图片和关闭按钮。可以使用一个<div>元素,并给它一个唯一的ID,例如"imagePopup"

js代码如下(示例):

// 显示弹窗
function toggleImageSize(url) {
    var popup = document.getElementById('imagePopup');
    var image = popup.getElementsByTagName('img')[0];
    image.src = url;
    popup.style.display = 'block';
}

//关闭弹窗
function closePopup() {
    var popup = document.getElementById('imagePopup');
    popup.style.display = 'none';
}

2:修改原来的代码,将点击事件绑定到添加了参数的toggleImageSize方法上,并将图片的URL作为参数传递进去。

{
    field: 'certAddress1',
    title: '证件正面',
    formatter: function(value, row, index) {
        return '<img src="' + row.certAddress1 + '" onclick="toggleImageSize(\'' + row.certAddress1 + '\');" style="width: 1in; height: 1in;">';
    }
}

 

3:在弹窗容器中添加展示图片的<img>元素和关闭按钮。 

 html代码如下(示例):

<!--            弹窗容器-->
            <div id="imagePopup" style="display: none; position: fixed; top: 40%; left: 50%; transform: translate(-50%, -50%);">
                <img id="popupImage" src="" style="width: 6in; height: 6in;">
                <span id="closeButton" onclick="closePopup();" style="position: absolute; top: 0; right: 0; background-color: #fff; color:black; padding: 5px;"><strong>X</strong></span>
            </div>

2.实现效果

点击一下:


 点击右上角的“X”可以关闭弹窗

总结

作为一名入行1年的初初初级java程序员前端是真的很差(以前只负责写接口不用写前端,现在前端老哥跑路了,公司暂时也没有招募前端人员)只能自己慢慢摸索,这两天有时间将这个小功能记录一下(PS:公司用的若依前后端不分离框架进行二次开发)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值