对 jquery 非常陌生 - 为什么这不起作用
我只是想将 .icon 移到右侧。尝试按position().left 的数量对.icon 进行动画处理
我的代码无法正常工作,我不知道为什么。我敢打赌它非常简单!
<script>
$("li.menu-item").hover(function(){
var pos = $(this).position().left;
$(".icon").animate({left : pos+'px'}, 1500 );
});
</script>
</head>
<body>
<div id="menu-wrapper">
<ul id="main-nav">
<li class="menu-item"><a href="">Blog</a></li>
<li class="menu-item"><a href="">Sponsored</a></li>
<li class="menu-item"><a href="">Facebook</a></li>
<li class="menu-item"><a href="">Twitter</a></li>
<li class="menu-item"><a href="">About</a></li>
<li class="menu-item"><a href="">Contact</a></li>
</ul>
<div class="icon">move please</div>
</div><!-- //END menu-wrapper -->
</body>
css
#menu-wrapper { position: fixed; bottom: 50px; margin: 0 auto; text-align:center;}
.icon {
width: 9px;
height: 9px;
background: url(../images/plus-menu.png) no-repeat 0 0;
position: absolute;
left: -5px;
bottom: -4px;
}
#main-nav { line-height: 1.0; float: left; margin-bottom: 1em; }
#main-nav{list-style: none;}
#main-nav li a {
display: block;
position: relative;
text-decoration: none;
margin-right: 20px;
color: #b2b2b2;
line-height: 19px;
padding-bottom: 17px;
}
#main-nav li { float: left; position: relative; }
#main-nav li a:hover, #main-nav li a:active { color: #404040; }
任何帮助都会很棒
谢谢
I simply want to shift the .icon to the right. trying to animate .icon by the amount of position().left
My code isn't working and I don't know why. I bet its very simple!!
<script>
$("li.menu-item").hover(function(){
var pos = $(this).position().left;
$(".icon").animate({left : pos+'px'}, 1500 );
});
</script>
</head>
<body>
<div id="menu-wrapper">
<ul id="main-nav">
<li class="menu-item"><a href="">Blog</a></li>
<li class="menu-item"><a href="">Sponsored</a></li>
<li class="menu-item"><a href="">Facebook</a></li>
<li class="menu-item"><a href="">Twitter</a></li>
<li class="menu-item"><a href="">About</a></li>
<li class="menu-item"><a href="">Contact</a></li>
</ul>
<div class="icon">move please</div>
</div><!-- //END menu-wrapper -->
</body>
css
#menu-wrapper { position: fixed; bottom: 50px; margin: 0 auto; text-align:center;}
.icon {
width: 9px;
height: 9px;
background: url(../images/plus-menu.png) no-repeat 0 0;
position: absolute;
left: -5px;
bottom: -4px;
}
#main-nav { line-height: 1.0; float: left; margin-bottom: 1em; }
#main-nav{list-style: none;}
#main-nav li a {
display: block;
position: relative;
text-decoration: none;
margin-right: 20px;
color: #b2b2b2;
line-height: 19px;
padding-bottom: 17px;
}
#main-nav li { float: left; position: relative; }
#main-nav li a:hover, #main-nav li a:active { color: #404040; }
Any help will be great
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将代码包装在
$(document).ready()
中,查看此初学者教程 获取更多信息
Wrap your code in
$(document).ready()
Check this beginner tutorial out for more info
哼。通过最小的修改(
stop()
在每个新的悬停事件上 ping 动画,这样东西就不会被链接起来),它似乎可以在 这个小提琴。Hum. With minimal modifications (
stop()
ping the animation on each new hover event, so stuff doesn't get chained), it seems to work in this fiddle.