Skip to content

Commit c7c5ba0

Browse files
authored
Automatically set navbar light/dark based on color (daattali#702)
1 parent 5ce0f3a commit c7c5ba0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased version
22

3-
- Add YAML option `head-extra` which is similar to `footer-extra` but is used to include custom HTML code in a page's `<head>` tag
3+
- Add YAML option `head-extra` which is similar to `footer-extra` but is used to include custom HTML code in a page's `<head>` tag
4+
- Add automatic navbar color detection (#702)
45

56
## v4.1.0 2020-08-08
67

assets/js/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ var BeautifulJekyllJS = {
66
numImgs : null,
77

88
init : function() {
9+
// Set the navbar-dark/light class based on its background color
10+
const rgb = $('.navbar').css("background-color").replace(/[^\d,]/g,'').split(",");
11+
const brightness = Math.round(( // http://www.w3.org/TR/AERT#color-contrast
12+
parseInt(rgb[0]) * 299 +
13+
parseInt(rgb[1]) * 587 +
14+
parseInt(rgb[2]) * 114
15+
) / 1000);
16+
if (brightness <= 125) {
17+
$(".navbar").removeClass("navbar-light").addClass("navbar-dark");
18+
}
19+
920
// Shorten the navbar after scrolling a little bit down
1021
$(window).scroll(function() {
1122
if ($(".navbar").offset().top > 50) {

0 commit comments

Comments
 (0)