Shane Kerr
Shane Kerr

Reputation: 1

Javascript animate div to position of other divs?

Alright, so I haven't been able to find an answer that suits what I am looking for. I am in need of a simple script that can animate a colored div to the position of a menu selection or navigation button. I want it to animate to the position of the div that the cursor is hovering over, but I don't want it to follow the cursor, I just want it to slide over to a portion of text on the menu to act as a highlight, or selector. Here is a JSFiddle. Oh, and one more thing. I would like the highlight to slide back to its original position(the selected menu item) after the menu has been hovered off of. EXAMPLE: (if 'about' selected, and 'home' hovered, slide to 'home', but slide back to 'about' if hovered off without selection). Any help is greatly appreciated!

http://jsfiddle.net/hbv63znv/

var main = function() {
    $('.menu-item1').hover(function() {
      $('.hover').animate({
          left: ''
      });
    });
}

$(document).ready(main);

/*
Had to remove a few of the buttons to fit it in the JSFiddle page but I want to make the highlight slide to the position of each selection. I also want it to conform to the size(width) of the button. It would also be very nice if it automatically did all the animations by itself, by this I mean that the highlight would automattically conform to the size of the button instead of having a fixed width and position to slide to(This would allow me to change the text of the menu and I wouldnt have to change any of the javascript.
*/
/*Initial body */
body {
  left: 0;
  margin: 0;
  overflow: hidden;
  position: relative;
}

/* Initial menu */
.menu {
  position: fixed;
  left: 30%;
  right: 30%;
  width: 40%;
  text-align: center;
  background-color: black;
  padding-left: 10px;
  padding-right: 10px;
  -webkit-transform: skew(20deg);
  -moz-transform: skew(20deg);
  -o-transform: skew(20deg);
}

/* Basic styling */

.content {
  background-color: #E0E0E0;
  height: 100%;
  -webkit-background-size: cover;
     -moz-background-size: cover;
       -o-background-size: cover;
          background-size: cover;
}

.menu ul {
  list-style: none;
  margin: 0;
  padding-left: 5px;
  padding-right: 5px;
  -webkit-transform: skew(-20deg);
     -moz-transform: skew(-20deg);
       -o-transform: skew(-20deg);
}

.hover {
  height: 100%;
  width: 100px;
  background-color: #303030;
  position: absolute;
  margin-left: 30px;
}

.menu li {
  font-family: 'Open Sans', sans-serif;
  line-height: 45px;
  padding-bottom: 3px;
  padding-left: 30px;
  padding-right: 30px;
  padding-top: 3px;
  display: inline
}

.menu a {
  color: #fff;
  font-size: 15px;
  text-decoration: none;
  text-transform: uppercase;
}

.icon-close {
  cursor: pointer;
  padding-left: 10px;
  padding-top: 10px;
}

.icon-menu {
  color: #fff;
  cursor: pointer;
  font-family: 'Open Sans', sans-serif;
  font-size: 16px;
  padding-bottom: 25px;
  padding-left: 25px;
  padding-top: 25px;
  text-decoration: none;
  text-transform: uppercase;
}

.icon-menu i {
  margin-right: 5px;
}
h1 {
  top: 80px;
  position: absolute

}
  <body>

    <div class="menu">
      <!-- Menu -->
      <div class='hover'>
          
      </div>
      <ul>
        <li class='menu-item'><a href="#">Home</a></li>
        <li class='menu-item'><a href="#">Gallery</a></li>
        <li class='menu-item'><a href="#">About</a></li>
        <li class='menu-item'><a href="#">Contact</a></li>
          </div>
    <h1>Fullscreen to see the menu in the right form</h1>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="app.js"></script>
  </body>
</html>

Upvotes: 0

Views: 145

Answers (2)

Matt
Matt

Reputation: 1407

Give all menu items the same class, then pass the offsetLeft of the event target (the menu item you are hovering over) into the animate call.

Updated Fiddle: http://jsfiddle.net/hbv63znv/7/

<li class='menu-item'><a href="#">Home</a></li>
<li class='menu-item'><a href="#">Gallery</a></li>

var main = function() {
    $('.menu-item').hover(function() {
      var that = this;
      $('.hover').animate({
          left: that.offsetLeft
      });
    });
}

Upvotes: 0

mituw16
mituw16

Reputation: 5250

Maybe something like this?

Javascript

var main = function() {
  $('.menu-item').hover(function() {
    var $el = $(this);
  $('.hover').animate({
      left: $el.offset().left - $('.menu').offset().left
  });
});

$('.hover').css({
    left: $('.menu-item:first').offset().left - $('.menu').offset().left
});
}

JSFIDDLE

Upvotes: 2

Related Questions