I'm trying to replicate this: http://twitter.github.com/bootstrap/base-css.html#typography (the one on top that has the tabs: | Typography | Code Tables | Forms | Buttons | Icons by Glyphicons |). I'd like to replicate that same fast acting effect.
I'd like to make this div do the same:
#panel {
margin: auto;
border-bottom: 1px solid rgba(0,0,0,.6);
border-top: 0;
box-shadow: 0 1px rgba(0,0,0,.1),
1px 0 rgba(0,0,0,.1),
-1px 0 rgba(0,0,0,.1),
0 -1px rgb(255,255,255) inset,
0 1px 2px rgba(0,0,0,.2) inset,
1px 0 rgb(255,255,255) inset,
-1px 0 rgb(255,255,255) inset;
background: #E8E8E8;
width: 75%;
-webkit-border-bottom-left-radius: 5px;
-moz-border-radius-bottomleft: 5px;
-moz-border-radius-bottomright: 5px;
-webkit-border-bottom-right-radius: 5px;
color: rgba(0,0,0,.7);
text-shadow: 0 1px white;
padding: 2px 0px 2px 8px;
margin-top: -149px;
text-align: left;
font-size: 11px;
}
on my website: http://www.bobbaxtrade.com
Thanks :)
I've created the jsFiddle here: http://jsfiddle.net/cBk7q/
So, you need to add a class to your css rules, to make your div static on top of the window:
.fixed {
position: fixed;
top: 0;
left: 0;
}
Then, add a jQuery function to bind the scroll event and add the fixed class when you reach the desired position:
<script type="text/javascript">
var $mydiv = $("#mydiv"),
origTop = $("#mydiv").position().top;
$(window).scroll(function(e) {
if( document.body.scrollTop > origTop) {
console.log($mydiv.hasClass('fixed'));
$mydiv.addClass("fixed");
}
else {
console.log('c ' + (document.body.scrollTop > origTop));
$mydiv.removeClass("fixed");
}
});
</script>
The Bootstrap website explains you how to use this feature .
If you are using bootstrap, you can use the CSS class navbar-fixed-top
to get this behavior:
<div id="panel" class="navbar navbar-fixed-top">
...
</div>
If you want the effect, that the navigation bar only becomes fixed after scrolling over it, you need to add the class navbar-fixed-top
dynamically with some JavaScript (taken from LessCSS ).
Suppose you have some HTML
<!-- some content -->
<div id ="panel">
…
</div>
<!-- enough other content to make the document scroll -->
and some CSS
.navbar-fixed-top {
position: fixed;
top: 0;
left: 0;
}
Then the following JS will give you the requested behavior.
var docked = false;
var menu = document.getElementById('panel');
var init = menu.offsetTop;
function scrollTop() {
return document.body.scrollTop || document.documentElement.scrollTop;
}
window.onscroll = function () {
if (!docked && (menu.offsetTop - scrollTop() < 0)) {
menu.style.top = 0;
menu.className = 'navbar-fixed-top';
docked = true;
} else if (docked && scrollTop() <= init) {
menu.style.top = init + 'px';
menu.className = menu.className.replace('navbar-fixed-top', '');
docked = false;
}
};
This example works in Firefox, Chrome, Opera and Safari. Internet Explorer needs a workaround.
If you're using jQuery, try https://github.com/bigspotteddog/ScrollToFixed . Basic usage would be:
$(document).ready(function() {
$('#panel').scrollToFixed();
});
... however, see the link for options (eg allowing for footer panels when the user scrolls down, etc).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.