1.0.8 • Published 5 years ago

marvina-slider v1.0.8

Weekly downloads
12
License
MIT
Repository
github
Last release
5 years ago

Marvina Slider

Image Slider library for web and mobile browsers. \ For more information you can take a look at the demos: Demo 1, Demo 2, Demo 3, Demo 4.

NPM

npm install --save-dev marvina-slider

Yarn

yarn add marvina-slider --dev

Installation

You can simply import Marvina Slider and create a new object with the Slider class.

import {Slider} from 'marvina-slider';

const slider = new Slider({
    el: '#slider'
});

You can also add the script file into your html.

<script src="/node_modules/marvina-slider/dist/js/marvina-slider.min.js"></script>
<script>
var slider = new MarvinaSlider.Slider({
    el: '#slider'
});
</script>

Add the css file.

<link rel="stylesheet" href="/node_modules/marvina-slider/dist/css/marvina-slider.min.css" /> 

Configuration

Id

You can customize each element by assinging an id.

// html
<div id="slider">
    <div class="ms-slider-element" ms-id="img1">Image-1</div>
    <div class="ms-slider-element" ms-id="img2">Image-2</div>
    <div class="ms-slider-element" ms-id="img3">Image-3</div>
</div>

// js
import {Slider, SliderType} from 'marvina-slider'

const slider = new Slider({
    el: '#slider',
    imagesSettings: [
        { id:'img1', sliderType:SliderType.Carousel },
        { id:'img2', sliderType:SliderType.Flow },
        { id:'img3', sliderType:SliderType.Fade }
    ]
});

Options

OptionTypeDefaultDescription
elstring | HTMLElement*nullContainer element.
timingstring"ease"Specifies the speed curve of an animation. Takes all the values CSS3 can take. (like ease, linear, cubic-bezier, step)
durationnumber800Defines how long an animation should take to complete one cycle. (as milliseconds)
sliderTypeSliderTypeCarouselSpecifies the animation type. It can be customized for each image element with the imagesSettings property.
touchMovebooleantrueEnables slide transitive with touch.
listbooleantrueDisplays a list at the bottom of the slider.
asListstring | HTMLUListElement | HTMLOListElement*nullDeclares the given list as the slider list.
arrowsbooleantrueDisplays right and left arrows to change the index.
asPrevArrowstring | HTMLElement*nullDeclares the given element as the prev arrow.
asNextArrowstring | HTMLElement*nullDeclares the given element as the next arrow.
autoPlaybooleanfalseEnables auto play of slides.
autoPlaySpeednumber5000Sets auto play interval. (as milliseconds)
imagesSettingsSliderElement[][]Customizes each element.

*: You can give an HTML element or a CSS selector (like #carousel, .container > div:first-child)

Slider Type

Specifies the slider animation type. You can import SliderType if you are using modules.

import {Slider, SliderType} from 'marvina-slider'

As List

You can convert an HTML list element to a slider list that shows the current index and works as a pager.

  • It can be a ul or ol element.
  • It can be placed anywhere in the body.
  • List is updated when the index is changed.
  • Assigns ms-active class to list element that holds the current index.
// html
<div id="slider">
    <div class="ms-slider-element" ms-id="img1">Image-1</div>
    <div class="ms-slider-element" ms-id="img2">Image-2</div>
    <div class="ms-slider-element" ms-id="img3">Image-3</div>
</div>
<ul id="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

// script
var slider = new MarvinaSlider.Slider({
    el: '#slider',
    list: false,
    asList: '#list'
});

Callbacks

Before

before(current:SliderElement, next:SliderElement): Promise\ \ It is invoked before animation runs. It returns a promise so that animation waits for this mehtod to complete.

after(current:SliderElement, prev:SliderElement): Promise\ \ It is invoked after animation runs. It returns a promise so before the method completes running, another animation cannot run.

Each slider element also has an before and after method for themselves.

Events

EventDescription
touchStartFires when touching starts.
touchEndFires when touching ends.
changeFires when index changes.
playFires when autoplay starts.
stopFires when autoplay stops.
destroyFires when the slider is destroyed.
import {Slider} from 'marvina-slider';

const slider = new Slider({
    el: '#slider'
});

slider.el.addEventListener('touchStart', () => {
    console.log('touching starts');
});

slider.el.addEventListener('touchEnd', () => {
    console.log('touching ends');
});

Methods

MethodParamsReturnDescription
addel: string | HTMLElement* index: number options: SliderElementvoidAdds a new element to the slider.
addFirstel: string | HTMLElement* options: SliderElementvoidAdds a new element to the head of the slider.
addLastel: string | HTMLElement* options: SliderElementvoidAdds a new element to the end of the slider.
removeindex: numbervoidRemoves the element at the specified index from the slider.
removeFirstvoidRemoves the first element from the slider.
removeLastvoidRemoves the last element from the slider.
prevPromise\Triggers the previous image. Returns false if the slider is in animation.
nextPromise\Triggers the next image. Returns false if the slider is in animation.
playvoidStarts autoplay.
stopvoidStops autoplay.
togglevoidToggles autoplay.
destroyvoidDestroys the slider.
getIndexnumberReturns index.
setIndexindex: numberPromise\Sets index and animates the slider. Returns false if the slider is in animation.
getTotalnumberReturns total number of images.
getCurrentSliderElementReturns the current element.
getTimingstringReturns timing value.
setTimingtiming: stringvoidSets timing value.
getDurationnumberReturns duration.
setDurationduration: numbervoidSets duration.
getAutoPlaySpeednumberReturns auto play speed.
setAutoPlaySpeedspeed: numbervoidSets auto play speed.

*: You can give an HTML element or a CSS selector (like #carousel, .container > div:first-child)

Slider Element

It is an object that holds one slider element.

Options

id {String} \ Specifies the id of an image.

sliderType {SliderType} \ Specifies the slider type of an image.

before(el:SliderElement, active:boolean): Promise\ \ It is invoked before animation runs. It returns a promise so animation waits for this method to complete. It is only invoked when it is the current or the next element. If it is the next element, active is true.

after(el:SliderElement, active:boolean): Promise\ \ It is invoked after animation runs. It returns a promise so before the method completes, another animation cannot run. It is only invoked when it is the current or the previous element. If it is the current element, active is true.

IE Support

IE 10 is not supported and patches to fix problems will not be accepted.

License

Marvina Slider is provided under the MIT License.

Related Projects

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

6 years ago