😁Importing CSS Breakpoints Into JavaScript (ok)
https://www.lullabot.com/articles/importing-css-breakpoints-into-javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<p>The breakpoint (as known to the JS) is <span class="breakpoint">Breakpoint</span>.</p>
<p>This works on IE9+ and all other browsers that support media queries.</p>
<p>The article describing this technique can be found on <a href="https://www.lullabot.com/blog/article/simpler-solution-bring-css-breakpoints-javascript" target="_blank">Lullabot.com</a>.</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="test.js"></script>
</body>
</html>/**
* These values will not show up in content, but can be
* queried by JavaScript to know which breakpoint is active.
* Add or remove as many breakpoints as you like.
*/
body:before {
content: "smartphone";
display: none; /* Prevent from displaying. */
}
@media (min-width: 550px) {
body:before {
content: "smartphone_wide";
}
}
@media (min-width: 650px) {
body:before {
content: "tablet_narrow";
}
}
@media (min-width: 900px) {
body:before {
content: "tablet_wide";
}
}
@media (min-width: 1100px) {
body:before {
content: "desktop";
}
}
@media (min-width: 1500px) {
body:before {
content: "wide";
}
}
@media (min-width: 2000px) {
body:before {
content: "OMG_THATS_SO_HUGE";
}
}
/* A bit of fluff */
::selection {
background: #ca322c;
color: white;
}
body {
font-family: Georgia, 'Times New Roman', Times, serif;
font-size: 1.16em;
color: #4b4b4b;
margin-top: 50px;
}
p {
width: 80%;
max-width: 600px;
padding-left: 10%;
}
a {
color: #ca322c;
}
.breakpoint {
background-color: #ca322c;
color: white;
display: inline-block;
padding: 2px 4px;
}Importing CSS Breakpoints Into JavaScript
A Simpler Solution
Declare your breakpoints
Importing the Breakpoints into JavaScript
Trigger on resize and page load
In use
Sample Use Case
1. Get the current breakpoint
2. Function to pass the number of columns to leaderboardMoveHelper()
3. Helper function to move the leaderboard to the appropriate location within the DOM
4. Run the JS on browser resize and page load
Conclusion
PreviousjQuery 1.9 .live,.delegate,.on is not a function (ok)NextThe matchMedia() method returns a MediaQueryList with the results from the query. (ok)
Last updated
