Element UI 走马灯高度自适应

Element UI走马灯中,通过属性height来设置高度,但是设置就是死的,不能自适应。要自适应需要监控窗口宽度的变化。

网上别人分享的太复杂了,这儿有简单的方法实现高度自适应。

首先,确定图片的最大高度

我的图片最大高度为270px,屏幕宽度超过container的宽度,这个值就不能再变化了。

首先计算图片高度和网站宽度的百分比,例如,内容区域最大宽度为1180px,图片高度为270px,270/1180约0.22

那么,只要宽度被改变,高度就变化为宽度的0.22倍即可

data中声明hdgd属性,作为高度变量

VUE添加mounted事件:

mounted() {
        let that = this;
        window.onresize = function windowResize() {
          // 通过捕获系统的onresize事件触发我们需要执行的事件
          var w = window.innerWidth
          var h = 270
          if (w > 1180) {
            h = 270
          } else {
            h = 0.22 * w
          }
          that.hdgd = h + 'px'
          console.log(that.hdgd)
        }
      }

添加el-carousel,:height="hdgd"属性

效果如下:

完整代码:

图片和vue以及element自行引入

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="lib/element-ui.css">
    <link rel="stylesheet" type="text/css" href="lib/css/index.css" />
  </head>
  <body>
    <div id="app">
      <header>Header</header>
      <el-container>
        <el-main>
          <el-row :gutter="10">
            <el-col :md="24">
              <el-carousel :type="cancard" indicator-position="outside" :height="hdgd">
                <el-carousel-item v-for="item in img" :key="item">
                  <img :src="'img/'+item+'.jpeg'" width="100%">
                </el-carousel-item>
              </el-carousel>
            </el-col>
            
          </el-row>
        </el-main>

      </el-container>
    </div>
  </body>
  <!-- import Vue before Element -->
  <script src="lib/vue.min.js"></script>
  <!-- import JavaScript -->
  <script src="lib/element-ui.js"></script>
  <script src="lib/template.js"></script>
  <script>
    var vue = new Vue({
      el: '#app',
      data: {
        show: true,
        hdgd: '270px',
        img: ['lb01', 'lb02', 'lb03']
      },
      computed: {
        cancard: () => {
          return ""
        }
      },
      methods: {},
      mounted() {
        let that = this;
        window.onresize = function windowResize() {
          // 通过捕获系统的onresize事件触发我们需要执行的事件
          var w = window.innerWidth
          var h = 270
          if (w > 1180) {
            h = 270
          } else {
            h = 0.22 * w
          }
          that.hdgd = h + 'px'
          console.log(that.hdgd)
        }
      }
    });
  </script>
</html>

 

上一篇 【VUE速成】2.模板语法
下一篇 【Electron教程01】跨平台桌面应用开发介绍
目录
applek

applek管理员

个人说明在个人中心里面设置

本月创作热力图

最新评论
hui
hui
8月19日
能否支持微信第三方登录啊?
Fiee
Fiee
7月21日
我网站基本上都装了,速度确实有所提升,还免费。我不喜欢白嫖,加你QQ没同意,网站也没有打赏通道 想支持一下的。
小超
小超
7月9日
下个版本考虑将下载地址加密(防止采集)
Lvtu
Lvtu
7月4日
qiang厉害
评论于留言建议
chinacnd
chinacnd
6月26日
这个主题是怎么做到加载速度那么快的?