vue点击按钮弹窗显示图片

        需求是根据当前表格的行数据来获取对应的图片。然后我的行数据里已经有了id值,图片的名称也是id值。所以先通过按钮点击获取对应行的数据。这是写在表格里的按钮。

<template slot-scope="scope">
    <!-- scope.row可以拿到对应行的数据 -->
    <el-button
    type="primary" 
    icon="el-icon-search"
    @click="dialogVisible = true"
    @click.native.prevent="getrows(scope.row)"
    >
    查看
    </el-button>
</template>

         然后写弹窗,注意el-dialog不能写在template里面,不然会根据数据生成多个弹窗,一旦图片较长的时候,通过滑动条查看图片的时候会显示低层的弹窗,就会出现多出来的部分。

        这里我的src是自己图片的位置,图片按照1,2,3这样子的顺序来命名

 

<el-dialog
    title="帧图"
    :visible.sync="dialogVisible"
    :modal="false"
    append-to-body
    :width="dialogWidth"> 
    <!-- :modal="false"去掉遮蔽层 -->
    <!-- :width绑定宽度,来适应图片的宽度 -->
    <!-- 通过load事件来获取图片宽度,使得容器宽度适应图片 -->
    <!-- src里注意require里面用模板字符串 -->
    <el-image :src="require(`../../assets/${index}.jpg`)" 
    @load="onLoadImg" :width="boxWidth">
    </el-image>
    <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>
</el-dialog>

        然后在data里加入数据 ,这里仅展示有关数据,我的表格数据里是包含id值的。

export default {
    data(){
        return{
            index:1,    //默认设为1,因为我的图片没有0.jpg,所以如果设置为0为报错
            dialogWidth: "",    //弹窗宽度,可根据图片宽度进行改变
            boxWidth: "",    //图片宽度
            //其他数据里包含id值,这个可以自己根据数据来调整
        }
    },
    methods:{
        // 通过图片宽度来改变容器宽度,做到自适应,但是有待改善的地方。如果图片大小不一样.
        //不刷新页面的情况下,点击了宽度较小的图片后再点击宽度较大的图片,宽度会按照小的图片的宽度来显示。
        onLoadImg: function (e) {
            var img = e.target;
            var width = 0;
            if (img.fileSize > 0 || (img.width > 1 && img.height > 1)) {
                width = img.width;
            }
            // if ((img.height > img.width) &&  width > 370) {
            //     width = 370
            // } else if (width > 600) {
            //     width = 600
            // }
            this.boxWidth= width + 'px';
            this.dialogWidth = width + 40 + 'px';
        },
        //获取id值,并且将其赋值给index,从而获取对应的图片名字
        getrows(row){
            console.log(row.id)
            this.index=row.id
        },
    }
}

表格里左边显示图片帧号,帧号即id值,右边是可以点击显示帧图 

点击查看,横向的图片显示

竖线的图片显示

由于高度是自适应的,所以有些图片可能较长超出页面,需要拖动。

小图片显示:

 

 

 

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用Vue.js框架中的模态框组件来实现点击按钮弹窗的功能。以下是一个简单的示例: 1. 在你的Vue组件中,引入模态框组件: ``` <template> <div> <button @click="showModal = true">点击弹窗</button> <modal v-if="showModal" @close="showModal = false"> <h3>这是一个弹窗标题</h3> <p>这是弹窗的内容</p> </modal> </div> </template> <script> import Modal from './Modal.vue'; export default { components: { Modal }, data() { return { showModal: false } } } </script> ``` 2. 创建一个模态框组件Modal.vue: ``` <template> <div class="modal-mask"> <div class="modal-wrapper"> <div class="modal-container"> <div class="modal-header"> <slot name="header"></slot> <button class="modal-close-btn" @click="$emit('close')">关闭</button> </div> <div class="modal-body"> <slot></slot> </div> </div> </div> </div> </template> <script> export default { name: 'modal' } </script> <style> /* 模态框样式 */ .modal-mask { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 9998; } .modal-wrapper { display: table; width: 100%; height: 100%; } .modal-container { display: table-cell; vertical-align: middle; text-align: center; } .modal-header { margin-bottom: 10px; font-size: 18px; font-weight: bold; color: #333; } .modal-close-btn { position: absolute; top: 10px; right: 10px; padding: 5px 10px; border: none; background-color: #ddd; cursor: pointer; } .modal-body { padding: 20px; font-size: 16px; color: #666; } </style> ``` 3. 在你的CSS文件中添加以下样式来美化模态框: ``` /* 模态框样式 */ .modal-mask { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 9998; } .modal-wrapper { display: table; width: 100%; height: 100%; } .modal-container { display: table-cell; vertical-align: middle; text-align: center; } .modal-header { margin-bottom: 10px; font-size: 18px; font-weight: bold; color: #333; } .modal-close-btn { position: absolute; top: 10px; right: 10px; padding: 5px 10px; border: none; background-color: #ddd; cursor: pointer; } .modal-body { padding: 20px; font-size: 16px; color: #666; } ``` 4. 运行你的Vue应用程序,点击按钮就可以弹出模态框了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值