我需要使用仅HTML和CSS的水平按钮滚动条,像这样的滚动条:
浏览互联网寻求解决方案后,我的代码如下:
.myBtnContainer{
display: grid;
grid-gap: calc(var(--gutter) / 2);
grid-template-columns: repeat(6, calc(50% - var(--gutter) * 2));
grid-template-rows: minmax(150px, 1fr);
overflow-x: scroll;
scroll-snap-points-x: repeat(100vh);
scroll-snap-type: x mandatory;
}
.btn{
position:relative;
color: lightgrey;
text-transform: uppercase;
background: white;
padding: 1em;
border: 0.5em solid lightgrey;
border-radius: 6px;
display:inline-block;
transition: all 0.3s ease 0s;
scroll-snap-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.btn:hover{
color: grey;
border-radius: 50px;
border-color: grey;
transition: all 0.3s ease 0s;
}
<div id="myBtnContainer">
<button class="btn active" onclick="filterSelection('all')"> Show all</button>
<button class="btn" onclick="filterSelection('nature')"> American</button>
<button class="btn" onclick="filterSelection('cars')"> Italian</button>
<button class="btn" onclick="filterSelection('people')"> Asian</button>
<button class="btn" onclick="filterSelection('people')"> Mexican</button>
<button class="btn" onclick="filterSelection('people')"> Romanian</button>
<button class="btn" onclick="filterSelection('people')"> Barbeque</button>
<button class="btn" onclick="filterSelection('people')"> Indian</button>
<button class="btn" onclick="filterSelection('people')"> Chinese</button>
<button class="btn" onclick="filterSelection('people')"> Mediterranean</button>
<button class="btn" onclick="filterSelection('people')">Desserts</button>
<button class="btn" onclick="filterSelection('people')"> Smoothies</button>
<button class="btn" onclick="filterSelection('people')"> Coffee</button>
</div>
myBtnContainer
在body
中。我在某处读到的内容会阻止scroll-snap-type: x mandatory;
正常工作,但我仍然不知道如何解决此问题。我对此很陌生,因此可以提出任何建议。
答案 0 :(得分:0)
$(document).ready(function() {
$('.b-slider').owlCarousel({
loop: true,
margin: 10,
nav: true,
responsive: {
0: {
items: 1
},
600: {
items: 3
},
1000: {
items: 3
}
}
})
});
.btn {
color: lightgrey;
text-transform: uppercase;
background: red;
padding: 1em;
border: 0.5em solid lightgrey;
border-radius: 6px;
display: inline-block;
transition: all 0.3s ease 0s;
scroll-snap-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width:50px;
height:50px;
}
.btn:hover {
color: black;
border-radius: 50px;
border-color: grey;
transition: all 0.3s ease 0s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
</head>
<body>
<div class="owl-carousel b-slider">
<div class="item">
<button class="btn" onclick="filterSelection('nature')"> American</button>
</div>
<div class="item">
<button class="btn" onclick="filterSelection('nature')"> American1</button>
</div>
<div class="item">
<button class="btn" onclick="filterSelection('nature')"> American2</button>
</div>
</div>
</body>
</html>
这应该做到。 滑块使用过的猫头鹰木马