Blog

Find out how to Create an Emoji Ranking Slider With JavaScript

Emojis are a few of the mostly used icons on social apps at the moment. Whether or not you need to convey feelings or improve communication, there may be an emoji or a mixture of emojis to perform the duty. On this tutorial, we are going to construct a ranking emoji part that can permit the person to charge their stage of satisfaction through the use of a slider.

HTML Construction

The HTML Construction will include a container div factor with the next youngster parts:

  • A title and a subtitle
  • The picture factor to point out the present emoji
  • A div to point out the ranking content material.
  • A spread enter factor.

Styling With CSS

We are going to begin by making use of the next types to the physique to heart all our parts within the web page. 

Bricolage Grotesque is a font obtainable from Google fonts. And the colour I’ve chosen makes use of a slight transparency so  that when the background adjustments, the textual content colour will change too.

Lastly, you’ll see a transition property on the background, in order that after we change the colour it fades properly from one to the opposite.

1physique {
2 font-family: ‘Bricolage Grotesque’, sans-serif;
3 show: flex;
4 justify-content: heart;
5 align-items: heart;
6 flex-direction: column;
7 min-height: 100vh;
8 background: #fff;
9 colour: rgba(0,0,0,0.7);
10 transition: background .5s ease-in-out;
11}

For the container factor, apply the next types in order that the kid parts are centered and vertically stacked. 

1.container {
2 width: 300px;
3 padding: 0 10px;
4 background: #B4BED9;
5 border-radius: 20px;
6 padding: 40px 5px;
7 show: flex;
8 flex-direction: column;
9 align-items: heart;
10 text-align: heart;
11 justify-content: space-around;
12 place: relative;
13
14}

The title and subtitle are positioned on the prime of the container factor with the next types.

1.title {
2 font-size: 3rem;
3 font-weight: 800;
4 margin: 0 0 1rem 0;
5 line-height: .9;
6}
7.subtitle {
8 font-size: 1rem;
9 margin: 0;
10}

Customized Slider

The vary enter consists of a bar and a slider management factor by default. The slider management factor (the thumb) strikes alongside the horizontal bar, permitting customers to pick a particular worth. The enter vary additionally appears totally different relying on the browser it’s displayed on. Right here is the default vary enter on Chrome.

range inputrange input

As an alternative of utilizing the default slider above, we are going to customise it to look a lot better. Set -webkit-appearance: none; and look: none; on the vary enter. 

1enter[type=“range”] {
2 -webkit-appearance: none;
3 look: none;
4}

The impact of those types is to take away the default look in order that we will present our customized type. Set the next  extra types on the slider bar.

1enter[type=“range”] {
2 place: absolute;
3 prime: 85%;
4 -webkit-appearance: none;
5 look: none;
6 width: 80%;
7 background: #e5e5e5;
8 peak: 2px;
9 border-radius: 5px;
10 define: none;
11 box-shadow: #F2D2BD;
12}

The looks of the thumb varies throughout totally different browsers; therefore, we are going to apply particular person types based mostly on the browser to make the thumb constant throughout totally different browsers. The type  -webkit-appearance: none;  will take away the default thumb type supplied by the browser. Let’s  additionally apply a customized peak, width, padding, and background colour. 

1enter[type=“range”]::-webkit-slider-thumb {
2 -webkit-appearance: none;
3 padding: 5px;
4 peak: 5px;
5 width: 5px;
6 border:5px strong #fff;
7 border-radius: 50%;
8 cursor: pointer;
9 background: #4c4c4c;
10}
11
12/* FIREFOX */
13enter[type=“range”]::-moz-range-thumb {
14 padding: 5px;
15 peak: 5px;
16 width: 5px;
17 border:5px strong #fff;
18 border-radius: 50%;
19 cursor: pointer;
20 background: #4c4c4c;
21
22}

Set dimensions to the picture and add a bounce impact animation.

1@keyframes bounce {
2 0%, 100% {
3 remodel: translateY(0);
4 }
5 50% {
6 remodel: translateY(-10px);
7 }
8}
9
10img {
11 peak: 150px;
12 width: 150px;
13 define: none;
14 animation: dance 1s infinite;
15}

Lastly type the ranking content material div.

1.ranking {
2 font-size: 20px;
3 font-weight: 100;
4 width: 150px;
5}

JavaScript Performance

As the worth on the vary enter adjustments, we are going to use JavaScript to replace the emoji kind, the colour of the container, and the ranking message. 

