Javascript动画div到其他div的位置?

时间:2014-10-29 14:33:48

标签: javascript jquery html css css3

好吧,所以我还没有找到适合我所寻找的答案。我需要一个简单的脚本,可以将彩色div设置为菜单选择或导航按钮的位置。我希望它为光标悬停在div上的div的位置设置动画,但我不希望它跟随光标,我只是想让它滑到菜单上的一部分文本中作为突出显示或选择器。这是一个JSFiddle。哦,还有一件事。我希望突出显示在菜单被拖出后滑回原始位置(所选菜单项)。示例:(如果' 关于 '已选中,' 主页 & #39;悬停,滑到' 主页 ',但请回到' 关于 '如果在没有选择的情况下徘徊)。非常感谢任何帮助!

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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

也许是这样的?

<强>的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

答案 1 :(得分:0)

将所有菜单项设为同一个类,然后将事件目标的offsetLeft(您悬停的菜单项)传递给动画调用。

更新小提琴: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
      });
    });
}