1. Introduction
Hello everyone, nice to meet you once a month. As usual, for every post I will share about what I have learned, it might help some people you don’t know. I’m happy to be shared with everyone.
If you often visit websites that are often online sales websites, businesses … then you often see the top of the website often has a constantly changing picture, video, gif show, this is called sliders. The use of slider has a great effect of attracting the attention of customers. Webmasters often put important content in a slider at the top of the website to help users quickly see the highlights of the website.
Sliders usually change over a certain period of time (usually 4 to 5s). Users can also operate directly on the website by clicking or swiping to view the next slide or prev about the previous slide.
For a programmer, it is probably not new to this function. Today, I will show you some ways to create a slide quickly and simply
2. Ways to create a slider
2.1 Use bootstrap
Method 1: Here I want to introduce to you the slider that bootstrap supports us.
If you want to use boostrap’s built-in slider, you need to go to https://getbootstrap.com to download boostrap or you can use the link directly on the bootstrap page to get js, css paste into the website that we have. can use slider without having to code css or js.
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 | <div class="bd-example"> <div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li> <li data-target="#carouselExampleCaptions" data-slide-to="1"></li> <li data-target="#carouselExampleCaptions" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <div class="carousel-item active"> <img src="image1.jpg" class="d-block w-100" alt="..."> </div> <div class="carousel-item"> <img src="image2.jpg" class="d-block w-100" alt="..."> </div> <div class="carousel-item"> <img src="image3.jpg" class="d-block w-100" alt="..."> </div> </div> <a class="carousel-control-prev" href="#carouselExampleCaptions" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExampleCaptions" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> </div> |
Here I briefly explained the parts to show a slider:
- .carousel-inner this is the wrapper around the div containing the image item, caption each image that we want to display.
- .carousel-indicators here is the section containing the navigation links we want to click on the slide show the image we want to visit. Usually here is the dot icon or slide index icon.
- .carousel-control-prev. carousel-control-next is the part containing the prev or next link back and forth from the slides.
Also you want to add caption for each image, we add a content div after each img tag.
1 2 3 4 5 | <div class="carousel-caption d-none d-md-block"> <h5>Tiêu đề caption</h5> <p>Nội dung caption</p> </div> |
Very simple is not it, continue to the second way I will guide you to use css, pure js to code out a slider, in this way requires you have basic knowledge of html, css, js okay . It is not difficult to stay tuned!
2.2 Use pure html, css, js
In the second way we code the slider automatically, you create yourself 3 files index.html
, style.css
, javascript.js
:
- The
style.css
file you use css to align the slider size as well as the position of the images, next, prev button:
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 | * { box-sizing: border-box } body { font-family: Verdana, sans-serif; margin:0 } .mySlides { display: none } img { vertical-align: middle; } .slideshow-container { max-width: 1000px; position: relative; margin: auto; } .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; } .next { right: 0; border-radius: 3px 0 0 3px; } .prev:hover, .next:hover { background-color: rgba(0,0,0,0.8); } .text { color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 8px; width: 100%; text-align: center; } .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } .dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: #717171; } .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from {opacity: .4} to {opacity: 1} } @keyframes fade { from {opacity: .4} to {opacity: 1} } @media only screen and (max-width: 300px) { .prev, .next,.text {font-size: 11px} } |
- The file
index.html
contains static html code, here you lead the image link to the image …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <div class="slideshow-container"> <div class="mySlider fade"> <img src="image1.jpg" style="width:100%" /> <div class="text">Caption Text</div> </div> <div class="mySlider fade"> <img src="image2.jpg" style="width:100%" /> <div class="text">Caption Two</div> </div> <div class="mySlider fade"> <img src="image3.jpg" style="width:100%" /> <div class="text">Caption Three</div> </div> <a class="prev" onclick="plusSlider(-1)">&#10094;</a> <a class="next" onclick="plusSlider(1)">&#10095;</a> </div> <div style="text-align:center"> <span class="dot" onclick="currentSlider(1)"></span> <span class="dot" onclick="currentSlider(2)"></span> <span class="dot" onclick="currentSlider(3)"></span> </div> |
- The
javascript.js
file where we create the next image event, prev image:
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 | var slideIndex = 1; showSlider(slideIndex); function plusSlider(n) { showSlider(slideIndex += n); } function currentSlider(n) { showSlider(slideIndex = n); } function showSlider(n) { var i; var slider = document.getElementsByClassName("mySlider"); var dots = document.getElementsByClassName("dot"); if (n > slider.length) {slideIndex = 1} if (n < 1) {slideIndex = slider.length} for (i = 0; i < slider.length; i++) { slider[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slider[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; } |
Ok very verbose, right. This way, you can easily create sliders, effects as you like
2.3 Use gem
Why am I saying using gem here? Here I also introduce to you that I am a Ruby backend (Ruby on Rails). I recommend that we should try this language, it is easy to learn and use. When we want to do a function, Ruby’s framework Ruby on Rails has a lot of support gems when we want to program.
For example, some support gems include: Pagination (“kaminari” gem), login (gem “devise”, “omniauth”, “omniauth-google-oauth2”), decentralization (gem “cancancan”) … And there are countless other gems, today’s article I want to share about a gem that supports creating slides is the gem “lex-slider-rails”.
Want to use this gem, then ruby your project rails add gem below and run the bundle install command to install the gem.
1 2 3 4 | gem 'bootstrap-sass', '~> 3.4.1' gem 'jquery-rails' gem 'flex-slider-rails' |
For the gem 'flex-slider-rails'
work, we also need to run the gem 'bootstrap-sass'
and gem 'jquery-rails'
to add bootstrap and jquery to the project.
In the application.css
directory, add yourself the line *= require flexslider
In the application.js
directory you add yourself the line //= require jquery.flexslider
and a piece of js code as below.
1 2 3 4 | $(document).on("turbolinks:load", function() { $('.flexslider').flexslider(); }); |
Ok here basically has completed the installation step and then you want to use the slider where, just add the html code as below as we have 1 slider to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <div class="flexslider"> <ul class="slides"> <li> <img src="image1.jpg" /> </li> <li> <img src="image2.jpg" /> </li> <li> <img src="image3.jpg" /> </li> </ul> </div> |
The result of all three ways is that we create an active slider. Very simple is not it and below is my achievement.
3. Conclusion
Ok, got it Here I would like to end my share. See you next time next month. If you like the article, vote give me
Reference links: