vue实现弹性布局,横向超出则可滚动,并且隐藏滚动条

代码:

<template>
<div >

  <div class='flexStyle'>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
    <span class='contentStyle'></span>
  </div>
  </div>
</template>
<script>
export default {
  data(){
    return{
      cj:1,
    }
   
  },
   methods:{
    }

}
</script>
<style>
.flexStyle{
  height:100px;
  display:flex;
  justify-content:space-between;
  /* 设置超出滚动 */
  overflow-x:auto;
}
.contentStyle{
  width:50px;
  height:50px;
  display:inline-block;
  background:#a3a2a2;
  margin-right:5px;
  /* 超出滚动的关键,没有它元素会自动缩小,不会滚动 */
  flex-shrink: 0;
}
::-webkit-scrollbar {
  /* 隐藏滚动条 */
  display: none;
}
</style>

效果:
弹性布局左右滑动无滚动条.gif

复习弹性布局的各个属性:
justify-content:默认值flex-start

/* Positional alignment */
justify-content: center;     /* 居中排列 */
justify-content: start;      /* Pack items from the start */
justify-content: end;        /* Pack items from the end */
justify-content: flex-start; /* 从行首起始位置开始排列 */
justify-content: flex-end;   /* 从行尾位置开始排列 */
justify-content: left;       /* Pack items from the left */
justify-content: right;      /* Pack items from the right */

/* Baseline alignment */
justify-content: baseline;
justify-content: first baseline;
justify-content: last baseline;

/* Distributed alignment */
justify-content: space-between;  /* 均匀排列每个元素
                                   首个元素放置于起点,末尾元素放置于终点 */
justify-content: space-around;  /* 均匀排列每个元素
                                   每个元素周围分配相同的空间 */
justify-content: space-evenly;  /* 均匀排列每个元素
                                   每个元素之间的间隔相等 */
justify-content: stretch;       /* 均匀排列每个元素
                                   'auto'-sized 的元素会被拉伸以适应容器的大小 */

/* Overflow alignment */
justify-content: safe center;
justify-content: unsafe center;

/* Global values */
justify-content: inherit;
justify-content: initial;
justify-content: unset;

flex-shrink:该属性指定了 flex 元素的收缩规则,默认值为1。flex 元素仅在默认宽度之和大于容器的时候才会发生收缩,其收缩的大小是依据 flex-shrink 的值。flex-shrink值越大,收缩程度越高,值为0不收缩

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容