I am using fixed bootstrap responsive navigation on a page with fixed width container, i.e. on small devices page itself has a horizontal scroll, but navigation adopts to a page width.
One of the buttons in navigation has a popover. The problem is: popover is rendered incorrectly or completely disappears when you scroll main page to the right.
HTML
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>Dummy 1</li>
<li>Dummy 2</li>
<li>
Contact Popover
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div id="main" class="container">
<div class="row">
<h1>Dummy 1</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>
</div>
CSS
body {
padding-top: 50px;
width: 1700px;
}
Here is an example: https://jsfiddle.net/nnxfk7s6/8/
Run snippet
Press "Contact Popover" to see the popover
Scroll page to the right
Press "Contact Popover" again to see not fully rendered popover
I tried to play with container and viewport properties of the popover, but it did not resolve a problem. (http://getbootstrap.com/javascript/#popovers-options)
So I ended up with a little bit of javascript code to adjust popover position.
we need to set popover trigger to manual
we have to set width or min-width for popover itself
we know that small arrow position above popover is always correct, we can use arrow offset to calculate correct popover position
Here is the code:
$(function () {
$('[data-toggle="popover"]').popover();
$('body').on("click", '[data-toggle="popover"]', function (e) {
e.preventDefault();
if (!$(this).attr('aria-describedby')) {
$(this).popover("show");
// aria-describedby attribute keeps id of the popover
var $pop = $('#' + $(this).attr('aria-describedby'));
if($pop) {
// find arrow element within popover to obtain its offset
var $arrow = $pop.find('.arrow');
// set new left position for the popover
$pop.css('left', $arrow.offset().left - $pop.width()*0.75);
// set new arrow position
$arrow.css('left', '75%');
}
} else {
$(this).popover("hide");
}
});
});
Here is the working example: https://jsfiddle.net/nnxfk7s6/9/
Related
Struggling to add css opacity transition with js. When clicking on a tab, the js loop hides all content and only shows the content with the corresponding id. Im trying to get the loop to add the transition as well. The tabcontent class is the CSS in question.
<li>
<a style="left: 9.6%" href="javascript:switchTab('tb_1', 'content_1', 'icon_1');" id="tb_1" class="tabmenu active">FIT GUIDE<span class='mobileshow' id='icon_1'>-<span></a>
<section id="content_1" class="tabcontent">
<p class='lorem'>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui</p>
</section>
</li>
<li>
<a style="left: 39%;" href="javascript:switchTab('tb_2', 'content_2', 'icon_2');" id="tb_2" class="tabmenu">CARE <span class='mobileshow' id='icon_2'>+</span></a>
<section id="content_2" class="tabcontent" style="display:none; opacity: 0;">
<p class='lorem'>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui</p>
</section>
</li>
<li>
<a style="left: 60.6%" href="javascript:switchTab('tb_3', 'content_3', 'icon_3');" id="tb_3" class="tabmenu">MATERIALS <span class='mobileshow' id='icon_3'>+</span></a>
<section id="content_3" class="tabcontent" style="display:none; opacity: 0;">
<div class='materials-content'>
<ul class='materials-stats'>
<li class='material-item'>50%<span class='subtext'>cashmere</span></li>
<li class='material-item'>46%<span class='subtext right'>wool</span></li>
<li class='material-item'>4%<span class='subtext'>modal</span></li>
</ul>
<div class='border-bot'></div>
<p class='lorem'>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui</p>
</div>
</section>
</li>
</ul>
function switchTab(tab_id, tab_content, icon_id) {
// select tab content
let content = document.getElementsByClassName("tabcontent");
for (let i = 0; i < content.length; i++) {
// hide tab content
content[i].style.opacity = '0';
content[i].style.display = 'none';
}
//document.getElementById(tab_content).style.display = 'block';
//document.getElementById(tab_content).classList.add("showme");
document.getElementById(tab_content).style.cssText = "opacity: 1; display: block;"
}
.tabcontent {
max-width: 750px;
margin-top: 45px;
position: relative;
transition: opacity 1s ease-in-out;
padding: 20px 0;
top: 0;
}
It is not possible to animate opacity and display at the same time. You would probably want to set display: block first, and then change the opacity. Or the other way around if you want to hide the block.
I think you might want to have a look at #keyframes. Here's a link to documentation about it. https://developer.mozilla.org/en-US/docs/Web/CSS/#keyframes
It will look something like this:
#keyframes yourAnimation {
from {
transform: opacity(0);
}
to {
transform: opacity(1);
}
}
Have a class named animation and apply the keyframes to that. Then add the class to the HTMLElement you want to be animated using JS. This will cause the CSS animation to occur.
CSS for this part will be looking something like this:
.animation {
animation: yourAnimation;
}
It seems like the JS part you can manage, but if you need more help let me know!
Hello guys I'm using scroll spy of bootstrap. I have applied some height and a overflow to make the scroll appear but after applying it the scroll spy does not works. I mean that when I scroll up or down the link in the navbar does not changes If I remove the style that I gave it starts working. Here is a Jsbin code. Hope this helps and please tell me what is it that I'm doing wrong
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li>Section 1</li>
<li>Section 2</li>
<li>Section 3</li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Section 4 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Section 4-1</li>
<li>Section 4-2</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
<div style="height:200px; overflow:scroll;">
<div id="section1" class="container-fluid">
<h1>Section 1</h1>
..
</div>
<div id="section2" class="container-fluid">
<h1>Section 2</h1>
..
</div>
<div id="section3" class="container-fluid">
<h1>Section 3</h1>
..
</div>
<div id="section41" class="container-fluid">
<h1>Section 4 Submenu 1</h1>
..
</div>
<div id="section42" class="container-fluid">
<h1>Section 4 Submenu 2</h1>
..
</div>
</div>
</body>
JsBin
The problem is that you're overriding the BODY scroll by using style="height:500px;overflow:scroll;" in the DIV that wraps all of the sections.
As a result, the BODY is never scrolled. You can instead put the ScrollSpy on this wrapper DIV..
<div style="height:500px;overflow:scroll;" data-spy="scroll" data-target="#myNavbar" data-offset="50">
...
</div>
Also, the data-target should be #myNavbar...
Working Demo on Codeply
Try the following code. Taken this example form the page - http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-scrollspy.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bootstrap 3 ScrollSpy Events</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".scroll-area").scrollspy({target: "#myNavbar"});
});
</script>
<style type="text/css">
.scroll-area {
height: 250px;
position: relative;
overflow: auto;
}
</style>
</head>
<body>
<div class="container">
<h2>Bootstrap Scrollspy</h2>
<p>Scroll the area below the navbar and see how menu items are highlighted automatically based on the scroll position. The items in dropdown submenu will be highlighted as well.</p>
<nav id="myNavbar" class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbarCollapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Scrollspy</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active">Section 1</li>
<li>Section 2</li>
<li>Section 3</li>
<li>Section 4</li>
</ul>
</div>
</nav>
<div class="scroll-area" data-spy="scroll" data-offset="0">
<div class="section">
<h3 id="section-1">Section 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio in fringilla. Vivamus nisl leo, blandit at bibendum eu, tristique eget risus. Integer aliquet quam ut elit suscipit, id interdum neque porttitor. Integer faucibus ligula.</p>
<p>Vestibulum quis quam ut magna consequat faucibus. Pellentesque eget nisi a mi suscipit tincidunt. Ut tempus dictum risus. Pellentesque viverra sagittis quam at mattis. Suspendisse potenti. Aliquam sit amet gravida nibh, facilisis gravida odio. Phasellus auctor velit at lacus blandit, commodo iaculis justo viverra. Etiam vitae est arcu. Mauris vel congue dolor. Aliquam eget mi mi. Fusce quam tortor, commodo ac dui quis, bibendum viverra erat. Maecenas mattis lectus enim, quis tincidunt dui molestie euismod. Curabitur et diam tristique, accumsan nunc eu, hendrerit tellus.</p>
</div>
<hr>
<div class="section">
<h3 id="section-2">Section 2</h3>
<p>Nullam hendrerit justo non leo aliquet imperdiet. Etiam in sagittis lectus. Suspendisse ultrices placerat accumsan. Mauris quis dapibus orci. In dapibus velit blandit pharetra tincidunt. Quisque non sapien nec lacus condimentum facilisis ut iaculis enim. Sed viverra interdum bibendum. Donec ac sollicitudin dolor. Sed fringilla vitae lacus at rutrum. Phasellus congue vestibulum ligula sed consequat.</p>
<p>Vestibulum consectetur scelerisque lacus, ac fermentum lorem convallis sed. Nam odio tortor, dictum quis malesuada at, pellentesque vitae orci. Vivamus elementum, felis eu auctor lobortis, diam velit egestas lacus, quis fermentum metus ante quis urna. Sed at facilisis libero. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vestibulum bibendum blandit dolor. Nunc orci dolor, molestie nec nibh in, hendrerit tincidunt ante. Vivamus sem augue, hendrerit non sapien in, mollis ornare augue.</p>
</div>
<hr>
<div class="section">
<h3 id="section-3">Section 3</h3>
<p>Integer pulvinar leo id risus pellentesque vestibulum. Sed diam libero, sodales eget sapien vel, porttitor bibendum enim. Donec sed nibh vitae lorem porttitor blandit in nec ante. Pellentesque vitae metus ipsum. Phasellus sed nunc ac sem malesuada condimentum. Etiam in aliquam lectus. Nam vel sapien diam. Donec pharetra id arcu eget blandit. Proin imperdiet mattis augue in porttitor. Quisque tempus enim id lobortis feugiat. Suspendisse tincidunt risus quis dolor fringilla blandit. Ut sed sapien at purus lacinia porttitor. Nullam iaculis, felis a pretium ornare, dolor nisl semper tortor, vel sagittis lacus est consequat eros. Sed id pretium nisl. Curabitur dolor nisl, laoreet vitae aliquam id, tincidunt sit amet mauris.</p>
<p>Phasellus vitae suscipit justo. Mauris pharetra feugiat ante id lacinia. Etiam faucibus mauris id tempor egestas. Duis luctus turpis at accumsan tincidunt. Phasellus risus risus, volutpat vel tellus ac, tincidunt fringilla massa. Etiam hendrerit dolor eget ante rutrum adipiscing. Cras interdum ipsum mattis, tempus mauris vel, semper ipsum. Duis sed dolor ut enim lobortis pellentesque ultricies ac ligula. Pellentesque convallis elit nisi, id vulputate ipsum ullamcorper ut. Cras ac pulvinar purus, ac viverra est. Suspendisse potenti. Integer pellentesque neque et elementum tempus. Curabitur bibendum in ligula ut rhoncus.</p>
<p>Quisque pharetra velit id velit iaculis pretium. Nullam a justo sed ligula porta semper eu quis enim. Pellentesque pellentesque, metus at facilisis hendrerit, lectus velit facilisis leo, quis volutpat turpis arcu quis enim. Nulla viverra lorem elementum interdum ultricies. Suspendisse accumsan quam nec ante mollis tempus. Morbi vel accumsan diam, eget convallis tellus. Suspendisse potenti.</p>
</div>
<hr>
<div class="section">
<h3 id="section-4">Section 4</h3>
<p>Nam eget purus nec est consectetur vehicula. Nullam ultrices nisl risus, in viverra libero egestas sit amet. Etiam porttitor dolor non eros pulvinar malesuada. Vestibulum sit amet est mollis nulla tempus aliquet. Praesent luctus hendrerit arcu non laoreet. Morbi consequat placerat magna, ac ornare odio sagittis sed. Donec vitae ullamcorper purus. Vivamus non metus ac justo porta volutpat.</p>
<p>Vivamus mattis accumsan erat, vel convallis risus pretium nec. Integer nunc nulla, viverra ut sem non, scelerisque vehicula arcu. Fusce bibendum convallis augue sit amet lobortis. Cras porta urna turpis, sodales lobortis purus adipiscing id. Maecenas ullamcorper, turpis suscipit pellentesque fringilla, massa lacus pulvinar mi, nec dignissim velit arcu eget purus. Nam at dapibus tellus, eget euismod nisl. Ut eget venenatis sapien. Vivamus vulputate varius mauris, vel varius nisl facilisis ac. Nulla aliquet justo a nibh ornare, eu congue neque rutrum.</p>
<p>Suspendisse a orci facilisis, dignissim tortor vitae, ultrices mi. Vestibulum a iaculis lacus. Phasellus vitae convallis ligula, nec volutpat tellus. Vivamus scelerisque mollis nisl, nec vehicula elit egestas a. Sed luctus metus id mi gravida, faucibus convallis neque pretium. Maecenas quis sapien ut leo fringilla tempor vitae sit amet leo. Donec imperdiet tempus placerat. Pellentesque pulvinar ultrices nunc sed ultrices. Morbi vel mi pretium, fermentum lacus et, viverra tellus. Phasellus sodales libero nec dui convallis, sit amet fermentum sapien auctor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed eu elementum nibh, quis varius libero.</p>
</div>
</div>
</div>
</body>
</html>
I think its a some kind of bug trying to remove height and add min-height instead or take another div element and give it max-height:200px or what ever you want
hope this help http://jsbin.com/kubisivuwo/edit?html,output
I am trying to implement JQuery Tabs on my page. But, the problem is, when I click on the tabs,the UI of the tabs do not change.The selected tab always remains selected and the unselected tab is always unselected. I am not able to figure out which tab is actually clicked.Since, I have used "bind" to bind the click event on the tabs,the implementation is working as expected.Since I am totally new to JQuery UI stuff, I am not able to figure out where I am going wrong.
Here is the jsp code for the tabs(written as mentioned on the net).
<div id="tabs" style="margin-top: 100px; width: 100%;margin-left:20%;">
<ul>
<li id="allocated">Allocated</li>
<li id="pending">Pending</li>
</ul>
</div>
In javascript file :
$('#tabs',container).tabs();
I have written different bind event for both the tabs as follows :
$("#allocated", container).unbind("click").click(function() {
});
$("#pending", container).unbind("click").click(function() {
});
Do I need to change the state of the tabs manually or it should change by itself.
There is no content to display for your tabs, that's why this is not working properly :
<div id="tabs" style="margin-top: 100px; width: 100%;margin-left:20%;">
<ul>
<li id="allocated">Allocated
</li>
<li id="pending">Pending
</li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
</div>
<div id="tabs-2">
<p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
</div>
</div>
And no need to specify container in your selector :
$('#tabs').tabs();
ยป Live Demo
Why you are unbinding the click event? This will stop the click event added by $('#tabs').tabs();.
If you want to do custom actions on the click, you just need to define the click:
$("#allocated", container).click(function() {
alert(1);
});
$("#pending", container).click(function() {
alert(2)
});
Basically, I am creating a web page for my school project... sorry for silly explanation but..
I want to have fixed navigation bar at the top of the site and when you click any link it will move you up or down to the requested content (not to different subpage, just scrolling up/down)..
see example here: http://www.danbrown.com/
however I need just the simplest way how to do it...and I didnt find any tutorial whatsoever.
Thanks
You need to use fixed position for you menu.
http://www.w3schools.com/css/css_positioning.asp
And to scroll to a particular spot in the page use link anchor.
http://www.echoecho.com/htmllinks08.htm
I could code the ole thing for you but you will learn nothing.
Link anchors are totally the easiest and most reliable way to do what you want.
<a name="a">text</a> or <a id="a">text</a> both set an anchor point somewhere, and text will scroll the viewport to that link.
You can also set the id attribute on other elements, say, headings, to make them into anchor targets, but the name property will only work on <a> tags.
As a starting point, you could use the following twitter bootstrap demo to do the fixed navigation bar.
The jquery code is just needed to make a scroll animation. It would also work with-out jQuery.
In the demo you should do a change effect to the navigation to show the current scroll position. That's not in the demo.
You can find the same code here at jsFiddle.
The initial scroll effect is not difficult to do. Please check my anwser in this SO question that will help you to do that effect.
(You could also do the fixed navigation with the code from the counter example and do your own menu styling.)
$(function () {
// source for transition animation from here: https://stackoverflow.com/questions/12643510/css3-transitions-with-anchors
$('a[href^=#]').on("click", function (e) {
var t = $(this.hash);
var t = t.length && t || $('[name=' + this.hash.slice(1) + ']');
if (t.length) {
var tOffset = t.offset().top;
$('html,body').animate({
scrollTop: tOffset - 20
}, 'slow');
e.preventDefault();
}
});
});
body {
min-height: 2000px;
padding-top: 50px;
}
/* anchor style to scroll to the right position */
a.anchor {
display: block;
position: relative;
top: -20px;
visibility: hidden;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active">Dummy 1
</li>
<li>Dummy 2
</li>
<li>Contact
</li>
<li class="dropdown"> Dropdown <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Action
</li>
<li>Another action
</li>
<li>Something else here
</li>
<li class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li>Separated link
</li>
<li>One more separated link
</li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="row"> <a class="anchor" id="dummy1"></a>
<h1>Dummy 1</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>
<div class="row"> <a class="anchor" id="dummy2"></a>
<h1>Dummy 2</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p>
</div>
</div>
I have been trying to achieve a sticky navbar in my project, but somehow its not working. I can't get what's wrong here (PS: it doesn't even show up in my fiddle but does show in local files I'm building. )
Have been using twitter bootstrap 3.x. What might possibly be going wrong? A little help would be much appreciated.
Here's the code I'm implementing:
HTML:
<div class="container-fluid">
<h3>this is a title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc molestie posuere justo id condimentum. Morbi eu massa non enim varius lobortis vitae id massa. Aliquam congue consectetur nisi, sit amet tempus nunc mattis at. Aliquam non eleifend quam, ac accumsan urna. Nullam porta metus lacus, aliquam pretium purus rhoncus et. Donec in congue mi. Integer eu massa tincidunt, convallis diam in, ultrices lacus. Nulla bibendum metus sed enim faucibus, vel egestas tortor sollicitudin. Proin bibendum condimentum enim, ac luctus augue gravida et. Proin pretium tortor sed turpis gravida molestie. In hac habitasse platea dictumst.</p>
</div>
<div id="navi" class="navbar navbar-inverse visible-md visible-lg" role="navigation">
<ul class="nav navbar-nav">
<li>Home
</li>
<li class="active">About me
</li>
<li>Resume
</li>
<li>Contact
</li>
</ul>
</div>
<p>lol Proin auctor turpis ac elit pharetra laoreet. Cras consequat mauris ut condimentum vulputate. Nunc faucibus viverra suscipit. Sed sit amet ante interdum libero malesuada adipiscing sed at erat. Praesent et tristique lorem. Vestibulum lobortis laoreet tortor eget pellentesque. Nam commodo nibh libero, in elementum mi lobortis ut. this is a title Pellentesque interdum, quam sit amet sagittis porttitor, nunc lorem aliquam lorem, varius laoreet lectus diam vel mi. Fusce ut justo a neque vestibulum aliquam ac at ante. Phasellus porttitor mollis semper. Cras ultricies augue augue, non consequat nisi laoreet et. Donec sapien erat, molestie in faucibus ut, dapibus id risus. Morbi nec lorem nulla. Suspendisse potenti. Ut congue purus vel tincidunt facilisis. Cras condimentum lorem a purus lacinia dignissim. Curabitur convallis, leo ut vestibulum feugiat, ante urna ullamcorper ipsum, et aliquam justo sem ac urna. Praesent rhoncus risus nec dignissim ornare. Sed nec tellus nibh. Nulla in mauris ac lorem mollis ultricies id et mauris.</p>
The CSS:
.fixed {
position: fixed;
top: 0;
height: 50px;
z-index: 180;
}
The jQuery
$(document).ready(function(){
$(window).bind('scroll', function() {
var navHeight = $( window ).height() - 50;
if ($(window).scrollTop() > navHeight) {
$('#navi').addClass('fixed');
}
else {
$('#navi').removeClass('fixed');
}
});
});
The Fiddle Link
Thanks in advance
Add the following code to your css
#navi{
display: block!important;
}
It is because somehow bootstrap is making your nav display:none
Working Code
.visible-md and .visible-lg are hiding your nav bar, because with .visible-md and .visible-lg, your element will only be visible in desktop and large devices (desktops) and in jsfiddle, the viewport is small.
You can do the following to show your nav, but it won't be required if your site opens in parent frame. See your old fiddle in full screen result here: http://jsfiddle.net/x84PJ/5/embedded/result/ Your nav is visible here.
#navi { display: block !important;} /* use this only for jsfiddle result output, else your nav will break on medium and small devices*/
and also fix your jQuery code
$('nav').removeClass('fixed');
should be
$('#navi').removeClass('fixed');
For more detail on navbar try this: http://getbootstrap.com/examples/navbar/