This time I will share jQuery Plugin and tutorial about Auto Pass CSS Breakpoints To JavaScript – js.device.selector, hope it will help you in programming stack.
js.device.selector is a JavaScript plugin that detects the device/display type (mobile, tablet, desktop, large-desktop, retina) of a browser and automatically passes the breakpoints defined in the CSS to the JavaScript.
How to use it:
1. Include the minified version of the js.device.selector plugin after jQuery.
1 |
< script src = "/path/to/jquery.js" ></ script > |
2 |
< script src = "/path/to/jquery.device.selector.js" ></ script > |
2. Create the HTML for the device selector.
1 |
< div data-device-selector> |
2 |
< div data-device-selector-item data-device-selector-devicetype = "mobile" ></ div > |
3 |
< div data-device-selector-item data-device-selector-devicetype = "tablet" ></ div > |
4 |
< div data-device-selector-item data-device-selector-devicetype = "desktop" ></ div > |
5 |
< div data-device-selector-item data-device-selector-devicetype = "large-desktop" ></ div > |
6 |
< div data-device-selector-item data-device-selector-displaytype = "retina" ></ div > |
3. Define the breakpoints in the CSS media queries.
01 |
[data-device-selector-item] { |
06 |
only screen and ( max-width : 768px ) { |
07 |
[data-device-selector-devicetype= "mobile" ] { |
08 |
display : block !important ; |
13 |
only screen and ( min-width : 769px ) and ( max-width : 960px ) { |
14 |
[data-device-selector-devicetype= "tablet" ] { |
15 |
display : block !important ; |
20 |
only screen and ( min-width : 961px ) and ( max-width : 1200px ) { |
21 |
[data-device-selector-devicetype= "desktop" ] { |
22 |
display : block !important ; |
27 |
only screen and ( min-width : 1201px ) { |
28 |
[data-device-selector-devicetype= "large-desktop" ] { |
29 |
display : block !important ; |
34 |
only screen and (-webkit-min-device-pixel-ratio: 2 ), |
35 |
only screen and ( min--moz-device-pixel-ratio: 2 ), |
36 |
only screen and ( -o-min-device-pixel-ratio: 2 / 1 ), |
37 |
only screen and ( min-device-pixel-ratio: 2 ), |
38 |
only screen and ( min-resolution: 192 dpi), |
39 |
only screen and ( min-resolution: 2 dppx) { |
40 |
[data-device-selector-displaytype= "retina" ] { |
41 |
display : block !important ; |
4. Initailize the js.device.selector plugin.
5. Get the current device type: mobile || tablet || desktop || large-desktop.
1 |
console.log($.fn.deviceSelector.getDeviceType()); |
6. Get the current display type: retina || undefined.
1 |
console.log($.fn.deviceSelector.getDisplayType()); |
7. The DeviceSelector options.
- selector: The DeviceSelector selector options.
- selector.name: The DeviceSelector selector name for initialisation.
- selector.items: The DeviceSelector selector items for initialisation.
- selector.items.name: The DeviceSelector selector items name for initialisation.
f
- selector.parent: The DeviceSelector selector parent for initialisation.
- selector.parent.name: The DeviceSelector selector parent name for initialisation.
- device: The DeviceSelector device type options.
- device.selector: The DeviceSelector device type selector for initialisation.
- device.selector.name: The DeviceSelector device type selector name for initialisation.
- display: The DeviceSelector display type options.
- display.selector: The DeviceSelector display type selector for initialisation.
- display.selector.name: The DeviceSelector display type selector name for initialisation.
03 |
"name" : ".namespace__m-device-selector" , |
08 |
"name" : ".namespace__m-device-selector__item" , |
13 |
"name" : "data-ds-devicetype" , |