1.0.0 • Published 4 years ago

responsive-slider v1.0.0

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

CSS Media query in JS

npm npm

Installation

npm install js-media-query

Usage

import Media from "js-media-query";
import { up, sm, md } from "js-media-query";

See Examples

API

MethodDescription
queryCheck media query matching
minMinimum breakpoint width
maxMaximum breakpoint width
upMinimum breakpoint width
downMaximum breakpoint width
betweenCheck if screen between the from and to breakpoints
isTouchCheck if is touck device
isMouseCheck if is non-touch device
xsCheck if screen is bigger or equal to xs class defined breapoint
smCheck if screen is bigger or equal to sm class defined breapoint
mdCheck if screen is bigger or equal to md class defined breapoint
lgCheck if screen is bigger or equal to lg class defined breapoint
xlCheck if screen is bigger or equal to xl class defined breapoint
setBreakpointsSet Local class breapoints

Events

on(event, callback, media)

PropValueRequiredDescription
eventin , out , changedyesEvent type
callbackfunctionyesCallback function reference
mediafunctionyesFunction reference what return result of required media query

off(event, callback?, media?)

PropValueRequiredDescription
eventin , out , changedyesEvent type
callbackfunctionyesCallback function reference
mediafunctionyesFunction reference what return result of required media query

Examples

Basic Usage

import { min } from "js-media-query";

window.addEventListener("resize", () => {
  if (min(992)) {
    console.log("Screen size is equals or bigger than 992px");
  } else {
    console.log("Screen size is lower than 992px");
  }
});

Using with default breakpoints

import Media from "js-media-query";

if (Media.sm()) {
  // Your code
}

Using with custom breakpoints

import { Media } from "js-media-query";

const media = new Media({
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
});

if (media.md()) {
  // Execute code if screen size is bigger or equals to 768px
}

Event listening

import { Media } from "js-media-query";

const media = new Media();

const changedHandler = () => {
  console.log("changed");
};

const mediaFn = () => media.up(768);

// Add event listener
media.on("changed", changedHandler, mediaFn);

// Remove all event listeners of event type
media.off("changed");

// Remove event listener of event type
media.off("changed", changedHandler);

// Remove event listener of event type on with specific media function
media.off("changed", changedHandler, mediaFn);

@media (min-width: 768px) AND (max-width: 992px)

Media.between(768, 992); // true | false

@media (pointer: coarse)

Media.isTouch(); // true | false

@media (min-width: 992px)

Media.min(992); // true | false

@media (max-width: 991px)

Media.down(991); // true | false

@media (min-width: 1200px)

Media.xl(); // true | false