Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Install Swiper on Grav

Started by Muut Archive 9 years ago · 4 replies · 1152 views
9 years ago

Hi, It's been awhile since I try to add Swiper (http://idangero.us/swiper/get-started) to my Grav website,

They say to add this html code
--- html
<head>
...
<link rel="stylesheet" href="path/to/swiper.min.css">
</head>
<body>
...
<script src="path/to/swiper.min.js"></script>
</body>

TWIG
So I added this one to the user  hemes\antimatter  emplates\home.html.twig
--- 
{% block stylesheets %}
        {% do assets.addCss('theme://css/swiper.min.css') %}

    {% endblock %}
    {{ assets.css() }}

    {% block javascripts %}

        {% do assets.addJs('theme://js/swiper.min.js') %}                                                                           
    {% endblock %} 
    {{ assets.js() }}

{% endblock head %}

Then they say to add this code to html layout
--- html
<!-- Slider main container -->
<div class="swiper-container">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
...
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>

HTML
<!-- If we need navigation buttons -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>

<!-- If we need scrollbar -->
<div class="swiper-scrollbar"></div>

</div>

TWIG
So I added this one to the user  hemes\antimatter  emplates\home.html.twig just before the {% endblock %} and </body>.

I also added what is necessary on the user  hemes\mytheme\css\home.css
--- css
.swiper-container {
    width: 600px;
    height: 300px;
}      

Then they say:


Finally, we need to initialize Swiper in JS. There are few options/places to do that:

The best option will be in inline script or in script file that is included in the very end of body (right before closing </body> tag):
<script>
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
direction: 'vertical',
loop: true,

YAML
// If we need pagination
pagination: '.swiper-pagination',

// Navigation arrows
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',

// And if we need scrollbar
scrollbar: '.swiper-scrollbar',

})
</script>
</body>

TXT

But I don't know where I need to place that and actually the slideshow looks like this:
 [Capture30](//muut.com/u/getgrav/s1/:getgrav:RDlu:capture30.png.jpg) 
I can't click anywhere on the slideshow and slide doesn't slide.
Thank so much in advance for who can help me :)
9 years ago

Hi
I don't know anything about this this plugin specifically but in general you can add that script code into your twig template file or if it's going to be used over multiple templates it might be easier to create a .js file and call that from the twig template(s).

You would add those <script> lines within the {% block %} commands - probably at the end of the file between the end of the HTML and the block tags like this:

HTML

</div>

<script>        
  var mySwiper = new Swiper ('.swiper-container', {
    // Optional parameters
    direction: 'vertical',
    loop: true,

    // If we need pagination
    pagination: '.swiper-pagination',

    // Navigation arrows
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev', 

    // And if we need scrollbar
    scrollbar: '.swiper-scrollbar',
  })        
  </script>

{% endblock %}
9 years ago

Hello,

You can also put all your javascript that you'll need on every pages in a file (ie: script.js) and then, add it in your template :

TWIG

    {% block javascripts %}

        {% do assets.addJs('theme://js/swiper.min.js') %}
         {% do assets.addJs('theme://js/script.js') %}                                                                           
    {% endblock %} 
    {{ assets.js() }}
---
9 years ago

Thank you so much for your time guys,
I try as you say but it still doesn't work, I do not succeed for find where the problem is originated.

I show you where I put my codes. You may see where my problems come from,
→ → → I added arrows where I added code. ← ← ←
Here is <head> in my twig template file (user/themes/antimatter/templates/home.html.twig) :
--- twig
<head>
<meta name="theme-color" content="#42f4d9">
<link href="https://fonts.googleapis.com/css?family=Ubuntu+Condensed" rel="stylesheet"/>
{% block head %}
<meta charset="utf-8" />
<title>{% if header.title %},{{ header.title|e('html') }} | {% endif %},{{ site.title|e('html') }}</title>
{% include 'partials/metadata.html.twig' %}
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="icon" type="image/png" href="{{ url('theme://images/favicon.png') }}" />
<link rel="canonical" href="{{ page.url(true, true) }}" />

TWIG
{% block stylesheets %}
    {% do assets.addCss('theme://css/pure-0.5.0/grids-min .css', 103) %}
    {% do assets.addCss('theme://css-compiled/nucleus.css', 102) %}
    {% do assets.addCss('theme://css-compiled/template.css', 101) %}
    {% do assets.addCss('theme://css/custom.css', 100) %}
    {% do assets.addCss('theme://css/home.css', 100) %}

