Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Last active August 29, 2015 14:18
Show Gist options
  • Save pstuifzand/6abb0ae862c3b8bd54a8 to your computer and use it in GitHub Desktop.
Save pstuifzand/6abb0ae862c3b8bd54a8 to your computer and use it in GitHub Desktop.
Menu without delay
<!doctype html>
<html>
<head>
<style>
* {
padding:0;
margin:0;
box-sizing:border-box;
}
body {
padding:10px;
font-size:12px;
}
.menu-button {
width:200px;
background:#def;
padding:3px;
color:#24d;
font-family:sans-serif;
font-weight:bold;
font-size:25px;
}
.menu-button:hover {
background: #fed;
cursor: pointer;
}
.menu-button.active {
background: #fed;
}
.menu {
display:none;
width:200px;
float:left;
}
.menu ul {
padding:0;margin:0;
list-style-type:none;
padding-left:0;
margin-left:0;
}
.menu li {
padding-left:0;
margin-left:0;
background:#def;
margin-top:1px;
padding:5px;
height:30px;
color: #888;
font-size:15px;
}
.menu li:hover {
cursor: pointer;
}
.menu li.active {
background:#555;
}
.menu-panel {
display:none;
background: #acd;
float:left;
width:600px;
height:auto;
columns: 3;
padding:8px;
}
h2 {
font-size:15px;
}
</style>
</head>
<body>
<div class="menu-button">MENU</div>
<div class="menu">
<ul>
<li data-color="#d34">Item 1</li>
<li data-color="#def">Item 2</li>
<li data-color="#acd">Item 3</li>
<li data-color="#eee">Item 4</li>
<li data-color="#5d5">Item 5</li>
<li data-color="#4de">Item 6</li>
</ul>
</div>
<div class="menu-panel">
<h2>Sectie 1</h2>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<h2>Sectie 2</h2>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<h2>Sectie 3</h2>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<h2>Sectie 4</h2>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<h2>Sectie 5</h2>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<h2>Sectie 6</h2>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
<a href="">Link</a>
</div>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
function elementTop(x) {
var offset = $(x).offset();
var width = $(x).width();
var height = $(x).height();
return [offset.left, offset.top];
}
function elementBottom(x) {
var offset = $(x).offset();
var width = $(x).width();
var height = $(x).height();
return [offset.left, offset.top+height];
}
function resetActiveMenuItem(clientX, clientY) {
x = document.elementFromPoint(clientX, clientY);
if ($(x).is('li')) {
ALeftBot = [clientX, clientY];//elementBottom(x);
ALeftTop = [clientX, clientY];//elementTop(x);
var y = $('.menu-panel');
$(y).fadeIn();
$(y).css('background', $(x).data('color'));
$('h2',$(y)).html($(x).html());
$('.selected').removeClass('selected');
$('.active').removeClass('active');
$(x).addClass('active selected');
}
else if ($(x).is('ul')) {
}
else if ($(x).is('.menu')) {
}
else if ($(x).is('.menu-panel')) {
}
else if ($(x).is('.menu-panel a')) {
}
else if ($(x).is('.menu-panel h2')) {
}
else if ($(x).is('.menu-button')) {
}
else {
$('.menu-panel').fadeOut('fast');
$('.menu').fadeOut('fast');
$('.active').removeClass('.active');
$('.selected').removeClass('.selected');
}
}
var ALeftBot, ALeftTop;
function mouseMove(e) {
var x = $('li.selected');
if (x.length) {
var y = $('.menu-panel');
var BLeftBot = elementBottom(y);
var BLeftTop = elementTop(y);
// use BLeftBot as origin
var p = perp(sub(ALeftBot, BLeftBot));
var sign = inner(p, sub([e.clientX, e.clientY],BLeftBot));
if (sign < 0) resetActiveMenuItem(e.clientX, e.clientY);
var p = perp(sub(ALeftTop, BLeftTop));
var sign = inner(p, sub([e.clientX, e.clientY],BLeftTop));
if (sign > 0) resetActiveMenuItem(e.clientX, e.clientY);
} else {
resetActiveMenuItem(e.clientX, e.clientY);
}
}
$('.menu-button').on('mouseenter', function() {
$('.menu').fadeIn();
});
$('.menu-panel').on('mouseenter', function() {
$('li.selected').removeClass('selected');
});
function inner(a, b) {
return a[0]*b[0] + a[1]*b[1];
}
function perp(a) {
return [ -a[1], a[0] ];
}
function sub(a, b) {
return [a[0] - b[0], a[1] - b[1]];
}
$(document).ready(function() {
$(document).on('mousemove', mouseMove);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment