1.3.0 • Published 4 years ago

@mrbakieness/npm_js_selector v1.3.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

JavaScript Selector

Selector

Install

npm install @mrbakieness/npm_js_selector --save-dev

Useage

To use the module first you have to import the module into your main JavaScript file.

import "../node_modules/@mrbakieness/npm_js_selector/index.js";

This libary has multiple functions that can be used to make javascript easier to write. Loosely based on jQuery but much more lightweight.

MethodsArgumentsDescription
readycallbackDocument ready function. All other JS code goes in here.
onevent, callback, options (optional)Function to add an event to elements.
forEachcallbackFunction to loop through elements
addClassclassAdds a class to a given element or list of elements.
removeClassclassRemoves a class to a given element or list of elements.
modalnoneCreates a pop up modal
slideposition, timeSlides element horizontally to center of page
fadeOuttimeFades an element out based on time given

.ready(callback)

Basic ready function.

$_(document).ready(() => {
    console.log('document ready!');
    //code goes here
})
ArgumentsDescription
callbackAll code to be executed after document has loaded

.on(events, callback, options)

Function to add event listener to elements.

    $_('.element').on('click', (e) => {
        e.target.style.color = 'red';
    })
ArgumentsDescription
eventEvent to listen for
callbackFunction to be executed on event
optionsJSON object of options

The following are the current option arguments that can be passed.

OptionsDescription
onceExecute event only once. Type bool default false

If once: true is being used the following needs to be include within the callback so that the event only happenes once on IE11.

if (IE_once(e) == true) {
    //code goes here ...
}

A full example:

    $_('.element').on('mouseover', (e) => {
        if (IE_once(e) == true) {
            e.target.style.color = 'red';
        }
    }, { once: true })

.forEach(callback)

Function to loop through elements and executes code for each element.

$_('.elements').forEach((el) => {
    el.classList.remove('active');
})
ArgumentsDescription
callbackFunction to execute on each element

.addClass(class)

Adds a class to an element or list of elements.

$_('.elements').addClass('class');
ArgumentsDescription
classClass to add to element

.removeClass(class)

Removes a class to an element or list of elements.

$_('.elements').removeClass('class');
ArgumentsDescription
classClass to remove to element

.modal()

Creates a modal based on element ID. The Element need to have the attribute data-modal-title.

<div id="element" data-modal-title="Basic dialog">
    <div class="modal__content">
        <p>This is the default dialog.</p>
    </div>
</div>
$_('#element').modal();

.slide(position, time)

Horizontal slide function to move elements left to right based on position.

$_('.element').slide(toCenter, 500);
ArgumentsDescription
positionPosition for element to slide in pixels
timeTime to slide elements in milliseconds

.fadeOut(time)

Fades out and element in a given time.

$_('.element').fadeOut(2000);
ArgumentsDescription
timeTime to fade out in milliseconds