gpt4 book ai didi

css - 如何使用 flexbox 制作水平滚动的轮播?

转载 作者:技术小花猫 更新时间:2023-10-29 11:01:31 25 4
gpt4 key购买 nike

我的目标:

像谷歌一样做一个水平滚动的轮播(见图片)

horizontal scrolling carousel

经过搜索,这些链接提供了一个良好的开端:

我以前从未使用过 flexbox。我的直觉是我需要设置 flex-direction:rowdisplay:inline-flex

关于我需要使用的其他 CSS 属性有什么想法吗?我真的很感激。

最佳答案

Flexbox 水平轮播:

flex 容器

  • 使容器显示:flex
  • 确保元素不换行:flex-wrap: nowrap;
  • 允许区域滚动:overflow: auto;
  • iOS 的势头和易用性:-webkit-overflow-scrolling 属性;
  • IE 10、11 和 Edge:-ms-overflow-style: -ms-autohiding-scrollbar;

    .container {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: -ms-autohiding-scrollbar;
    }

flex 元素

每个item都需要flex-growth和flex-shrink值为0,flex-based属性可以设置为auto:

.item {
flex: 0 0 auto;
}

我在这里发布了一个带有几个例子的片段。希望对您有所帮助!

.scroll {
display: flex;
flex-wrap: nowrap;
overflow: auto;
-webkit-overflow-scrolling: touch;
-ms-overflow-style: -ms-autohiding-scrollbar;
}

.flex {
display: flex;
flex-wrap: nowrap;
}

.logo {
flex: 0 0 120px;
}

.nav-item {
flex: 0 0 auto;
}

.example-three .logo {
display: block;
border: none;
}

.example-three .nav-item {
color: #fff;
}

header {
background: #281a41;
}

.example-one-header, .example-two-header {
border-radius: 3px;
}

.example-three-header {
border-radius: 3px 3px 0 0;
}

.example-three nav {
background: #727c87;
border-radius: 0 0 3px 3px;
}

.logo {
text-align: center;
font-weight: 700;
color: #727c87;
border-right: 1px solid rgba(114, 124, 135, 0.4);
padding: 13px 24px 12px;
}

.logo, .nav-item {
padding: 13px 16px 12px;
}
.logo:not(:last-child), .nav-item:not(:last-child) {
border-right: 1px solid rgba(114, 124, 135, 0.2);
}

* {
box-sizing: border-box;
}

body {
max-width: 360px;
margin: 5% auto;
color: #64cce3;
line-height: 1.5;
}

.logo, .nav-item {
font-size: 14px;
}

.title {
margin: 24px 0 6px;
font-size: 12px;
text-transform: uppercase;
letter-spacing: .2em;
color: #999;
}

::-webkit-scrollbar {
width: 12px;
}

::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px #76daff;
border-radius: 10px;
}

::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(255, 255, 255, 0.5);
}
<div class="container example-one">
<div class="title">Whole Section scroll</div>
<header class="example-one-header scroll">
<span class="logo">Company Logo</span>
<nav class="flex">
<span class="nav-item">Shop</span>
<span class="nav-item">Portfolio</span>
<span class="nav-item">Downloads</span>
<span class="nav-item">About Us</span>
<span class="nav-item">Contact Us</span>
</nav>
</header>
</div>
<div class="container example-two">
<div class="title">Inside Nav only scrolling</div>
<header class="example-two-header flex">
<span class="logo">Company Logo</span>
<nav class="scroll">
<span class="nav-item">Shop</span>
<span class="nav-item">Portfolio</span>
<span class="nav-item">Downloads</span>
<span class="nav-item">About Us</span>
<span class="nav-item">Contact Us</span>
</nav>
</header>
</div>
<div class="container example-three">
<div class="title">Separated navigation</div>
<header class="example-three-header">
<span class="logo">Company Logo</span>
</header>
<nav class="scroll">
<span class="nav-item">Shop</span>
<span class="nav-item">Portfolio</span>
<span class="nav-item">Downloads</span>
<span class="nav-item">About Us</span>
<span class="nav-item">Contact Us</span>
</nav>
</div>

你也可以看看 Andi Smith flexbox-carousel:

http://www.andismith.com/flexbox-carousel/

http://www.andismith.com/blog/2012/05/css3-flexbox-carousel/

关于css - 如何使用 flexbox 制作水平滚动的轮播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40559629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com