• Stars
    star
    3,669
  • Rank 11,992 (Top 0.3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Switch your nav's design on the fly

Midnight

A jQuery plugin that switches between multiple header designs as you scroll, so you always have a header that looks great with the content below it.

Check out the demo (watch the logo as you scroll).

Installation

Midnight is available on Bower and NPM as midnight.js:

bower install midnight.js

npm install midnight.js

Quick start

Create your fixed nav (or header) as you typically would. For an example, something like this (you can use whatever markup suits you)

<nav class="fixed">
  <a class="logo">Logo</a>
</nav>

Make sure the header works well with position:fixed

After that, take any sections of your page that need a different nav and add data-midnight="your-class" to it, where your-class is the class you are going to use to style that header. If you don't use the property or just leave it blank, the .default header will be used for that section.

<section data-midnight="white">
  <h1>A section with a dark background, so a white nav would look better here</h1>
</section>

<div data-midnight="blue">
  <h1>A blue nav looks better here</h1>
</div>

<footer>
  <h1>This will just use the default header</h1>
</footer>

Multiple headers as necessary will be created based on the classes declared in these sections.

You can style it in your css using the class .midnightHeader.your-class (replace your-class with the correct one). For example:

.midnightHeader.default {
  background: none;
  color: black;
}
.midnightHeader.white {
  background: white;
  color: black;
}
.midnightHeader.blue {
  background: blue;
  color: white;
}
.midnightHeader.red {
  background: red;
  color: white;
}

To initialize, just load midnight and initialize it

<script src="midnight.jquery.js"></script>
<script>
  // Start midnight
  $(document).ready(function(){
    // Change this to the correct selector for your nav.
    $('nav.fixed').midnight();
  });
</script>

Using custom markup

Let's say you want to create a special header with a butterfly in it, which needs some extra markup. You need to do two things:

  • First, add a div with the class .midnightHeader.default . This will be the header that's used for every section (that doesn't have a specific style) and duplicated as necessary, automatically replacing .default with the correct class.

  • Then, add a div with the class .midnightHeader.your-class (like .butterfly). This will be used in that case instead, so you can use some custom markup in that case. Repeat this step for any other header with custom markup.

  • Keep in mind that all headers need to be the same height. Take that into account when styling your headers. If you have one that's larger than usual, we recommend you make all the headers the same height and try to handle it with additional markup.

<nav class="fixed">
  <!-- Your standard header -->
  <div class="midnightHeader default">
    <a class="logo">Logo</a>
  </div>

  <!-- A header with a butterfly -->
  <div class="midnightHeader butterfly">
    <a class="logo">Logo</a>
    <span class="a-butterfly"><!-- Everybody loves butterflies! --></span>
    <span class="another-butterfly"><!-- OH GOD THEY ARE IN MY FACE --></span>
    <span class="yet-another-butterfly"><!-- AAAAAHHHHHHHHHHHHHHHHHHHHH --></span>
  </div>
</nav>

Options

You can use a variety of custom options when using midnight:

$('nav').midnight({
  // The class that wraps each header. Used as a clipping mask.
  headerClass: 'midnightHeader',
  // The class that wraps the contents of each header. Also used as a clipping mask.
  innerClass: 'midnightInner',
  // The class used by the default header (useful when adding multiple headers with different markup).
  defaultClass: 'default'
});

Running Multiple Instances with Different Breakpoints

If you want to run multiple instances of midnight with different breakpoints, you can use the sectionSelector option to choose where each nav is going to be split.

By default, the plugin will look for all the sections with the data-midnight attribute, which is the default, but you can change this to suit your needs. For example:

$('nav.one').midnight({
  // By default, sectionSelector is 'midnight'. It will switch only on elements that have the data-midnight attribute.
  sectionSelector: 'midnight'
});

$('nav.two').midnight({
  // We want this nav to switch only on elements that have the data-noon attribute.
  sectionSelector: 'noon'
});

CDN Hosting

To lower hosting costs & have users load midnight.js faster, you can use the jsDelivr CDN like so:

