npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

contraster

v0.2.0

Published

New JS library for create a before-after effect

Downloads

11

Readme

Contraster

Vanilla JS library for create before-after effect.

Demo

Installation

Install from NPM

npm i contraster
import Contraster from "contraster";
import "contraster/contraster.css";
const slider = new Contraster({
  container: "#horizontal"
});

Download assets

If you want to use library assets locally, you can directly download CSS and JS files from unpkg.com

Also support versions:

Contraster ES5

Contraster IE11

Add styles

<head>
...
<link rel="stylesheet" href="path/to/contraster.css">
...
</head>

Add scripts

<footer> ... </footer>
<script src="path/to/contraster.js"></script>

HTML Layout

<div id="mySlider"
     data-before="./img/ba-2-1.jpg"
     data-after="./img/ba-2-2.jpg">
</div>

Initialize

// index.js
const mySlider = new Contraster({
    container: '#mySlider',
    direction: 'diagonal',
    freePosition: true,
    'separator': {
        'class': 'custom-separator',
    },
});

Separator CSS styles

:before and :after

You can add before/after pseudo-elements:

// index.css
.custom-separator {
    position: relative;
    width: 10px;
}

.custom-separator:before {
    content: '::';
    width: 20px;
    height: 40px;
    position: absolute;
    background-color: #fff;
    border-radius: 6px;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: 100%;
}

Custom inner HTML-layout

Or you can append yout HTML-layout in separator:

// index.js
const separatorInnerHTML = `
    <span class="span1"></span>
    <span class="span2"></span>
    <span class="span3"></span>
`;

const mySlider = new Contraster({
    'separator': {
        'class': 'custom-separator',
        'innerHTML': separatorInnerHTML,
    },
});
// index.css
.separator-custom-inner .span1,
.separator-custom-inner .span2,
.separator-custom-inner .span3 {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    display: block;
}
.separator-custom-inner .span1 {
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-right: 10px solid #333;
    transform: translate(-200%,-50%);
}
.separator-custom-inner .span2 {
    width: 20px;
    height: 10px;
    background-color: #333;
    transform: translate(-50%,-50%);
}
.separator-custom-inner .span3 {
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-left: 10px solid #333;
    transform: translate(100%,-50%);
}

Parameters

| Name | Type | Default | Description | |------------------------|--------------------------------------------------|----------------------------------------|----------------------------------------------------------------------------| | container | CSSSelector | HTMLElement | null | String with CSS selector or HTML element | | className | string | null | Class for slider container | | direction | "vertical" | "horizontal" | "diagonal" | "vertical" | "Slider direction. Can be "vertical" or "horizontal" or "diagonal" | | init | boolean | true | Automatically initialize | | freePosition | boolean | false | Handle interaction (LMB click or touch) not only with the separator | | progressPosition | number | 50 | percent | | separator | object | null | separator options | | separator.class | string | null | Custom class for separator | | separator.innerHTML | string | null | Custom HTML in separator | | separator.activeClass | string | "before-after-active-separator" | This class set when user interactive with slider |

Methods

| Name | Arguments | Description | |-------------|----------------------------------|---------------------------------------------------------------| | init | - | Initialize slider after destroy or with parameter init: false | | destroy | - | Destroy slider | | setProgress | percent | percent - number (example) | | on | "event, handler" | Add event handler | | off | "event, handler" | Remove event handler | | emit | "event, args" | Fire event on instance |

Examples

Initialize on demand

Create slider with disable automatically initialize:

const slider = new Contraster({
    container: '#slider',
    init: false
});

Initialize slider when it took:

slider.init();

Initialize after destroy

Destroy slider:

slider.destroy();

Initialize slider when it took:

slider.init();

Setting progress

slider.setProgress(30); // will set progress = 30%

Fire event

You can look all default events on the Events paragraph.

slider.emit('your-event');

Event listener

function myEventCallback(event) {
    console.log('my event fire! ', event)
}
slider.emit('your-event', myEventCallback);

Remove event listener

slider.off('your-event', myEventCallback);

Events

| Name | Description | |-------------|-----------------------------------------| | init | Event will fired after initialize | | destroy | Event will fired after destroy | | setProgress | Event will fired after progress setting | | setSizes | Event will fired after sizes setting |

Examples

Create slider:

const slider = new Contraster({
    container: '#slider',
});

Add event listeners with "on" method:

slider.on('init', function() {
    alert('Slider was initialize!');
})
slider.on('destroy', function() {
    alert('Slider was destroy!');
})
slider.on('setProgress', function() {
    alert('Slider was change!');
})
slider.on('setSizes', function() {
    alert('Slider was set sizes!');
})

Remove event listeners with "off" method:

Create callback:

function callback(event) {
    console.log('Slider was initialize ', event);
}

Add listener:

slider.on('init', callback);

Remove listener:

slider.off('init', callback);

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

History

12.12.21

Publish v0.1

License

MIT License

Copyright (c) 2021 timshaq