jQuery 动画问题,已添加解决方案

发布于 2024-12-02 07:51:38 字数 1663 浏览 1 评论 0原文

我正在开发导航菜单,标记结构如下,span 元素添加到 document.ready 上:-

<ul id="menu">
        <li class="menu act">Additional Fixture<span class="active"></span></li>
        <li class="menu">list with sub menu
            <ul class="sub-menu">
                <li class="menu">Option one</li>
            <li class="menu">Option two</li>
            <li class="menu">Option three</li>
            <li class="menu">Option four</li>
            <li class="menu">Option five</li>
        </ul>
    </li>
    <li class="menu"">Special Tricast</li>

跨度以设定的宽度和高度绝对定位,并包含一个箭头图像作为背景。

当用户单击菜单类以将活动范围移动到所选菜单 (li) 元素后面作为其选择的视觉指示器时,我使用 jQuery 为跨度/箭头图像设置动画。

当在根 li 处设置 active (act) 但如果位于列表中的其他任何位置时,此方法效果很好,例如: -

<ul id="menu">
        <li class="menu">Additional Fixture<span class="active"></span></li>
        **<li class="menu act">list with sub menu</li>**
</ul>

加载文档时箭头位置正确,但在用户单击时不会移动到正确的选择。我已经尝试了 divLocation 变量的位置和偏移量,并且我目前正在使用位置,因为即使在根目录下,偏移量也无法工作。

我基本上希望根据用户导航到的页面来设置箭头,因此无论列表的长度或位置如何,动画都需要从任何起始位置开始工作。

我已经在论坛上搜索并尝试了一些方法,但说实话我很难过,并且非常感谢任何帮助!

下面是用于对跨度/箭头图像进行动画处理的 jQuery 代码:-

var divLocation = $(this).position();

$("span.active").animate({
  'top': divLocation.top + topOffset,
      'left': divLocation.left + leftOffset

}, animationSpeed, "linear", function(){ 

$(selectedElement).addClass("text-light"); 
    $(selectedElement).fadeTo("100", opacityIn);


});

I am working on a nav menu the markup structure is below, the span element is added on document.ready: -

<ul id="menu">
        <li class="menu act">Additional Fixture<span class="active"></span></li>
        <li class="menu">list with sub menu
            <ul class="sub-menu">
                <li class="menu">Option one</li>
            <li class="menu">Option two</li>
            <li class="menu">Option three</li>
            <li class="menu">Option four</li>
            <li class="menu">Option five</li>
        </ul>
    </li>
    <li class="menu"">Special Tricast</li>

The span is positioned absolute with a set width and height and contains an arrow image as the background.

I'm using jQuery to animate the span/arrow image when a user clicks on a menu class to move the active span behind the selected menu (li) element as a visual indicator for their selection.

This works fine when active (act) is set at the root li but if positioned anywhere else in the list for example: -

<ul id="menu">
        <li class="menu">Additional Fixture<span class="active"></span></li>
        **<li class="menu act">list with sub menu</li>**
</ul>

The arrow is positioned correctly when the document loads but doesn't move to the correct selection when clicked by a user. I've tried both position and offset for my divLocation variable, and i'm currently using position as offset fails to work even at root.

I'm basically wanting the arrow to be set depending on what page the user has navigated to, so the animation needs to work from whatever starting position regardless of the length or position of the list.

I've trawled the forums and tried a few things but i'm stumped to be honest and would greatly appreciate any assistance!!

Below is the jQuery code for animating the span/arrow image: -

var divLocation = $(this).position();

$("span.active").animate({
  'top': divLocation.top + topOffset,
      'left': divLocation.left + leftOffset

}, animationSpeed, "linear", function(){ 

$(selectedElement).addClass("text-light"); 
    $(selectedElement).fadeTo("100", opacityIn);


});

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

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

发布评论

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

评论(2

负佳期 2024-12-09 07:51:38

我不确定是否有问题,但检查子菜单 li 是否有“position:relative”。如果跨度定位不正确,这可能就是原因。

更新

我认为你应该稍微重写一下脚本。

// set height of the main links
var mainLinksHeight = 35;

// get index of the clicked element
var nextLink;

$('#menu li').click(function () {
    nextLink = $(this).index();
    $("span.active").animate({ top: nextLink * mainLinksHeight }, animationSpeed, "linear");
});

希望您能够根据您的需要修改此基本代码。

I'm not sure, if I've got the problem, but check if submenu li has "position:relative". This can be the cause, if span is not correctly positioned.

UPDATE

I think you should rewrite the script a little bit.

// set height of the main links
var mainLinksHeight = 35;

// get index of the clicked element
var nextLink;

$('#menu li').click(function () {
    nextLink = $(this).index();
    $("span.active").animate({ top: nextLink * mainLinksHeight }, animationSpeed, "linear");
});

Hope, you will be able to modify this basic code to your needs.

你是暖光i 2024-12-09 07:51:38

解决方案:

我开始使用下面发布的代码,并意识到通过将包含箭头图像的跨度嵌套在 li 元素本身中,我让自己的生活变得困难。这使得当前 li 元素始终处于跨度的 0 位置,因此,如果它位于节点 3,移动到节点 1,则必须转到负数。

修复实际上非常简单,我将跨度移动到无序列表的顶部,并使用 jQuery .css 设置页面加载时图像的起始位置。这样我根本不必更改动画代码或弄乱方程..数学从来都不是我的强项;)

可以在这里看到工作解决方案(http://www.kryptonite-dove.com/sandbox/menu /fix.html)感谢您的帮助!

Solution:

I started playing with the code posted below and realised I was making life difficult for myself by nesting the span containing the arrow image in the li elements themselves. This made the current li element always the 0 position for the span so that if it was at node 3 travelling to node 1 it would have to go to minus numbers.

The fix was pretty simple really, I moved the span to the the the top of my unordered list and used jQuery .css to set the start position of the image on page load. This way i didn't have to change the animation code at all or mess with equations.. maths was never my strong point ;)

Working solution can be seen here (http://www.kryptonite-dove.com/sandbox/menu/fix.html) thanks for the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文