→ → → {% do assets.addCss('theme://css/swiper.min.css') %} ← ← ←
{% do assets.addCss('theme://css/font-awesome.min.css', 100) %}
{% do assets.addCss('theme://css/slidebars.min.css') %}

TWIG
    {% if browser.getBrowser == 'msie' and browser.getVersion == 10 %}
        {% do assets.addCss('theme://css/nucleus-ie10.css') %}
    {% endif %}
    {% if browser.getBrowser == 'msie' and browser.getVersion >= 8 and browser.getVersion <= 9 %}
        {% do assets.addCss('theme://css/nucleus-ie9.css') %}
        {% do assets.addJs('theme://js/html5shiv-printshiv.min.js') %}
    {% endif %}
{% endblock %}
{{ assets.css() }}

{% block javascripts %}
    {% do assets.addJs('jquery', 101) %}
    {% do assets.addJs('theme://js/modernizr.custom.71422.js', 100) %}
    {% do assets.addJs('theme://js/antimatter.js') %}

→ → → {% do assets.addJs('theme://js/swiper.min.js') %} ← ← ←
{% do assets.addJs('theme://js/slidebars.min.js') %}
{% endblock %}
{{ assets.js() }}

{% endblock head %}
</head>

TWIG
And this is between the end of the HTML and the block tags :
--- twig
    {% block bottom %}
        {{ assets.js('bottom') }}
        <script>
        $(function () {
            $(document).ready(function() {
              $.slidebars({
                hideControlClasses: true,
                scrollLock: true
              });
            });
        });
        </script>

→ → → <script>        
  var mySwiper = new Swiper ('.swiper-container', {
    // Optional parameters
    direction: 'vertical',
    loop: true,

    // If we need pagination
    pagination: '.swiper-pagination',

    // Navigation arrows
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev',

    // And if we need scrollbar
    scrollbar: '.swiper-scrollbar',
  })        
  </script> ← ← ←

    {% endblock %}
</body>
</html>

Here is the code on the page where I want to add the slider (user/pages/05.home/article1/home.md) :
--- html
<body>
<div class="flex-container">
<div class="flex1">
</div>
<div class="flex2">

→ → → <div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div> ← ← ←
</div></div>

→ → → <script>
var swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
slidesPerView: 4,
centeredSlides: true,
paginationClickable: true,
spaceBetween: 30,
grabCursor: true
direction: 'horizontal'
});
</script> ← ← ←
</body>
</html>

CSS
And there is the .css (user/themes/mytheme/css/home.css) :
--- css
/* Flexbox gallery */
gallery { 
  padding: .5vw;
  display: -ms-flexbox;
  -ms-flex-wrap: wrap;
  -ms-flex-direction: column;
  -webkit-flex-flow: row wrap; 
  flex-flow: row wrap; 
  display: -webkit-box;
  display: flex;
  max-width: 1000px;
  margin: 0 auto;
}
gallery div { 
  -webkit-box-flex: auto;
  -ms-flex: auto;
  flex: auto; 
  width: 400px; 
  margin: .5vw; 
}
gallery div img { 
  width: 100%; 
  height: auto; 
}
@media screen and (max-width: 400px) {
  gallery div { margin: 0; }
  gallery { padding: 0; }

}
  ↓ ↓ ↓
/* Slider */
.swiper-container {
    width: 300px;
    height: 300px;
}      
  ↑ ↑ ↑

Let me know if you see what causes this.

The Grav community is really superb :)

9 years ago

Hello

Is there a link where we could see it live ?

For now, my first guess would be that you create two objects Swiper().
You could probably delete :

TXT
    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        slidesPerView: 4,
        centeredSlides: true,
        paginationClickable: true,
        spaceBetween: 30,
        grabCursor: true
        direction: 'horizontal'
    });

Do you have any js error in your console when you are on your homepage ?

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1323 9 years ago
Archive · by Muut Archive, 9 years ago
2 919 9 years ago
Archive · by Muut Archive, 9 years ago
2 4048 9 years ago
Archive · by Muut Archive, 9 years ago
1 2923 9 years ago
Archive · by Muut Archive, 9 years ago
3 1106 9 years ago