使用 jquery 的圆形旋转/放大菜单
我正在尝试制作一个包含 5 个项目/图标的菜单,其中所选项目/图标位于中心。单击该居中图标的左侧或右侧,可向左或向右旋转菜单,环绕边缘并将最靠近边缘的项目移回到相对的项目中。单击居中的项目会将您带到其链接的 URL。
菜单也应该以类似于 OS X 扩展坞的方式放大,只不过放大级别是根据位置而不是鼠标悬停设置的。
我做了一个图表,比我的漫谈更容易理解。
(来源:yfrog.com)
我'我们设法拼凑出一个简单的 jQuery 版本,其中项目根据需要交换位置,但无法弄清楚如何为该运动设置动画,尤其是围绕边缘部分的环绕,并根据位置更改大小。
我猜我的代码可能也不是最好的:)
HTML 如下:
<div id="nav">
<div id="leftnav"></div>
<div id="rightnav"></div>
<div id="navblock1" class="navblock">
one
</div>
<div id="navblock2" class="navblock">
two
</div>
<div id="navblock3" class="navblock">
three
</div>
<div id="navblock4" class="navblock">
four
</div>
<div id="navblock5" class="navblock">
five
</div>
JS:
function rotateNav(direction) {
var change = (direction=='left')?(-1):(+1);
$('div.navblock').each(function() {
oldPos = parseInt($(this).attr('id').substr(9));
newPos = oldPos+change;
if (newPos == 0)
newPos = 5;
else if (newPos == 6)
newPos = 1;
$(this).attr('id','navblock'+newPos);
});
}
$(document).ready(function(){
$("#leftnav").click(function() {
rotateNav('right');
});
$("#rightnav").click(function() {
rotateNav('left');
});
});
所有 .navblock 元素都是绝对定位的。 #leftnav 和 #rightnav 元素也有较高的 z-index,因此浮动在项目/图标上方。
我看过各种 jQuery 插件,但似乎没有一个能满足我的需要。
I'm trying to make a menu that contains 5 items/icons with the selected one being in the center. Clicking to the left or right of this centered icon, rotates the menu left or right, wrapping round the edges and moving whichever item was closest to the edge back in through the opposite one. Clicking on the centered item takes you to its linked URL.
The menu should also magnify in a way similar to the OS X dock except the magnification levels are set based on position not mouseover.
I've made a diagram which is easier to understand than my ramblings.
(source: yfrog.com)
I've managed to cobble together a simple jQuery version, where the items swap positions as needed, but can't figure out how to animate this movement, especially the wrap around the edges part, and change size based on position.
I'm guessing my code is probably not the best either :)
The HTML is as follows:
<div id="nav">
<div id="leftnav"></div>
<div id="rightnav"></div>
<div id="navblock1" class="navblock">
one
</div>
<div id="navblock2" class="navblock">
two
</div>
<div id="navblock3" class="navblock">
three
</div>
<div id="navblock4" class="navblock">
four
</div>
<div id="navblock5" class="navblock">
five
</div>
And the JS:
function rotateNav(direction) {
var change = (direction=='left')?(-1):(+1);
$('div.navblock').each(function() {
oldPos = parseInt($(this).attr('id').substr(9));
newPos = oldPos+change;
if (newPos == 0)
newPos = 5;
else if (newPos == 6)
newPos = 1;
$(this).attr('id','navblock'+newPos);
});
}
$(document).ready(function(){
$("#leftnav").click(function() {
rotateNav('right');
});
$("#rightnav").click(function() {
rotateNav('left');
});
});
All the .navblock elements are absolutely positionned. The #leftnav and #rightnav elements also and they have a higher z-index so float above the items/icons.
I've looked at various jQuery plugins but none seem close to what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以更改 CSS 类并使用 jQuery UI 的 switchClass() 方法来设置旋转动画,而不是更改 id 属性(您实际上不应该这样做)。
您还必须执行一些
clone()
操作,使其看起来像边缘navblock
已旋转到小部件的另一侧,并且一些>queue()
/dequeue()
ing 处理多次点击。工作演示:
http://jsbin.com/ovemu (可通过 http://jsbin.com/ovemu/edit)
完整来源:
JavaScript
CSS
HTML
Instead of changing
id
attributes (which you really shouldn't do in the first place) you can change CSS classes and use jQuery UI'sswitchClass()
method to animate the rotation.You would also have to do a bit of
clone()
ing to make it look like the edgenavblock
s have rotated around to the other side of the widget and somequeue()
/dequeue()
ing to handle multiple clicks.Working Demo:
http://jsbin.com/ovemu (editable via http://jsbin.com/ovemu/edit)
Full Source:
JavaScript
CSS
HTML
我建议您使用现有的解决方案,而不是自己执行此操作并浪费时间使其正常工作。这里有一些提示(我想可以通过使用谷歌找到更多提示
jQuery:类似Mac的Dock
类似 Mac 的图标底座 (v2)
MAC CSS Dock 菜单
jQuery 模仿 OS X 停靠栏
使用 jQuery 的类似 OSX 的简单底座
iconDock jQuery 插件
Instead of doing this yourself and wasting time on getting this to work properly I suggest you use existing solutions ones. Here a few pointers (I guess many more can be found by using google
jQuery: Mac-like Dock
Mac-like icon dock (v2)
MAC CSS Dock Menu
jQuery mimicking the OS X dock
Simple OSX-like dock with jQuery
iconDock jQuery Plugin
你似乎走在正确的轨道上。一个问题是这一行
应该在子字符串中使用 8:
You seem to be on the right track. One issue is that this line
Should use 8 in the substr: