Swiper is not defined - wordpress

I try to use Swiper js in my wordpress website with my custom theme.
https://swiperjs.com/demos/270-mousewheel-control/core.html
I always have a js error in my console : 'Swiper' is not defined
My code :
<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 class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
</div>
<div class="swiper-pagination"></div>
</div>
In my custom JS file, i add :
var swiper = new Swiper(".swiper-container", {
direction: "vertical",
slidesPerView: 1,
spaceBetween: 30,
mousewheel: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
},
});

I was going crazy trying to resolve the same issue.
Example Swiper script:
https://codepen.io/simranthapa/pen/eYmjYYw
In my case the issue was:
When using code from codepen.io, you have to click on 'settings' to check if any external css/js are required to make the script work. Don't just rely on the content it shows you in the 3 boxes, as that is not always the full code.
Add the JS files from the CDN to your header.php file. There should be one for the full swiper code and one for jquery in general, eg:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js></script>
and:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/js/swiper.min.js"></script>
Add the page jquery in the wordpress page you want the swiper to appear, but wrap it in this:
jQuery(document).ready(function() {
//your code here
});
All 3 issues needed addressing before the slider/swiper finally worked!
Hope this helps somebody.
Might not be the most optimised approach, but it proves that it CAN work and that you are still sane.

Related

Meteor swiper slider taking time to load images and videos?

I am using swiper slider in meteor but it's taking too much time to load images and videos in slider on the home page. On my home page posts are coming and each posts videos and images are coming in the slider. But after taking too much time to load it crash the page. This is code to show posts videos and images in swiper slider.
{{#if imagesArguments}}
<div class="argu-scroller" id="argumentImages" style="border:1px solid #d4d6d8; margin-top:10px;" data-userProfile="{{getImage plaintiff._id}}">
<div class="swiper-wrapper">
{{#if meta.thumbnail_url}}
<div class="swiper-slide swiper-card">
<div class="evidencecard">
<div class="evidencecard-inside">
<header>
<a href="{{plaintiff_bitlyurl.url}}" target="_blank">
<img src="{{meta.thumbnail_url}}" />
<div class="text-wrap">
<h2>{{meta.title}}</h2>
</div>
</a>
</header>
<main>
<div class="info-wrap">
<h5>{{plaintiff_bitlyurl.url}}</h5>
<!--<p>{{evidenceFull plaintiff_evidence}}</p>-->
<p>{{meta.description}}</p>
</div>
</main>
</div>
</div>
</div>
{{/if}}
{{#each imagesArguments}}
<div class="swiper-slide swiper-image">
<div class="argumentimageshome argument2 tem-miniargument">
<a data-toggle="modal" data-target="#imageZoomview" class="imgZoom" data-id="{{this}}" >
<img class="img-thumbnail" src="{{this}}" />
</a>
</div>
</div>
{{/each}}
{{#each videoArguments}}
<div class="swiper-slide swiper-video">
<div class="argumentvideoshome">
<div class="video">
<video height="150" controls>
<source src="{{this}}" type="video/mp4">
</video>
</div>
</div>
</div>
{{/each}}
</div>
</div>
{{/if}}
code to call slider in js file
Template.miniArgument.onRendered(function(){
var swiper = new Swiper('.argu-scroller', {
slidesPerView: 3,
spaceBetween: 30,
slidesPerGroup: 3,
loop: true,
loopFillGroupWithBlank: true,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
});
I think this is not related to Meteor. Did you try to use the virtualization component in the Swiper? You would need to lazy-load extra slides. If not too many, you can preload all of them with the ones in view being prioritized. If you have many, you need to make use of the lazy load. Check this discussion: https://github.com/nolimits4web/swiper/issues/1279 . In your Network tab in Chrome developer tools, do you see a lot of MBs being downloaded? If you have huge assets ... it will indeed take time.

How to make iDangerous Swiper works on Wordpress

I'm learning to use Swiper Sldier in a website using wordpress and Divi theme. I also try to follow a previous thread as a guide.
After following the instruction, the slider does not work, the imgage stuck at the left screen and it does not move as I drag them.
Here is the code on functions.php
/** Function SSwiper **/
function swiper_magic() {
wp_enqueue_script('swiper','https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/js/swiper.min.js', array ('jquery'));
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
add_action( 'wp_enqueue_scripts', 'swiper_magic' );
On my header:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/css/swiper.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/css/swiper.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/js/swiper.min.js"></script>
<script>
$(document).ready(function(){
var mySwiper = new Swiper('.swiper-container',{
pagination: '.pagination',
loop:true,
grabCursor: true,
paginationClickable: true
})
jQuery('.arrow-left').on('click', function(e){
e.preventDefault()
mySwiper.swipePrev()
})
jQuery('.arrow-right').on('click', function(e){
e.preventDefault()
mySwiper.swipeNext()
})
})
</script>
And this is my HTML
<div class="device">
<a class="arrow-left" href="#"></a>
<a class="arrow-right" href="#"></a>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"> <img src="/images/slider1-1.png"> </div>
<div class="swiper-slide"> <img src="/images/slider1-2.png"> </div>
<div class="swiper-slide">
<div class="content-slide">
<p class="title">Slide with HTML</p>
<p>You can put any HTML inside of slide with any layout, not only images, even another Swiper!</p>
</div>
</div>
</div>
</div>
<div class="pagination"></div>
</div>
I don't know why it did not work.
Any help or general suggestions would be greatly appreciated...
Base on swiper demos, I suggest to put your script at the bottom. To work with swiper arrow button the easiest way are:
var swiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
pagination: {
el: '.swiper-pagination',
},
});
And your html would be:
<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>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
Then, if you want to run the callback. Simply use:
swiper.on('slideChange', function () {
console.log('slide changed');
});
slideChange are the event depend how do you want to run the callback. See full swiper event and method
Use this for your php function
function swiper_magic() {
wp_enqueue_script('swiper','https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/js/swiper.min.js', array ('jquery'), true); // true to load at the bottom after jquery
}
add_action( 'wp_enqueue_scripts', 'swiper_magic' );
Hope this help! ;)

Put md-switch in the middle of the cards (AngularJS material design ,Css)

I want to create the same design like chrome settings page
https://www.pcworld.com/article/3162716/software/how-to-switch-to-chromes-material-design-settings-page-for-an-easier-experience.html
I do not manage to put the md-switch at the right and in static position like chrome settings
I do manage to put the md-switch in right to the title but I need it always in the middle of the card stick to right.
Image of my result: (current design)
<md-card md-theme="{{ showDarkTheme ? 'dark-purple' : 'default' }}" md-theme-watch>
<div class="option-item">
<div class="option-item-header">
<div class="option-item-header-title">
title
</div>
</div>
<div class="clear"></div>
<div class="option-body">
<p>text</p>
</div>
</div>
<div class="option-switch">
<md-switch ng-model="vm.model" aria-label="active" ng-change="function()">
</md-switch>
</div>
</md-card>

Angular2 - switch between styles

I'm currently working on an application using Angular2.
Now that I finished my styling of the app, I want to make more themes to switch between.
I've tried setting my styleUrls to a variable and change it on click, but this doesn't work.
let styles= nightflat
#Component({
selector: 'start-app',
template: `
<div class="ui-g ui-g-nopad">
<div class="ui-g-12 ui-g-nopad" id="header">
<img src="../src/resources/icon/c2logo.png" alt="Logo" id="logo">
<button pButton icon="fa-sign-out" id="LogOut" type="button" label="LogOut" (click)="gotoLogout()"></button>
<h1></h1>
</div>
<div class="ui-g-12 ui-g-nopad">
<p-breadcrumb [model]="bcitems"></p-breadcrumb>
</div>
<div class="ui-g-12 ui-g-nopad">
<div class="ui-g-2 ui-g-nopad">
<p-panelMenu id="menu" [model]="itempanel"></p-panelMenu>
</div>
<div class="ui-g-10 ui-g-nopad" id="router-outlet">
<router-outlet> </router-outlet>
<div class="ui-g-12 ui-g-nopad">
<button pButton id="goBack" type="button" label="Zurück" (click)="goBack()"></button>
</div>
</div>
</div>
</div>
`
,
styleUrls: [styles],
directives: [Button],
providers: []
})
"nightflat" is an import from my style-urls.ts providing a css-path for every style I want to use (til now only 2):
export var nightflat = './dist/resources/scss/app.component.css'
export var froggy = './dist/resources/scss/test.component.css'
I then tried to use a function called by a Buttonclickevent to set the styles variable to another value.
setStyle(){
styles = froggy;
}
Well, that doesn't work at all.
First question: As I'm using gulp-inject - do I have to inject ALL of the style-sheets into my index.html?
Second question: How am I supposed to switch theming onclick?

Find the biggest height of a div in ng-repeat

I have a set of data which is an array of objects. Some objects have more data than the other. But each object will display in each col-md-4 div. Therefore, the height of each div will be different based on how much data an object has.
Here is my code:
<div class="col-md-12 eventlog-info-container">
<div class="row eventlogs-single-container">
<div class="col-md-4 eventlog-1-container" ng-repeat="record in records track by $index">
<h4>Event Log {{$index}}</h4>
<ul>
<li ng-repeat="(key, value) in record">{{::key}}: {{::value}}</li>
</ul>
</div>
</div>
</div>
My question is after ng-repeat, I want to find the biggest height of the element. And then apply the biggest height to each of the element using ng-style.
I have an idea to approach that using JQuery. However, I want to use Angular to do that? Any suggestion? Anuglar is not good for DOM manipulation?
Thank you in advanced.
After I did some research yesterday. I found an open source angular directive to solve my problem - angularJS Vertilize Directive An AngularJS directive to vertically equalize a group of elements with varying heights. In other words, it dynamically makes a group of elements the same height. Thank you Chris Collins who made this directive.
<div vertilize-container class="row">
<div ng-repeat="col in columns" class="col-sm-3">
<div class="well">
<div vertilize>
<h3>{{ col.title }}</h3>
<p>{{ col.body }}</p>
</div>
</div>
</div>
</div>
This answer should get you started on the right path.
<div class="col-md-12 eventlog-info-container">
<div class="row eventlogs-single-container">
<div class="col-md-4 eventlog-1-container" ng-repeat="record in records track by $index">
<h4>Event Log {{$index}}</h4>
<ul>
<li outer-height ng-repeat="(key, value) in record">{{::key}}: {{::value}}</li>
</ul>
</div>
</div>
</div>
app.directive('outerHeight', function(){
return{
restrict:'A',
link: function(scope, element){
//using outerHeight() assumes you have jQuery
// if not using jQuery
console.log(element.outerHeight());
}
};
});

Resources