How we can create a quote slider with lines, author name, and author’s image using HTML CSS JS? Solution: See this JavaScript Quote Slider With Controls and Like Button, CSS JS Slides.
Previously I have shared testimonial slider, it like a little similar to that but it is a quote slider. Basically quote slider contains meaningful or motivating quotes by famous persons with their name and image. And it pretends like a slider, you can change quotes by sliding them using left-right navigation buttons.
Today you will learn to create JavaScript CSS slider for quotes. Basically, there are four famous quotes with author names and images in slides. The image is placed on the left slides section and quote on the right side. Below the quotes line, you will see the author’s name and in the bottom-right side is a like button and counter.
So, Today I am sharing JavaScript Quote Slider With Controls and Like Button. There I have used pure JavaScript to creating the program, without jQuery or any library. And all the styles are based on CSS there is no library like bootstrap. If we use libraries like jQuery and Bootstrap then we have to work less also we can save time when creating this.
If you are thinking now how this quotes slides actually is, then see the preview given below.
Preview Of CSS JS Quotes Slides
See this video preview to getting an idea of how these slides of quotes look like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this then get the source code of its.
You May Also Like:
- JavaScript Portfolio Navigation
- Responsive Timeline Slider
- Counting Error 404 Page
- CSS Multi-Layer Navigation
JavaScript Quote Slider Source Code
Before sharing source code, let’s talk about it. First I have created the layout using HTML, I have created a main div and placed an element for photos, buttons for left & right navigation, space for quotes and element for the like wrapper. For the heart icon on the like button, I have used the font-awesome library.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. I gave the width of the elements in PX (pixel) but there also placed a max value like: max-width: max-height: . Also done basic works with CSS like margin, padding, color, etc. For reducing the fonts and other placements in small screen size, I have used the CSS @media query.
JavaScript handling the main feature of the program. I have stored quotes data like line, author name, the image in JavaScript variable. Create a variable named data and stored values inside it. And stored element’s ID and Class values inside another var. All the functions are based on JS DOM (info). Next and previous buttons functions are based on if {} & else if{} statement.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First for HTML, second for CSS, and the third for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>JS Quote Slider | Webdevtrick.com</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link href="https://fonts.googleapis.com/css?family=Notable&display=swap" rel="stylesheet"> <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css'> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <div class="inner-section"> <div class="quote-container"> <div class="photo-wrapper"> <div class="photo-overlay"> <div class="quote"><i class="fa fa-quote-left" aria-hidden="true"></i></div> </div> <img id="quote-image" src="" /> <div class="nav-btns"> <div id="move-left"><span><i class="fa fa-angle-left"></i></span></div> <div id="move-right"><span><i class="fa fa-angle-right"></i></span></div> </div> </div> <div class="details-wrapper"> <div id="quote-header"> <div class="like-wrapper"> <span class="heart"><i class="fa fa-heart" aria-hidden="true"></i></span> <span id="quote-likes">111</span> </div> </div> <div id="quote-text"></div> <div id="quote-footer">- <span id="quote-author"></span></div> </div> </div> </div> </div> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ html, body { height: 100%; width: 100%; margin: 0; padding: 0; } .container { height: 93%; font-family: 'Notable', sans-serif; letter-spacing: 2px; } .container .inner-section { min-height: 470px; height: 100%; width: 100%; position: relative; } .container .inner-section .quote-container { width: 670px; height: 470px; background: #79b9ff; position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; max-width: 100%; max-height: 100%; overflow: none; -webkit-box-shadow: 1px 37px 34px -13px rgba(0, 0, 0, 0.75); -moz-box-shadow: 1px 37px 34px -13px rgba(0, 0, 0, 0.75); box-shadow: 1px 37px 34px -13px rgba(0, 0, 0, 0.75); } .container .inner-section .quote-container .photo-wrapper { position: absolute; width: 25%; z-index: 1; overflow: hidden; left: 0; height: 470px; } .container .inner-section .quote-container .photo-wrapper .photo-overlay { background: #000; z-index: 85; position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: .6; text-align: center; } .container .inner-section .quote-container .photo-wrapper .photo-overlay .quote { color: #FFF; font-size: 50px; margin-top: 30px; opacity: .3; } .container .inner-section .quote-container .photo-wrapper img { height: 470px; margin-left: -75px; } .container .inner-section .quote-container .photo-wrapper .nav-btns { position: absolute; bottom: 10px; font-size: 30px; font-weight: bold; color: #FFF; width: 100%; text-align: center; padding-left: 15px; z-index: 9999; } .container .inner-section .quote-container .photo-wrapper .nav-btns #move-left, .container .inner-section .quote-container .photo-wrapper .nav-btns #move-right { float: left; width: 60px; } .container .inner-section .quote-container .photo-wrapper .nav-btns #move-left > span, .container .inner-section .quote-container .photo-wrapper .nav-btns #move-right > span { cursor: pointer; font-weight: bold; } .container .inner-section .quote-container .photo-wrapper .nav-btns #move-left > span:hover, .container .inner-section .quote-container .photo-wrapper .nav-btns #move-right > span:hover { color: #179ffe; } .container .inner-section .quote-container .photo-wrapper .nav-btns :after { content: ''; display: block; clear: both; } .container .inner-section .quote-container .details-wrapper { width: 70%; z-index: 1000; height: 100%; padding-right: 30px; position: absolute; right: 0; text-transform: uppercase; color: #FFF; min-height: 470px; } .container .inner-section .quote-container .details-wrapper #quote-header .like-wrapper { background: #212121; border-radius: 15px; width: auto; padding: 6px 10px 7px 10px; position: absolute; bottom: 20px; right: 20px; cursor: pointer; font-size: 12px; } .container .inner-section .quote-container .details-wrapper #quote-header .like-wrapper .heart { display: inline-block; font-size: 16px; } .container .inner-section .quote-container .details-wrapper #quote-header .like-wrapper .heart:hover { transform: rotate(360deg); transition: all 0.3s ease-in-out 0s; } .container .inner-section .quote-container .details-wrapper #quote-header .like-wrapper .liked { color: red; } .container .inner-section .quote-container .details-wrapper #quote-header .like-wrapper #quote-likes { display: inline-block; padding-left: 7px; } .container .inner-section .quote-container .details-wrapper #quote-header :hover { background: #ff3d3d; } .container .inner-section .quote-container .details-wrapper #quote-text { margin: 40px 25px 10px 35px; font-size: 45px; font-weight: bold; line-height: 49px; } .container .inner-section .quote-container .details-wrapper #quote-footer { position: absolute; bottom: 10px; font-size: 14px; font-weight: bold; margin: 20px 25px 10px 35px; } .container .inner-section .quote-container :after { content: ''; display: block; clear: both; } @media (max-width: 630px) { .container .inner-section .quote-container .details-wrapper #quote-text { font-size: 25px; line-height: 35px; } .container .inner-section .quote-container .details-wrapper #quote-header .like-wrapper { bottom: 40px; right: 20px; } } |
function.js
The last step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
// Code By Webdevtrick ( https://webdevtrick.com ) (function() { function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } var data = [{ "author": "Einstein", "quote": "I have no special talent. I am only passionately curious.", "image": "https://upload.wikimedia.org/wikipedia/commons/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg", "likes": "1032", "has_liked": 0 }, { "author": "Shakespeare", "quote": "Wisely, and slow. They stumble that run fast.", "image": "https://www.williamshakespeare.net/images/shakespeare.jpg", "likes": "4122", "has_liked": 0 }, { "author": "Mother Teresa", "quote": "If you judge people, you have no time to love them.", "image": "https://upload.wikimedia.org/wikipedia/commons/d/d6/Mother_Teresa_1.jpg", "likes": "2342", "has_liked": 0 }, { "author": "Elon Musk", "quote": "Dichotomy between sense of wonder and what is wrong.", "image": "https://upload.wikimedia.org/wikipedia/commons/e/ed/Elon_Musk_Royal_Society.jpg", "likes": "3315", "has_liked": 0 }]; var options = { imgDomElement: "quote-image", quoteDomElement: "quote-text", authorDomElement: "quote-author", likeDomElement: "quote-likes" }; var quoteBrowser = function(data, elements) { this.domElements = elements; this.quotes = data; this.currentQuoteIndex = 0; this.bindedElements = {}; this.bindedElements.img = document.getElementById(this.domElements.imgDomElement); this.bindedElements.quote = document.getElementById(this.domElements.quoteDomElement); this.bindedElements.author = document.getElementById(this.domElements.authorDomElement); this.bindedElements.likes = document.getElementById(this.domElements.likeDomElement); }; quoteBrowser.prototype.incrementCurrentQuoteLikes = function() { var currentQuote = this.quotes[this.currentQuoteIndex]; if (!currentQuote.has_liked) { currentQuote.likes++; currentQuote.has_liked = 1; } else { currentQuote.likes--; currentQuote.has_liked = 0; } this.setQuote(currentQuote); }; quoteBrowser.prototype.scrollQuote = function(direction) { var currentIndex = this.currentQuoteIndex; if (direction === 'prev') currentIndex--; else currentIndex++; if (currentIndex >= this.quotes.length) currentIndex = 0; else if (currentIndex < 0) currentIndex = this.quotes.length - 1; this.currentQuoteIndex = currentIndex; this.setQuote(this.quotes[currentIndex]); }; quoteBrowser.prototype.init = function() { var prevArrow = document.getElementById("move-left") var nextArrow = document.getElementById("move-right"); var likeButton = document.getElementsByClassName("like-wrapper")[0]; var browser = this; if (prevArrow.addEventListener) { prevArrow.addEventListener('click', function() { browser.scrollQuote('prev') }, false); nextArrow.addEventListener('click', function() { browser.scrollQuote('next') }, false); likeButton.addEventListener('click', function() { browser.incrementCurrentQuoteLikes() }, false); } else if (prevArrow.attachEvent) { prevArrow.attachEvent('onclick', function() { browser.scrollQuote('prev') }); nextArrow.attachEvent('onclick', function() { browser.scrollQuote('next') }); likeButton.attachEvent('onclick', function() { browser.incrementCurrentQuoteLikes() }); } }; quoteBrowser.prototype.getRandomQuote = function() { var random = getRandomInt(0, this.quotes.length - 1); this.currentQuoteIndex = random; return this.quotes[random]; }; quoteBrowser.prototype.setQuote = function(quoteObj) { this.bindedElements.img.src = quoteObj.image; this.bindedElements.quote.innerHTML = quoteObj.quote; this.bindedElements.author.innerHTML = quoteObj.author; this.bindedElements.likes.innerHTML = quoteObj.likes; var heartDiv = document.getElementsByClassName("heart")[0]; heartDiv.classList.remove("liked"); if (quoteObj.has_liked) heartDiv.classList.add("liked"); }; document.addEventListener("DOMContentLoaded", function() { var qb = new quoteBrowser(data, options); var randomQuote = qb.getRandomQuote(); qb.setQuote(randomQuote); qb.init(); }, false); })(); |
That’s It. Now you have successfully created JavaScript Quote Slider With Controls and Like Button, CSS JS Slides. If you have any doubt or questions comment down below.
Thanks For Visiting, Keep Visiting.