菜单项的 JavaScript 旋转在单击后卡住

发布于 2024-10-11 22:23:35 字数 422 浏览 7 评论 0原文

代码链接

大家好,我有一个圆形菜单(链接上方),我可以在其中想让所选项目旋转到“3点钟”,停在那里并打开内容。下一个选定的项目应该旋转到相同的位置并执行相同的操作等。使用 jquery.path 插件,我设法旋转 8 个项目一次,但是当我再次单击某个项目时,它根本不会继续旋转,而是重置动画。

我还认为,通过比较左侧值和顶部值,可以检查所选项目是否位于“3 点钟”位置。但是,如果有人有更好的解决方案,我很想听听。

我不希望有人给我一个完整完成的脚本,但我会感谢任何帮助,指出我的错误,为什么动画重置或为我指明正确的方向。即使 JavaScript 专家可以告诉我,我实现菜单的方法很垃圾,而且比我想象的要复杂得多。谢谢! :)

Link to code

Hello all, I have a circle menu (above link) where I want the selected item to rotate to "3 o'clock", stop there and open the content. Next selected item is supposed to rotate to the same position and do the same etc. Using the jquery.path plugin, I managed to rotate the 8 items once but when I click again on an item, it simply doesn't continue to rotate but resets the animation.

Also I thought that by comparing left and top values, one could check if the selected item is on the "3 o'clock" position or not. However if someone has a better solution, I would love to hear about it.

I'm not expecting anyone to hand me a completely finished script but I would appreciate any help, pointing out my mistake why the animation resets or pointing me in the right direction. Even if a JavaScript guru could tell me that my approach to implement the menu is garbage and that it's way more complicated than I think. Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-10-18 22:23:35

只是这里的猜测,但我没有看到你在任何地方存储当前角度。这意味着您告诉它们每次从 0 开始并迭代原始数组。您需要在函数外部存储某种偏移量。像这样的事情可能会起作用:

var fin = -1;
$('a.box').click(function() {

    var menu_items = Array("#box_8", "#box_7", "#box_6", "#box_5", "#box_4", "#box_3", "#box_2", "#box_1");
    fin++;

    var startAngle = 180 + fin * 45;
    var endAngle = 135 + fin * 45;
    var topLock = "154px";
    var leftLock = "260px";

    //$(this).addClass('selected');

        for (var i=0; i < menu_items.length; i++) {

            startAngle += 45;
            endAngle += 45;
            $(menu_items[i]).animate({
                path: new $.path.arc({
                    center: [154, 154],
                    radius: 106,
                    start: startAngle,
                    end: endAngle,
                    dir: -1
                }),
                opacity: '1'
                },400);
        };

});

我还认为你的结束角度和开始角度不正确。看起来表盘正在切换动画的数字,这意味着它可能正在向后移动物体,然后将它们动画到当前位置。这可能可以通过将 fin(上面的新变量)设置为 0 作为初始值来解决。

顺便说一句:您

  1. 可能希望将 menu_items 存储在单击处理程序之外。
  2. 您还可以将 menu_items.length 存储为循环外部的变量。

然后您可以定义角度差异。作为 360/menu_items.length 然后有:

  var angleDiff  = 360 / menuLength;
  var startAngle = 180 + fin * angleDiff;
  var endAngle = 135 + fin * angleDiff;

以及这个(在循环内):

        startAngle += angleDiff;
        endAngle += angleDiff;

Just a guess here, but I don't see you storing the current angle anywhere. This means that you're telling them to start at 0 each time and iterate through the original array. You need to have some sort of offset stored outside of the function. Something like this could work:

var fin = -1;
$('a.box').click(function() {

    var menu_items = Array("#box_8", "#box_7", "#box_6", "#box_5", "#box_4", "#box_3", "#box_2", "#box_1");
    fin++;

    var startAngle = 180 + fin * 45;
    var endAngle = 135 + fin * 45;
    var topLock = "154px";
    var leftLock = "260px";

    //$(this).addClass('selected');

        for (var i=0; i < menu_items.length; i++) {

            startAngle += 45;
            endAngle += 45;
            $(menu_items[i]).animate({
                path: new $.path.arc({
                    center: [154, 154],
                    radius: 106,
                    start: startAngle,
                    end: endAngle,
                    dir: -1
                }),
                opacity: '1'
                },400);
        };

});

I also think that your end angle and start angle are off. It looks like the dial is switching numbers for the animation, which means that it is probably moving things backwards and then animating them to their current positions. This would probably be solved by setting fin (the new variable above) to 0 as an initial value.

As an asside: you

  1. You may want to store menu_items outside of the click handler.
  2. You also store menu_items.length as a variable outside of the loop.

Then you could define the angle diff. as 360/menu_items.length and then have:

  var angleDiff  = 360 / menuLength;
  var startAngle = 180 + fin * angleDiff;
  var endAngle = 135 + fin * angleDiff;

as well as this (inside the loop):

        startAngle += angleDiff;
        endAngle += angleDiff;
风铃鹿 2024-10-18 22:23:35
var menu_items = Array("#box_8", "#box_7", "#box_6", "#box_5", "#box_4", "#box_3", "#box_2", "#box_1");
var menuLength = menu_items.length;
var angleDiff  = 360 / menuLength;
var fin = 0;

$('a.box').click(function() {
var startAngle = 180 + fin * angleDiff;
var endAngle = 225 + fin * angleDiff;
fin++;
    var topLock = "154px";
    var leftLock = "260px";

        for (var i=0; i < menu_items.length; i++) {
        startAngle += angleDiff;
        endAngle += angleDiff;
        $(menu_items[i]).animate({
            path: new $.path.arc({
                center: [154, 154],
                radius: 106,
                start: startAngle,
                end: endAngle,
                dir: 1
            }),
            opacity: '1'
            },400);
        };
});
var menu_items = Array("#box_8", "#box_7", "#box_6", "#box_5", "#box_4", "#box_3", "#box_2", "#box_1");
var menuLength = menu_items.length;
var angleDiff  = 360 / menuLength;
var fin = 0;

$('a.box').click(function() {
var startAngle = 180 + fin * angleDiff;
var endAngle = 225 + fin * angleDiff;
fin++;
    var topLock = "154px";
    var leftLock = "260px";

        for (var i=0; i < menu_items.length; i++) {
        startAngle += angleDiff;
        endAngle += angleDiff;
        $(menu_items[i]).animate({
            path: new $.path.arc({
                center: [154, 154],
                radius: 106,
                start: startAngle,
                end: endAngle,
                dir: 1
            }),
            opacity: '1'
            },400);
        };
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文