<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="pages">
<!-- name your ID same as the anchor hrefs -->
<div style="background:rgba(100, 100, 50, 0.9);" id="images">IMAGES PAGE</div>
<div style="background:rgba(50, 100, 50, 0.9);" id="movies">MOVIES!! YEY</div>
<div style="background:rgba(100, 50, 50, 0.9);" id="360">360 PAGE</div>
<div style="background:rgba(50, 50, 100, 0.9);" id="name">ABOUT PAGE</div>
<div style="background:rgba(100, 50, 100, 0.9);" id="contact">Contact</div>
</div>
<div id="aside">
<div id="menu">
<img id="logo" src="http://placehold.it/150x50/fff&text=Logo">
<nav>
<ul>
<li>
<a href="#images">3D Images</a>
</li>
<li>
<a href="#movies">3D Movies</a>
</li>
<li>
<a href="#360">3D 360°</a>
</li>
<li>
<a href="#name">About</a>
</li>
<li>
<a href="#contact">Contact</a>
</li>
</ul>
</nav>
</div>
<!-- more stuff here like thimbnails etc... -->
</div>
</body>
</html>
var $menu = $("#menu");
var $nav = $("nav");
var $navUl = $nav.find("ul");
var $links = $navUl.find("a");
var nLinks = $links.length;
var linkH = $links.outerHeight();
var interaction = false; // Was any menu interaction? (so we can hide the menu)
var menuHovered = false;
var hash = window.location.hash; // If a user comes to the page usign a hash like http://mypage.com#video
var mouseY = 0; // Needed to know the mouse position when menu is opening
var $pages = $("#pages > div"); // Get all pages;
$(document).on("mousemove", function( e ){
mouseY = e.clientY; // Update the Y value
});
function openMenu() {
$navUl.stop().animate({top: 0});
$nav.stop().animate({height: linkH*nLinks}, {
duration: 600,
step: function( menuHeight ){
// keeps removing and adding class during the animation time.
// (it's an overkill but no other solution to that issue so far)
$links.removeClass("hover").filter(function(i, e){
var t = e.getBoundingClientRect().top;
return mouseY > t && mouseY < t+linkH;
}).addClass("hover"); // only to the link returned by `.filter()` condition
}
});
}
function closeMenu() {
if(!interaction) return; // Don't close menu if no item was clicked
$navUl.stop().animate({top: - $navUl.find(".selected").position().top });
$nav.stop().animate({height: linkH});
}
function setSelected() {
$links.removeClass("selected"); // Remove from all
$("a[href='"+hash+"']").addClass("selected"); // Add to clicked one (by var hash)
closeMenu(); // force close menu!!
}
function openPage(){
$( hash ).stop().fadeIn().siblings().fadeOut();
setSelected(); // Set active state depenging on hash value
}
openPage(); // Open a page on website load (If we have hash value ofc.)
$links.on("click", function( e ){
e.preventDefault(); // Prevent browser action
interaction = true;
hash = $(this).attr("href"); // !!!!! here is where hash changes
history.replaceState(null, '', hash); // Set HASH to address bar
openPage(); // open page (ID) depending on hash value (it does also the selected stuff)
}).hover(function(){
$(this).toggleClass("hover");
});
$menu.hover(openMenu, closeMenu);
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |