DEV Community

Cover image for Slide Elements & the Louisiana Black Bear
Megan Grusenmeyer
Megan Grusenmeyer

Posted on • Edited on

Slide Elements & the Louisiana Black Bear

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

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

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

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

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

Image description

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

Image description

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

Image description

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

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

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

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

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

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

Now we've got a scrolling slideshow animation!

Image description

Image description

*UPDATE

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

Image description

You're welcome.

Top comments (0)