{"id":267821,"date":"2018-04-09T06:00:46","date_gmt":"2018-04-09T13:00:46","guid":{"rendered":"http:\/\/css-tricks.com\/?p=267821"},"modified":"2018-09-30T00:33:13","modified_gmt":"2018-09-30T07:33:13","slug":"simple-swipe-with-vanilla-javascript","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/simple-swipe-with-vanilla-javascript\/","title":{"rendered":"Simple Swipe with Vanilla JavaScript"},"content":{"rendered":"
I used to think implementing swipe gestures had to be very difficult, but I have recently found myself in a situation where I had to do it and discovered the reality is nowhere near as gloomy as I had imagined.<\/p>\n
This article is going to take you, step by step, through the implementation with the least amount of code I could come up with. So, let’s jump right into it!<\/p>\n
<\/p>\n
We start off with a We use The fact that both the .container<\/code> that has a bunch of images inside:<\/p>\n
<div class='container'>\r\n <img src='img1.jpg' alt='image description'\/>\r\n ...\r\n<\/div><\/code><\/pre>\n
Basic Styles<\/h3>\n
display: flex<\/code> to make sure images go alongside each other with no spaces in between.
align-items: center<\/code> middle aligns them vertically. We make both the images and the container take the
width<\/code> of the container’s parent (the
body<\/code> in our case).<\/p>\n
.container {\r\n display: flex;\r\n align-items: center;\r\n width: 100%;\r\n \r\n img {\r\n min-width: 100%; \/* needed so Firefox doesn't make img shrink to fit *\/\r\n width: 100%; \/* can't take this out either as it breaks Chrome *\/\r\n }\r\n}<\/code><\/pre>\n
.container<\/code> and its child images have the same
width<\/code> makes these images spill out on the right side (as highlighted by the red outline) creating a horizontal scrollbar, but this is precisely what we want:<\/p>\n