Skip to content

Instantly share code, notes, and snippets.

@webbingstudio
Last active January 14, 2018 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webbingstudio/afe5e8056cdae8a4f1c62ac08404a3bd to your computer and use it in GitHub Desktop.
Save webbingstudio/afe5e8056cdae8a4f1c62ac08404a3bd to your computer and use it in GitHub Desktop.
対象となる要素がスクロールによって画面から消えたときに、.is-active を付与する
/*
jquery_wsfixnav.js
--------------------
https://gist.github.com/webbingstudio/afe5e8056cdae8a4f1c62ac08404a3bd
Author: WebbingStudio
Released under the MIT license
http://opensource.org/licenses/MIT
*/
;(function($){
"use strict";
var WsFixNav = window.WsFixNav || {};
WsFixNav = (function() {
function WsFixNav(element, options) {
var
t = this;
t.initials = {
et: 0
};
t.settings = $.extend({}, t.initials, options);
t.init(element);
t.run(element);
window.onscroll = function() {
t.run(element);
};
return false;
}
return WsFixNav;
}());
WsFixNav.prototype.init = function(element) {
var
t = this;
t.et = $(element).position().top;
return false;
};
WsFixNav.prototype.run = function(element) {
var
t = this,
st;
st = $(window).scrollTop();
if (st > t.et) {
$(element).addClass('is-active');
} else {
$(element).removeClass('is-active');
}
return false;
};
$.fn.WsFixNav = function() {
var
element = this,
options = arguments[0],
args = Array.prototype.slice.call(arguments, 1),
i;
for (i = 0; i < element.length; i++) {
if (typeof options == 'object' || typeof options == 'undefined') {
element[i].WsFixNav = new WsFixNav(element[i], options);
}
}
return element;
};
$(function(){
$('.js-fixnav').WsFixNav();
});
})(jQuery);
@webbingstudio
Copy link
Author

webbingstudio commented Jan 14, 2018

Usage:

HTML
<div class="globalnav js-fixnav">(navigation)</div>

CSS
.globalnav.is-active { position: fixed; top: 0; left: 0; width: 100%; z-index: 999; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment