gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:03 25 4
gpt4 key购买 nike

好吧,我没能找到适合我所寻找的答案。我需要一个简单的脚本,可以将彩色 div 动画化到菜单选择或导航按钮的位置。我想让它动画到光标悬停的 div 的位置,但我不想让它跟随光标,我只想让它滑到菜单上的一部分文本上作为突出显示, 或选择器。这是一个 JSFiddle。哦,还有一件事。我希望突出显示在菜单悬停后滑回其原始位置(选定的菜单项)。示例:(如果选择了“about”,并且悬停在“home”上,则滑动到“” em>home',但如果在没有选择的情况下悬停,则滑回'about')。非常感谢任何帮助!

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>

最佳答案

也许是这样的?

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

关于Javascript 动画 div 到其他 div 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26633065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com