Easy ~~? I think it can be implemented in ~~.
slick
is a jQuery plugin that can add a function to display images etc. like a slide show.
This article only introduces ** implementation method ** and ** simple implementation **.
Those who say, "I want to make the site look fashionable, so please tell me something more advanced!" sorry.
It should be easy for beginners to understand because it is written in ordinary html
instead of haml
or slim
. ..
I will also introduce the place where I first introduced it and threw an error, along with a solution.
This time I'm using rails 5.2.4. Also, the OS is Windows, but I think it is the same for mac. (It may look a little different ...)
We will continue on the premise of the above two points.
First of all, slick
uses jQuery, so
Gemfile
gem "jquery-rails"
Describe and install.
Terminal
bundle install
After installing, write the following in another location.
app/assets/javascripts/application.js
Added bottom 2 lines(← I don't need Japanese characters)
//= require jquery
//= require jquery_ujs
//= require rails-ujs
//= require activestorage
//= require_tree .
I'll erase the following sentence for the time being.
//= require turbolinks
Now you are ready to go. I will introduce it at once.
Download the file from slick. Click this button to start the download. (Scroll down to see the screen above)
slick-1.8.1.zip Folder will be downloaded.
From there, go to slick-1.8.1 → slick and you will find some files.
From among them
・ Slick.min.js
・ Slick.scss
・ Slick-theme.scss
I will use three. (Only the minimum required)
File | storage site |
---|---|
slick.min.js | app/assets/javascripts/ |
slick.scss | app/assets/stylesheets/ |
slick-theme.scss | app/assets/stylesheets/ |
Please put it in the above place.
Describe in html.
html:app/views/layouts/application.html.erb
<head>
<title>Title</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<!--Add the bottom two lines-->
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css"/>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css"/>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
To import scss, write the following.
app/assets/stylesheets/application.scss
*= require_tree .
*= require_self
*/
*Added the following two lines
@import "slick-theme";
@import "slick";
This completes the introduction.
Now that we have introduced it, we will actually use it.
This time, we will implement the following basic functions. (See other people's articles when arranging) ・ Display dots below ・ Press the dot to slide the photo ・ Flick the image to slide
html.erb
<div class="hoge">
<div class="nest-hoge">aaa</div>
<div class="nest-hoge">bbb</div>
<div class="nest-hoge">ccc</div>
</div>
The hoge
and nest-hoge
parts can be anything. (I don't know the naming convention for ~~ css ... ~~)
app/assets/javascripts/application.js
$(function() {
$('.hoge').slick({
dots: true,
});
});
If you specify the parent class, it will work.
You have now implemented it.
Finally, I would like to introduce the error that occurred when I first introduced slick.
** does not work **
I've added a gem, so let's do a bundle install.
Put it in the right position.
** Don't put js files under stylesheets if you make a mistake **
I'm downloading the file and I thought I didn't need a description. Error Hello ** at ** developer mode It seems that a description is necessary.
I can not do it. .. .. What should I do? .. ..
Pay attention to the 18th line.
/*global $*/
It seems OK if you add.
That's it. There are various slider plugins such as sweeper as well as slick. There is also a Summary site, so please refer to it.
See you soon.
Recommended Posts