3 easy ways to create a slider is easy

Tram Ho

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.

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.

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:

  • The file index.html contains static html code, here you lead the image link to the image …

  • The javascript.js file where we create the next image event, prev image:

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.

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.

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.

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:

Share the news now

Source : Viblo