jQuery Detect Environment Plugin
A lightweight jQuery plugin that dynamically adds CSS classes to the <html>
tag based on the device type, operating system, screen size, orientation, browser, internet connection, time of day, color scheme preference, and pixel density. This helps create responsive and adaptive designs easily.
Demo
View Live Demo
Features
The plugin automatically detects and adds classes for:
- Device Type:
device-mobile
or device-desktop
- Operating System:
os-windows
, os-macos
, os-linux
, os-android
, os-ios
- Screen Width (Bootstrap-based):
width-xs
, width-sm
, width-md
, width-lg
, width-xl
- Screen Orientation:
orientation-portrait
or orientation-landscape
- Browser:
browser-chrome
, browser-firefox
, browser-safari
, browser-edge
- Internet Connection Status:
status-online
or status-offline
- Time of Day:
time-morning
, time-afternoon
, time-evening
, time-night
- Color Scheme Preference:
prefers-dark-mode
or prefers-light-mode
- Pixel Density:
pixel-ratio-1x
, pixel-ratio-2x
, or pixel-ratio-3x
Installation
-
Include jQuery in your project (if not already included):
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
-
Include the plugin script (choose between the full or minified version):
Usage
The plugin initializes automatically when the document is ready and dynamically adds classes to the <html>
tag. It also listens for window resize, orientation change, and network connection events to update classes in real-time.
$(document).ready(function() {
$('html').detectEnvironment();
});
The classes added to <html>
will update automatically, so you can use them directly in your CSS for responsive and adaptive design.
Example
With this plugin, the <html>
tag might look like:
<html class="device-desktop os-windows width-lg orientation-landscape browser-chrome status-online time-afternoon prefers-light-mode pixel-ratio-2x">...</html>
Explanation of Classes
Here are some sample classes and what they represent:
- device-mobile / device-desktop: Based on device type (mobile or desktop).
- os-windows, os-macos, os-linux, os-android, os-ios: Operating system detected.
- width-xs, width-sm, width-md, width-lg, width-xl: Screen width based on Bootstrap breakpoints.
- orientation-portrait / orientation-landscape: Current screen orientation.
- browser-chrome, browser-firefox, browser-safari, browser-edge: Browser type.
- status-online / status-offline: Internet connection status.
- time-morning, time-afternoon, time-evening, time-night: Time of day for time-based styling.
- prefers-dark-mode / prefers-light-mode: User’s color scheme preference (from system settings).
- pixel-ratio-1x, pixel-ratio-2x, pixel-ratio-3x: Screen pixel density, for handling high DPI displays.
Events
The plugin listens for the following events to update classes dynamically:
- Window Resize: Adjusts width classes and orientation as the window size changes.
- Orientation Change: Updates orientation classes when the device orientation changes.
- Online/Offline: Changes connection status classes depending on network status.
License
This project is licensed under the MIT License.
This portion of the README.md
file provides examples and explanations of the classes generated by the plugin, describes the events it listens for, and includes a brief note on the license. It’s designed to help users understand how to work with the classes generated by the plugin for responsive design.