Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Build a quote switcher with Vanilla JS
Juan F Gonzalez
Juan F Gonzalez

Posted on • Edited on • Originally published atblog.juanfgonzalez.me

     

Build a quote switcher with Vanilla JS

Hello again, my friend.

This time we're going to do something more practical, visual, and frontend-focused.

Also, since JavaScript has had several updates throughout the years, you would be surprised to see all the different things we can now create without the need for a framework.

Just plain good ol' vanilla JS.

You wanna know what it is? Well, let's jump right in!

jumping in

What are we going to build?

It will be this nice-looking, component that displays a quote when clicking on its author's image.

And it all works with just plain HTML, CSS, and JS.

Here's how it looks.

quotes.png

What we'll need

The Markup

Let's start with the markup. First, we will need a container for the component and some space for both the author and the quote.

It can be something like this...

<divclass="switcher"><divclass="authors">      ...</div><divclass="quotes">     ...</div></div>
Enter fullscreen modeExit fullscreen mode

Now, we can add the text of the quote and the name of the author.

<divclass="quote"><divclass="quote-text"><div><p>                ...</p></div><divclass="quote-author"><h3>...</h3></div></div></div>
Enter fullscreen modeExit fullscreen mode

Lastly, let's add the code for the button and the author's image.

<buttonclass="author-btn"type="button"data-index="0"><imgclass="btn-image"src="https://image.jpg"alt="Author Image"/></button>
Enter fullscreen modeExit fullscreen mode

That's pretty much all the code that we'll need to make this happen.

You can modify this markup to your liking or adapt it if you're using a preprocessor. Something likePug for example.

The Styling

Now for the styling, that's another thing that you can freely change to make the end result what you want it to be.

Also, if you want to use another preprocessor like SASS or Stylus, go right ahead. (The first one is my personal preference)

In this case, I'll be using this styling.

.switcher{margin:1rem;background-color:#fff;border:1pxsolid#eee;border-radius:8px;font-family:"Helvetica",sans-serif;margin-bottom:1.875rem;max-width:550px;}.btn-image{display:block;height:100%;width:100%;}.author-btn{border:0.125remsolid#4caf50;border-radius:50%;height:4.5rem;padding:0.02rem;margin-bottom:1rem;margin-left:1rem;margin-top:1rem;overflow:hidden;transition:opacity0.2sease-out;width:4.5rem;}.author-btn:focus{opacity:1;outline:0;}.author-btn-dimmed{border-color:#bdbdbd;border-width:0.0625rem;opacity:0.5;}.quotes{margin-top:1rem;background-color:#fafafa;}.title{margin:0;padding:0.5rem0;text-align:center;}.quote-container{background-color:#fafafa;width:100%;}.quote-container-hidden{display:none;}.quote{border-bottom:0.06remsolid#e0e0e0;width:100%;}.quote-text{width:calc(100%-2rem);margin-left:1rem;}.quote-author{margin:1rem0;}
Enter fullscreen modeExit fullscreen mode

The JavaScript

And now, here comes the fun part. Or I should say theinteractive part.

It's now time to give it some movin' & groovin' with a bit of JS.

First, we'll need to get references to the elements that we'll be interacting with.

constauthorsList=document.querySelectorAll(".author-btn");constquotesList=document.querySelectorAll(".quote-container");
Enter fullscreen modeExit fullscreen mode

Now, we'll need to add the event listener for when the author button gets clicked. We'll do for all the authors with this code.

authorsList.forEach((author)=>{author.addEventListener('click',handleClick)})
Enter fullscreen modeExit fullscreen mode

For thathandleClick function, we have to retrieve the value of the index for that particular button. It can be something like this.

functionhandleClick(event){constselectedQuote=parseInt(event.currentTarget.dataset.index,10);showQuote(selectedQuote);}
Enter fullscreen modeExit fullscreen mode

Now that we have that particular index, we'll call a helper function that will do the 'switching' of the selected quote.

That functionshowQuote will basically compare what's is the active quote being displayed and what's the new quote that's going to be displayed.

For that, we need to keep track of what's our current index. Something like this will do

letquoteIndex=0;
Enter fullscreen modeExit fullscreen mode

That will go at the start of the file alongside the authors and quotes lists.

Now, to implement the missing function. Here's what will do the trick.

functionshowQuote(index){if(index===quoteIndex)return;authorsList[quoteIndex].classList.toggle("author-btn-dimmed");quotesList[quoteIndex].classList.toggle("quote-container-hidden");authorsList[index].classList.toggle("author-btn-dimmed");quotesList[index].classList.toggle("quote-container-hidden");quoteIndex=index;}
Enter fullscreen modeExit fullscreen mode

And what we're doing here is a game of toggling CSS classes with a little animation to get that 'switching' effect.

And that's in a nutshell everything necessary to make the component work.

Putting it all together

Ok, with all those pieces in place we can interact with it and see it working.

We can add more authors and quotes by duplicating the markup for each of those and adding the corresponding text.

See this codepen for an example of the component built.

And like I said before if there are some tweaks and adjustments you want to do to this component, feel free to change it to your heart's content.

Conclusion

Well, there you have it! A fully functioning quote switching component built entirely with plain ol' vanilla JS and some HTML & CSS.

It's interesting the kind of dynamic things we can build just by using the technologies of the lean web.

That is not to say that frameworks are not useful or bad. But knowing how the system behind works without a bunch of abstractions, it gives us a better understanding and allows us to do our job much better.

Instead of not knowing how things work and depending on the mercy of "whenever the program wants to work properly".

That's it for this post. As always thanks a lot for reading. If you have any questions or comments send them my way through thelittle blue bird.


Photo bycapnsnap on

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
istealersn profile image
Stanley J Nadar
Passionate Frontend Engineer aiming to build scalable websites/web applications
  • Location
    Chennai
  • Work
    Engineering Manager at Freshworks
  • Joined
• Edited on• Edited

Any reason why you aren't using 'blockquote' HTML tag? It will resolve your accessibility issues too

CollapseExpand
 
avinaashdhumal profile image
Avinash Dhumal
Software Engineer
  • Location
    Pune, MH, India
  • Work
    Software Developer
  • Joined

Here I made simple random quotes generator.
engvibes.netlify.app

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Frontend dev. Online writer. Lifelong learner. Sharing practical knowledge.
  • Education
    Computer science & software engineering
  • Work
    Freelance writer & web developer
  • Joined

More fromJuan F Gonzalez

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp