Correcting in page navigation position when using fixed navbar - html

I am using a fixed navbar with bootstrap 4. When using fixed-top navbar, content below it is hidden by the navbar because its position is fixed. I had to give padding-top: 65px; on the body, to make the content appear below the navbar.
I have internal links so clicking on a navbar anchor positions the page on the section relative to it. However, because I used the padding-top trick, the position is 65px below the top of the section. Is there a way to solve it so that position returns to the top of the section?

You should not add from the first " fixed-nav" class. you can add this in scroll event of jquery.
$(window).on('scroll', function () {
var scrollTop = 20;
if ($(window).scrollTop() >= scrollTop) {
$('nav').addClass("fixed-nav");
}
if ($(window).scrollTop() < scrollTop) {
$('nav').removeClass('fixed-nav');
}
});
this add fixed-nav in user scroll down in your site.

On click of a link, I set the padding top of the body to 0, and on hash change event after the page positions on the section relative to the inline link, I set back the padding to 65.
$('.nav-link').click(function () {
$('body').css('padding-top', '0');
});
window.onhashchange = function () {
$('body').css('padding-top', '65px');
};

$(document).ready(function() {
var top_pad = $('.your_headerelem').height();
$('.first-emem-after-header').css({'padding-top':+top_pad+'px'});
$(window).resize(function(){
var top_pad = $('.your_headerelem').height();
$('.first-emem-after-header').css({'padding-top':top_pad+'px'});
}) ;
}) ;
This will make your first element after header, lie below header.

Related

Fixed a navbar from bootstrap

I used the property navbar-fixed-top to fixed my navbar to the top. But I have an other fixed element we don't see.
This is my fixed navbar and under it, the fixed element I want to see.
What is happening is that my element (just call it "line") is fixed by the options but the position of the navbar is not absolute, so the line pass under the navbar and when I scroll down, the line is well fixed but it's hidden by the navbar. I don't know if it's clear but I want this two element to be glued and not superimposed.
How should I do ?
EDIT :
My jsfiddle : http://jsfiddle.net/5Zv8h/13/
If you try to remove th enavbar, you'll see that the first line stays on the top of the page. With the navbar, it's hidden.
I have written script to fix this, add this to Your code:
js
var navheight = $(".navbar-fixed-top").innerHeight();
var offtop = $(".ht_clone_top").offset().top;
$(window).scroll(function(){
var scrolltop = $(window).scrollTop();
if(scrolltop + navheight >= offtop){
if(!($(".ht_clone_top").hasClass("affixed"))){
$(".ht_clone_top").addClass("affixed");
}
if(offtop>= scrolltop){
console.log("now");
$(".ht_clone_top").css({
"top": scrolltop - 40
});
} else {
$(".ht_clone_top").css({
"top": 50
});
}
} else {
$(".ht_clone_top").removeClass("affixed");
$(".ht_clone_top").css({
"top": 0
});
}
});
css
.affixed {
z-index: 99999;
position: absolute;
}
demo:
http://jsfiddle.net/5r9p380n/2/

Make DIV bigger if scrolled

Is it possible to make DIV bigger when content of the page is scrolled? I have chatbox with position:fixed on the right side of my page. My CSS for the chat box is height:100% right:0 bottom:0 top:50px. The top:50px is because I don't want it to hide the navigation bar on top of my page. Now the problem is, that when I start scrolling the page, the navigation bar obviously disappear from sight and there is 50px high blank space on top of my chatbox. What I want is that when I start scrolling the page, the chatbox should take the whole 100% of the screen, so that there is no blank space on top of it.
you may do this:
$(document).ready(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('.chat_box').css({top:'0px'});
}
else {
$('.chat_box').css({top:'10px'});
}
});
});
http://jsfiddle.net/5tnygmrz/1/
demo - http://jsfiddle.net/victor_007/cq1e8c1t/
i think you will need javascript
window.onscroll = function (e) {
var topscroll = window.scrollY
if (topscroll > 50) {
document.getElementById('fixed').style.top = 0
} else {
document.getElementById('fixed').style.top = 50 + 'px'
}
}

Responsive site menu

http://nggalaxy.ru/en/about-us/ - here is the example of exact behaviour to be implemented. When you scroll down the page, the side menu disappears and appears on top of the page.
How it can be realized using bootstrap for example?
Thank you.
Here is an example of raw Javascript + jQuery making this happen:
<script type="text/javascript">
$(window).scroll(function(){
var a = 112;
var pos = $(window).scrollTop();
if(pos > a) {
$("menu").css({
position: 'fixed'
});
}
else {
$("menu").css({
position: 'absolute',
top:'600px'
});
}
});
</script>
This will add a CSS style if the user scrolled 112 pixel down. Like this you can create all styles for your menu.
In General: Use javascript to check on what scroll-position the user is, and append the styles or classes.

Issues with width of "floating" link section

I have a list of links on the sidebar of my website that starts out as position: relative, and then when you scroll past the header, it changed to position: fixed. It does so with a short jQuery script that checks the height of the window on scroll and changes the class from "relative" to "fixed" if the height is greater than the height of my header, and corresponding classes in my CSS that change the position attribute. The issue I am having is with the width of the element containing the links. When it is relative, I have the width set to 100% so it fills the sidebar. When it is fixed, width is relative to the window so I set it to width: 25%, which was a close approximation. It worked fine on my screen, however when I tried it on a larger screen the element overlapped the main content area. I then tried changing the CSS to something like the following:
.fixed {
position: fixed;
left: 15%;
right: 70%;
}
But again, it didn't work properly on a large screen. Any ideas on what to try next? Thanks so much!
EDIT: I tried adding this to my script but it still isn't working:
$(window).scroll( function() {
if($(window).scrollTop() > 150){
//begin to scroll
var links = $("#project-links");
links.attr('class', 'fixed');
var windowWidth = $(window).width();
var newLeft = .075 * windowWidth;
var newRight = .68 * windowWidth;
var strLeft = newLeft.toString() + 'px';
var strRight = newRight.toString() + 'px';
links.css({'left': strLeft, 'right': strRight, 'width': ''});
} else {
//lock it back into place
var links = $("#project-links");
links.attr('class','relative');
links.css({'left': '', 'right': '', 'width': '100%'});
}
});
EDIT 2:
Finally fixed it, I was being stupid. Here is what did the trick:
if($(window).scrollTop() > 150){
//begin to scroll
var links = $("#project-links");
links.attr('class', 'fixed');
links.css('width', links.parent().width());
}
Indeed your .fixed container is relative to the window when the position property is set to fixed. Percentage does not help you here, you need absolute values. You have already a script embedded, it could use the width of the parent element and apply it to the .fixed element when resizing.

How can you z-index text / div behind a scrolling fixed nav header?

I want to put the div behind the scrolling header like it is for the footer.
The footer is
#rt-footer-surround {
color: #686868;
background-color: #2E244E;
height: 40px;
width: 100%;
z-index: 900;
bottom: 0;
left: 0;
padding: 10px;
position: fixed;
}
but I cannot replicate it for the header.
the site in question is here:
site with z-index issue
To use the z-index style, you must also use either position:fixed;, position:absolute;, ,or position:relative; on the same object you wish to style.
Then, you can simply use a div to represent your header, footer, and main content section as follows:
<div class="head"></div>
<div class="mainbody"></div>
<div class="foot"></div>
Or alternatively, you can use the <head>,<main> and <footer> tags instead of divs. As far as coding and visuals are concerned, there is no difference.
Also, you don't have to put a massive number on the z-index. As long as one element's z-index is greater than another one's, that element will always be layered above, whether it is 900 over 1, or 2 over 1.
Ok here is what I came up with to solve the problem. Jquery.
Essentially my question was asking for this in the first place.
If you have content in a div you can place a nav bar in that div as a position:relative i.e. relative to that div.
What you cannot do via css is have the content in that div scroll up and stay underneath the nav bar you created. Furthermore when the nav menu area scrolls beyond the top of the screen it will then disappear.
So the jquery code I used does two things. It allows you to take a nav menu bar i.e. width 600px / height 50px and place it in its relative position anywhere you like. Furthermore, when it reachs the top of a users screen it will stop/halt and allow that to be the menu that is visible while everything else scrolls underneath that menu area.
Now, I don't think this is anything really new from Jquery but what is ultra convenient is that you can define a menu nav bar in any div position you want. Have a regular menu at the top and another menu perhaps below a slider or some content further down the page.
I will share the code if that is ok with SO... I paid for it myself.
Oh and here are two websites I have employed it on.
http://isaerudition.com/study-pages &
This is for a client I am preparing his website...
// JavaScript Document
/* jQuery(document).ready(function(){
if(jQuery('header,div,p,span,h1,h2,h3,h4,a').hasClass('isa-scroll-fixed')){
var el = jQuery('.isa-scroll-fixed'),
elTop = jQuery(el).offset().top;
elLeft = jQuery(el).offset().left;
//alert(elTop);
jQuery(document).scroll(function(){
var height = jQuery(window).height();
var scrollTop = jQuery(window).scrollTop();
if(scrollTop>=elTop){
//add fixed
jQuery(el).addClass('scroll_fixed').css("left",elLeft+"px");
}else{
//clear fixed
jQuery(el).removeClass('scroll_fixed').attr('style','');
}
})
}
})
*/
// JavaScript Document
/* jQuery(window).load(function(){
if(jQuery('header,div,p,span,h1,h2,h3,h4,a').hasClass('isa-scroll-fixed')){
var el = jQuery('.isa-scroll-fixed'),
elTop = jQuery(el).offset().top;
elLeft = jQuery(el).offset().left;
//alert(elTop);
var scrollTop = jQuery(window).scrollTop();
scrollFixed(el,elTop,elLeft);
}
}) */
var setInter = null;
var session = null;
setInter = setInterval(function(){
if(jQuery('header,div,p,span,h1,h2,h3,h4,a').hasClass('isa-scroll-fixed')){
var el = jQuery('.isa-scroll-fixed');
session = jQuery(el).attr('set-scroll');
//alert(session);
if(session == '2'){
jQuery(el).attr('set-scroll','2');
}else{
jQuery(el).attr('set-scroll','1');
}
if(session == '1'){
setValue(el);
}
}
}, 200);
function setValue(el){
var setScroll = jQuery(el).attr('set-scroll');
elTop = jQuery(el).offset().top;
elLeft = jQuery(el).offset().left;
//alert(elTop);
jQuery(el).attr('set-scroll','2');
scrollFixed(el,elTop,elLeft);
};
function scrollFixed(el,elTop,elLeft){
jQuery(document).unbind('scroll').scroll(function(){
//alert(elTop);
var height = jQuery(window).height();
var scrollTop = jQuery(window).scrollTop();
if(scrollTop>=elTop){
//add fixed
jQuery(el).addClass('scroll_fixed').css("left",elLeft+"px");
}else{
//clear fixed
jQuery(el).removeClass('scroll_fixed').attr('style','');
}
})
}