- var i = 1;
- var n = 1;
.container
while i < 4
.hs__wrapper
.hs__header
h2.hs__headline Headline
= i++
.hs__arrows
a.arrow.disabled.arrow-prev
a.arrow.arrow-next
ul.hs
while n < 21
li.hs__item
.hs__item__image__wrapper
//img.hs__item__image(src="https://source.unsplash.com/random/300×300/?album&sig"+[n]+[i], alt="")
img.hs__item__image(src="https://picsum.photos/id/"+1+[n]+[i]+"/300/300", alt="")
.hs__item__description
span.hs__item__title
| Amazing title
= n++
span.hs__item__subtitle
| some subtitle
-n = 0
.description
h3 Description
p
strong Work in progress –
| Horizontal scroll containers are hyped right now. Whether you browse the Appstore, search for articles on Medium or set some filters in Instagram. Many apps use this kind of pattern. But most of them are very touch and mobile oriented. So I wondered how to make it nice on touch AND desktop.
p Depending on your desktop layout, you probably don't want the cards to run over the edge of the screen. Let's take a look at the "Home" section of Spotify. On the desktop, they show 4 to 6 cards in a slider with arrows at the top. And with touch/mobile they use the horizontal overflow scrolling.
p So let's recreate exactly this behaviour for responsive web use.
p Things already achieved:
ol
li 4 column grid on desktop. Tease the 4th on touch.
li Hide arrows on touch
li Disable corresponding arrow when end or start has been reached
li (As suggested) - Toggle arrow state on drag and scroll as well
li Always keep left and right spacings
li No ugly scrollbars on any browser
li Horizontal scrolling, dragging or arrow-click
li Multiple instances
li CSS Grid and CSS Vars are nice, but let's make this work on IE11 as well
p Todo / open (feel free to left suggestions in the comments)
ol
li If you click an arrow after box has been dragged / scrolled, it is not snapping to the grid anymore.
li Get rid of jQuery
p
strong BONUS –
| I made another alternative version here:
a(href="https://codepen.io/kilianso/pen/WqdVLx?editors=1010" target="_blank") https://codepen.io/kilianso/pen/WqdVLx
View Compiled
$gutter: 10px;
$bg: white;
$dark: #212121;
$darkest: #121212;
$green: #1cff65;
$maxwidth: 990px;
$colsBigDesktop: 4;
$colsSmallDesktop: 3;
* {
box-sizing: border-box;
}
::-webkit-scrollbar { /* Webkit */
width: 0;
height: 0;
}
.hs {
display: flex;
overflow-x: scroll;
justify-content: space-between;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE 10+ */
-webkit-overflow-scrolling: touch;
margin: 0 -20px;
&__header {
display: flex;
align-items: center;
width: 100%;
}
&__headline {
flex: 1;
}
&__arrows {
align-self: center;
.arrow {
&:before {
content: '';
display: inline-block;
vertical-align: middle;
content: "";
background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNSIgaGVpZ2h0PSI5IiB2aWV3Qm94PSIwIDAgMTUgOSI+Cgk8cGF0aCBmaWxsPSIjMzMzMzMzIiBkPSJNNy44NjcgOC41NzRsLTcuMjItNy4yMi43MDctLjcwOEw3Ljg2NyA3LjE2IDE0LjA1Ljk4bC43MDYuNzA3Ii8+Cjwvc3ZnPgo=");
background-size: contain;
filter: brightness(5);
width: 18px;
height: 12px;
cursor: pointer;
}
&.disabled:before {
filter: brightness(2);
}
&.arrow-prev:before {
transform: rotate(90deg);
margin-right: $gutter;
}
&.arrow-next:before {
transform: rotate(-90deg);
}
}
}
&__item {
flex-grow: 1;
flex-shrink: 0;
flex-basis: calc(100% / #{$colsBigDesktop} - (#{$gutter} * 2) - (20px / #{$colsBigDesktop}));
// how it works: total width devided by columns minus left and right margin per box minus negativ margin of outer container.
// oh and IE11 does not support the shorthand "flex" with calc, so you have to use flex-basis etc. separately;
margin: $gutter;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
user-select: none;
@media only screen and (max-width: 990px) {
flex-basis: calc(100% / #{$colsSmallDesktop} - (#{$gutter} * 2) - (20px / #{$colsSmallDesktop}));
}
&:last-child:after {
content: "";
display: block;
position: absolute;
width: $gutter;
height: 1px;
right: calc(#{$gutter} * 2 * -1);
}
&:first-child {
margin-left: calc(#{$gutter} * 2);
}
&__description {
z-index: 1;
align-self: flex-start;
margin: 10px 0;
}
&__subtitle {
color: #aaa;
display: block;
}
&__image__wrapper{
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%;
}
&__image {
pointer-events: none;
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}
}
&__wrapper {
@media only screen and (min-width: 990px) {
overflow: hidden;
}
@media (hover: none) and (pointer: coarse) {
.hs__arrows {
display: none;
}
.hs__item {
flex: 1 0 calc(23% - #{$gutter} * 2);
}
@media only screen and (max-width: 990px) {
.hs__item {
flex: 1 0 calc(45% - #{$gutter} * 2);
}
}
}
}
}
/* Base Styling */
body {
margin: 0;
padding: 0;
overflow-x: hidden;
font-family: 'Muli', sans-serif;
color: white;
background: $bg;
line-height: 1.5;
letter-spacing: .15px;
}
.container {
max-width: $maxwidth;
padding: calc(#{$gutter} * 2);
margin: 0 auto;
background: $darkest;
mix-blend-mode: invert;
position: relative;
&:after {
content: '';
width: 100vw;
height: 100%;
background: $dark;
position: absolute;
left: 50%;
top: 0;
transform: translateX(-50%);
z-index: -1;
}
@media only screen and (min-width: 990px) {
padding: calc(#{$gutter} * 4) calc(#{$gutter} * 8);
}
}
.description {
max-width: $maxwidth;
color: $dark;
margin: 0 auto;
padding: calc(#{$gutter} * 4);
}
ul {
padding: 0;
margin: 0;
}
View Compiled
// work in progress - needs some refactoring and will drop JQuery i promise :)
var instance = $(".hs__wrapper");
$.each( instance, function(key, value) {
var arrows = $(instance[key]).find(".arrow"),
prevArrow = arrows.filter('.arrow-prev'),
nextArrow = arrows.filter('.arrow-next'),
box = $(instance[key]).find(".hs"),
x = 0,
mx = 0,
maxScrollWidth = box[0].scrollWidth - (box[0].clientWidth / 2) - (box.width() / 2);
$(arrows).on('click', function() {
if ($(this).hasClass("arrow-next")) {
x = ((box.width() / 2)) + box.scrollLeft() - 10;
box.animate({
scrollLeft: x,
})
} else {
x = ((box.width() / 2)) - box.scrollLeft() -10;
box.animate({
scrollLeft: -x,
})
}
});
$(box).on({
mousemove: function(e) {
var mx2 = e.pageX - this.offsetLeft;
if(mx) this.scrollLeft = this.sx + mx - mx2;
},
mousedown: function(e) {
this.sx = this.scrollLeft;
mx = e.pageX - this.offsetLeft;
},
scroll: function() {
toggleArrows();
}
});
$(document).on("mouseup", function(){
mx = 0;
});
function toggleArrows() {
if(box.scrollLeft() > maxScrollWidth - 10) {
// disable next button when right end has reached
nextArrow.addClass('disabled');
} else if(box.scrollLeft() < 10) {
// disable prev button when left end has reached
prevArrow.addClass('disabled')
} else{
// both are enabled
nextArrow.removeClass('disabled');
prevArrow.removeClass('disabled');
}
}
});
This Pen doesn't use any external CSS resources.