<script src="//cdn.jsdelivr.net/npm/midnight.js@{version}/dist/midnight.jquery.min.js"></script>

JSDelivr

You can use jsDelivr's version aliasing & concocting to serve the latest minor branch version along with dependancies. For example, to download the latest patch versions of midnight.js v1.0.z together along with jQuery v1.11.z:

<script src="https://cdn.jsdelivr.net/combine/npm/[email protected],npm/[email protected]"></script>

Known Issues

On iOS <7 and older Android devices scrollTop isn't updated fluently, which creates a choppy effect. It can be fixed somewhat by wrapping the body in container and detecting touch events, but we're leaving that as an open issue. We'll probably disable the effect on older mobile devices due to bad performance.

You shouldn't add any sort of padding, margin or offset (top/left/right/bottom) to the nav, since it causes issues with rendering.

More Repositories

1

blockrain.js

HTML5 Tetris Game for jQuery
JavaScript
954
star
2

readremaining.js

How long you'll need to read an article
JavaScript
553
star
3

nextjs-pwa

Build a PWA with Next.JS
JavaScript
142
star
4

setup

Make your Mac Aerowesome
Python
134
star
5

frontend-developer-coding-challenge

Are your looking for a remote developer job? Solve this frontend developer challenge and show us what you can do and what you are an expert at!
125
star
6

rooms

A simple Display for Meeting Rooms
JavaScript
102
star
7

pixelcounterapp

How many pixels have you crafted?
HTML
68
star
8

booom

Dribbble Enhancement Suite
JavaScript
51
star
9

lullaby

A treatment for tinnitus
JavaScript
44
star
10

youtube-tv

Build a desktop app with Node Webkit
JavaScript
42
star
11

subtle-animations

How to add simple UI details
CSS
35
star
12

coding-challenges

Show your abilities as a developer
HTML
35
star
13

welcome

A macOS app to onboard new Aerolabers
JavaScript
34
star
14

platzi-pwa

Material de PWA en Platzi
JavaScript
28
star
15

acanohayluz

Reporte de cortes de luz en Argentina
PHP
20
star
16

workshop-nextjs

Aprendiendo Next.JS
JavaScript
16
star
17

pixelcounter

Count the pixels in all the images in a folder
JavaScript
15
star
18

svg

The SVG Workflow
HTML
15
star
19

aerolab.github.io

Open Source Projects by Aerolab
JavaScript
13
star
20

activate-mac

Activate Windows. For Mac.
JavaScript
12
star
21

criterios

Criterios de Calidad de Aerolab
HTML
11
star
22

fullstack-developer-coding-challenge

HTML
11
star
23

aerolab-cli

A tool for automating dev tasks
JavaScript
10
star
24

perfect

The only perfect piece of software
9
star
25

platzi-nextjs

Material de Next.JS en Platzi
JavaScript
9
star
26

iojs-colorpicker

A color picker for our IO.JS logo
JavaScript
8
star
27

nokia-psd

The long-awaited PSD for Nokia 1100
SCSS
7
star
28

type

Workshop de tipografía
CSS
7
star
29

jsconf2014-workshop

Node-Webkit Workshop for JSConf
JavaScript
7
star
30

meeting-quantifier

How much did you spend on that meeting?
CSS
6
star
31

goldenimage.js

Fix camera rotation of an image
JavaScript
6
star
32

iojs-logo

IO.JS Logo Proposal
6
star
33

nextjs-routing

Routing y Layout avanzado con Next.JS
JavaScript
5
star
34

rosarigasino

Egasel mejogasor lenguagasaje degasel mugasundo
JavaScript
5
star
35

zeplin-renamer

Rename Zeplin images for React Native
Python
5
star
36

nextjs-intro

Introducción a Next.JS y Styled JSX
JavaScript
5
star
37

origami

Workshop de Origami
3
star
38

startupmap

Mapping Startups in Latin America
CSS
3
star
39

tutorial-navigator

A tutorial for Auth0
JavaScript
1
star
40

meeetup

Buenos Aires Dribbble Meetup 2014
1
star
41

next-sanity-starter

Next.js starter with Santy
TypeScript
1
star