Created
June 7, 2018 14:59
-
-
Save DuckDivers/062df3a7434456ec11ec4ad57157e283 to your computer and use it in GitHub Desktop.
Bootstrap 4 Affix - Add data-toggle="affix" to element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.affix { | |
position: fixed; | |
top: 0; | |
right: 0; | |
left: 0; | |
z-index: 1030; | |
} | |
/* fixed to top styles */ | |
.affix.navbar { | |
background-color: #333; | |
} | |
.affix.navbar .nav-item>a, | |
.affix.navbar .navbar-brand { | |
color: #fff; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var toggleAffix = function(affixElement, scrollElement, wrapper) { | |
var height = affixElement.outerHeight(), | |
top = wrapper.offset().top; | |
if (scrollElement.scrollTop() >= top){ | |
wrapper.height(height); | |
affixElement.addClass("affix"); | |
} | |
else { | |
affixElement.removeClass("affix"); | |
wrapper.height('auto'); | |
} | |
}; | |
$('[data-toggle="affix"]').each(function() { | |
var ele = $(this), | |
wrapper = $('<div></div>'); | |
ele.before(wrapper); | |
$(window).on('scroll resize', function() { | |
toggleAffix(ele, $(this), wrapper); | |
}); | |
// init | |
toggleAffix(ele, $(window), wrapper); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment