Skip to content

Instantly share code, notes, and snippets.

@popovevgeny
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save popovevgeny/9790886 to your computer and use it in GitHub Desktop.

Select an option

Save popovevgeny/9790886 to your computer and use it in GitHub Desktop.
Simple slider above menu items
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.menu {
width: 540px;
position: relative;
margin: 25% auto;
}
.menu-item {
float: left;
width: 100px;
height: 100px;
background-color: grey;
margin-right: 10px;
cursor: pointer;
}
.menu-item:last-child {
margin: 0;
}
#slider {
position: relative;
left: 0;
width: 100px;
margin-bottom: 10px;
border-top: solid 4px red;
-webkit-transition: left .3s ease,width .3s ease;
transition: left .3s ease,width .3s ease;
}
.item-container {
clear: both;
}
</style>
</head>
<body>
<div class="menu">
<div id="slider"></div>
<div class="item-container">
<div class="menu-item"></div>
<div class="menu-item"></div>
<div class="menu-item"></div>
<div class="menu-item"></div>
<div class="menu-item"></div>
</div>
</div>
<script>
(function(document){
var slider = document.getElementById('slider'),
menuItems = document.getElementsByClassName('menu-item'),
slide = function(e){
slider.style.left = e.target.offsetLeft + 'px';
};
[].forEach.call(menuItems, function(item){
item.addEventListener('mouseover', slide);
});
})(document);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment