vue项目+element-ui 走马灯添加图片、图片自适应

上图为最终效果图

设置走马灯:

<template>
  <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in imagebox" :key="item.id">
        <img :src="item.idView" class="image">
      </el-carousel-item>
    </el-carousel>
</template>

<script>
  export default{
    name:'ZouMD',
    data(){
      return {
        imagebox:[

        {id:0,idView:require('../assets/imagebox/1.jpg')},
        {id:1,idView:require('../assets/imagebox/2.jpg')},
        {id:2,idView:require('../assets/imagebox/3.jpg')}
        //imagebox是assets下一个放图片的文件夹
        ]
      }
    }
  }
</script>

<style>
   .el-carousel__item h3 {
      color: #475669;
      font-size: 14px;
      opacity: 0.75;
      line-height: 200px;
      margin: 0;
    }

    .el-carousel__item:nth-child(2n) {
      background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n+1) {
      background-color: #d3dce6;
    }
</style>

 图片自适应:

<template>
  <!--动态将图片轮播图的容器高度设置成与图片一致-->
  <el-carousel :interval="4000" type="card" height="300px">
      <el-carousel-item v-for="item in imagebox" :key="item.id">
        <img :src="item.idView" class="image">
      </el-carousel-item>
    </el-carousel>
</template>

<script>
  export default{
    name:'zmd',
    data(){
      return {
        imagebox:[{id:0,idView:require('../assets/imagebox/1.jpg')},
        {id:1,idView:require('../assets/imagebox/2.jpg')},
        {id:2,idView:require('../assets/imagebox/3.jpg')}
        ],
       
        // 浏览器宽度
        screenWidth :0
      }
    },
     methods:{
              setSize:function () {
                // 通过浏览器宽度(图片宽度)计算高度
                this.bannerHeight = 400 / 1920 * this.screenWidth;
              },
            },
          mounted() {
              // 首次加载时,需要调用一次
              this.screenWidth =  window.innerWidth;
              this.setSize();
              // 窗口大小发生改变时,调用一次
              window.onresize = () =>{
              this.screenWidth =  window.innerWidth;
              this.setSize();
            }
          }
  }
</script>

<style>
   .el-carousel__item h3 {
      color: #475669;
      font-size: 14px;
      opacity: 0.75;
      line-height: 200px;
      margin: 0;
    }

    .el-carousel__item:nth-child(2n) {
      background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n+1) {
      background-color: #d3dce6;
    }
    .img{
     /*设置图片宽度和浏览器宽度一致*/
      width:100%;
      height:inherit;
    }
</style>

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以给你提供一个简单的代码示例,你可以根据自己的需求进行修改。 首先,你需要在你的Vue项目中安装Element-UI: ``` npm install element-ui ``` 然后,在你的Vue组件中引入Element-UI和CSS文件: ``` <template> <div> <el-upload class="upload-demo" action="//jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :on-success="handleUploadSuccess" :file-list="fileList" :auto-upload="false"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">jpg/png文件,不超过2MB</div> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt=""> </el-dialog> </div> </template> <script> import { ElUpload, ElButton, ElDialog } from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' export default { components: { ElUpload, ElButton, ElDialog }, data() { return { dialogImageUrl: '', dialogVisible: false, fileList: [ { name: 'food.jpeg', url: 'https://www.baidu.com/img/flexible/logo/pc/result.png' } ] } }, methods: { handleRemove(file, fileList) { console.log(file, fileList) }, handlePreview(file) { this.dialogImageUrl = file.url this.dialogVisible = true }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!') } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') } return isJPG && isLt2M }, handleUploadSuccess(response, file, fileList) { console.log(response, file, fileList) } } } </script> <style scoped> .upload-demo { display: inline-block; margin-bottom: 20px; } </style> ``` 在上面的代码中,我们使用了Element-UI中的ElUpload组件来上传图片,使用ElDialog组件来显示预览图。你可以根据需要修改上传的文件类型、大小等限制条件,以及自定义上传接口和成功回调函数。 希望这个代码示例能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值