I downloaded an excellent set of 100 SVG emojis from Envato Components, superb for this mission.

100 svg emojis100 svg emojis100 svg emojis

So, with my SVG icons chosen and uploaded, let’s get our parts and outline our information.

1const container = doc.querySelector(.container)
2const slider = doc.getElementById(slider);
3const emoji = doc.querySelector(.emoji);
4const charge = doc.getElementById(message);
5const colours = [#d35e65, #d3965c, #cad48a, #6ed494, #18c574];
6const emojis = [
7 {
8 text: Awful,
9 url: Disappointed.svg
10 },
11 {
12 text: Bad,
13 url: Sad.svg
14 },
15 {
16 text: Okay,
17 url: Expressionless.svg
18 },
19 {
20 text: Good,
21 url: Smile.svg
22 },
23 {
24 text: Great,
25 url: Love.svg
26 }
27];

The emojis array defines objects; every object comprises an emoji picture and textual content describing the emoji sentiment. We are going to use this information to dynamically show emojis based mostly on person interactions with the ranking part. 

Subsequent, create a perform known as UpdateRating(). Contained in the UpdateRating() perform, get the worth on the transferring slider.

1perform UpdateRating() {
2 const worth =slider.worth;
3 }

Subsequent, we are going to create if statements that can verify the worth on the slider and replace the emoji type, the background colour, and message relying on the worth on the slider. Low values will entice a low ranking, unhappy emojis, and damaging messages, whereas excessive values will entice a excessive ranking, optimistic messages, and optimistic emojis.

The slider spans from 0 to 100; we’ll due to this fact allocate every message a 20-unit interval.

Replace the UpdateRating perform as proven beneath.

1perform UpdateRating() {
2 const worth =slider.worth;
3
4 if (worth >= 0 && worth < 20) {
5 emoji.src = emojis[0].url
6 charge.textContent = emojis[0].textual content;
7 container.type.backgroundColor =colours[0];
8
9 } else if (worth >= 20 && worth < 40) {
10 emoji.src = emojis[1].url
11 charge.textContent = emojis[1].textual content;
12 container.type.backgroundColor =colours[1];
13
14 } else if (worth >= 40 && worth < 60) {
15 emoji.src = emojis[2].url
16 charge.textContent = emojis[2].textual content;
17 container.type.backgroundColor =colours[2];
18
19 } else if (worth >= 60 && worth < 80) {
20 emoji.src = emojis[3].url
21 charge.textContent = emojis[3].textual content;
22 container.type.backgroundColor = colours[3];
23
24 } else if (worth >= 80 && worth <= 100) {
25 emoji.src = emojis[4].url
26 charge.textContent = emojis[4].textual content;
27 container.type.backgroundColor = colours[4];
28 }}

Let’s refactor our conditional logic to make sure the code just isn’t repetitive. Create a perform known as setProperties(), which takes index as an argument . Inside setProperties(),  add the repetitive logic.

1perform setProperties(index){
2 emoji.src = emojis[index].url
3 charge.textContent = emojis[index].textual content;
4 container.type.backgroundColor =colours[index];
5 }

For every situation, name the setProperties() perform like this:

1perform UpdateRating() {
2 const worth =slider.worth;
3
4 if (worth >= 0 && worth < 20) {
5 setProperties(0)
6
7 } else if (worth >= 20 && worth < 40) {
8 setProperties(1)
9
10 } else if (worth >= 40 && worth < 60) {
11 setProperties(2)
12
13 } else if (worth >= 60 && worth < 80) {
14 setProperties(3)
15
16 } else if (worth >= 80 && worth <= 100) {
17 setProperties(4)
18 }}

Lastly, we have to add an occasion listener to the enter vary. The occasion listener will hear for the oninput occasion after which name the UpdateRating() perform. An oninput occasion happens when the worth of an enter factor is modified.Concerning the enter vary, the oninput occasion will present real-time suggestions(worth) when the person interacts with it.  

1slider.addEventListener(enter, UpdateRating);

The UpdateRating() perform will probably be invoked each time the person strikes the slider.

Conclusion

This tutorial has coated the best way to construct a practical emoji ranking part. Now we have additionally realized how the enter vary factor is styled on totally different browsers. To boost the performance, you can even request extra suggestions from customers.  

Let’s remind ourselves of what we’ve constructed—and I hope you loved this expertise!