Slide Elements & the Louisiana Black Bear
Published: 2022-03-03 06:04:19

Ever noticed those slideshows on a webpage that scroll over a set of photos or icons? A slide element is an animated HTML component that displays a set of things (photos, icons, anything) and scrolls through that content in a fixed place on the page. Sometimes there are buttons underneath that let you know how many options are in the set and may allow you to jump to a specific entry or preview what's coming up. 

In a quick search, I found 2 very different examples of slide animation in the first 3 websites I checked. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l670by8g4sxzs98nbqrf.png)

The LA State Parks website has photos that slide on a timer and can be previewed using the button at the bottom. Chase's homepage has five icons displayed at a time and more that can be shuffled through using the arrows left and right or the buttons at the bottom. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49avifvqds38z1cesmzz.png)


Our slide element will be composed of 7 photos of bears. To get started, we'll set up an HTML page with the images in a container div. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjdrw23azjjag5deknad.png)
 
I've sized each image to 500x300px and set them side-by-side without any spacing by using float: 'left'. The parent element is invisible and the size of all 7 images together, 3500px. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5erxq09nhowbs6f436fp.png)
 

At this point, we have 7 images, rendered next to each other from left to right, running off the page. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aat5wbu44hggz4ms1l81.png)
 

The next step to building our sliding animation is to create a second container around the existing one and set it's size to that of just one picture, and give it an overflow property 'hidden'. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nv8743k5ez4roexmuxdb.png)

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzptqfxo7weawg9xorks.png)
 


The overflow: 'hidden' property will ensure that only one picture shows at a time. Now only the first picture is visible. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldfs8l3f2763hwxrcjb5.png)



Next, we'll add the buttons at the bottom by creating a list of item links with a custom data attribute, data-pos, for position. Assign it the following positions. Can you guess why we've chosen those positions?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ge9te018ioxex5t5oaoq.png)
 

Style the list so the bullets display horizontally beneath the picture to resembles buttons. Add hover and select properties that change the bullets' color when you interact. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9yc7iuf169zmsil95fq.png)
 

This is looking very close to our final product. All that remains is the animation. To move forward from here we’ll need Javascript. We need to display another image from our strip of pictures, we’re going to query the DOM and set up event listeners in the script tag. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6l8rppuq3fx4nd18ld8.png)
 


Then we’ll give our first image the class ‘active’ and create a function that changes which image has the active class label.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mxxzfnv4xa1rrmoorja.png)
 
 
 
The last piece of JS: add a function that changes the displayed image and highlights its corresponding button. That custom attribute, data-pos, will come in handy to change the slider position. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89omw9wl0rcd7u888pt5.png)
 

Finally we’ll make the changes scroll rather than cut from image to image by adding a transition tag to the CSS of the inner container. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19efvoiiaqms6vc68f5v.png)
 
Now we've got a scrolling slideshow animation!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l31smijowf82eqgmcxm4.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6ycqse8joih9uk4hg9f.png)


##*UPDATE##
Some of you call this type of element a "carousel." So the theme could have been.. bearousel?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpl957beyagz8nx319bj.jpg)


You're welcome.