将 jquery 调用合并到 1 个函数中

发布于 2024-09-14 07:17:56 字数 873 浏览 4 评论 0原文

我有以下在悬停时动画的jquery:

        $('#footerNetwork').hover(function(){
            $('#popupNetwork').animate({top:'-62px'},{queue:true,duration:500});
        }, function(){
            $('#popupNetwork').animate({top:'30px'},{queue:true,duration:500});
        });

        $('#footerPort').hover(function(){
            $('#popupPort').animate({top:'-62px'},{queue:true,duration:500});
        }, function(){
            $('#popupPort').animate({top:'30px'},{queue:true,duration:500});
        });

        $('#footerAirport').hover(function(){
            $('#popupAirport').animate({top:'-62px'},{queue:true,duration:500});
        }, function(){
            $('#popupAirport').animate({top:'30px'},{queue:true,duration:500});
        });

等等...

我怎样才能将这些组合到on函数中,该函数可以识别哪个链接已悬停(即:footerNetwork)并针对适当的div进行动画(popupNetwork)? 谢谢!

I have the following jquery which animates on hover:

        $('#footerNetwork').hover(function(){
            $('#popupNetwork').animate({top:'-62px'},{queue:true,duration:500});
        }, function(){
            $('#popupNetwork').animate({top:'30px'},{queue:true,duration:500});
        });

        $('#footerPort').hover(function(){
            $('#popupPort').animate({top:'-62px'},{queue:true,duration:500});
        }, function(){
            $('#popupPort').animate({top:'30px'},{queue:true,duration:500});
        });

        $('#footerAirport').hover(function(){
            $('#popupAirport').animate({top:'-62px'},{queue:true,duration:500});
        }, function(){
            $('#popupAirport').animate({top:'30px'},{queue:true,duration:500});
        });

etc...

how can I combine these into on function which recognises which link has been hovered (ie: footerNetwork) and targets the appropriate div to animate (popupNetwork)??
thanks!

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

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

发布评论

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

评论(3

疯了 2024-09-21 07:17:56

你可以这样做:

var tops = { footerNetwork:'-80px', footerPort:'-62px', footerAirport:'-62px' };
$('#footerNetwork, #footerPort, #footerAirport').hover(function(){
  $('#'+this.id.replace('footer','popup')).animate({top: tops[this.id]}, 500);
}, function(){
  $('#'+this.id.replace('footer','popup')).animate({top:'30px'}, 500);
});

如果你向这些元素添加一个类,例如 class="footer" 那么你可以更改 .hover() 改为 $('.footer').hover(function(){ 使其更加简洁要获取适当的 #popup_____ 元素,我们只需获取当前 ID,例如 footerNetwork 并执行 .replace() 获取弹出窗口 ID。 tops对象是存储各种最高值,因为它们不同。

You could do something like this:

var tops = { footerNetwork:'-80px', footerPort:'-62px', footerAirport:'-62px' };
$('#footerNetwork, #footerPort, #footerAirport').hover(function(){
  $('#'+this.id.replace('footer','popup')).animate({top: tops[this.id]}, 500);
}, function(){
  $('#'+this.id.replace('footer','popup')).animate({top:'30px'}, 500);
});

If you add a class to those elements say class="footer" then you can change the .hover() to $('.footer').hover(function(){ to make it even cleaner. To get the appropriate #popup_____ element we're just take the current ID, e.g. footerNetwork and doing a .replace() to get the popup ID. The tops object is to store the various top values since they differ.

万劫不复 2024-09-21 07:17:56

我假设您在要添加悬停行为的项目上有一个类 foo 。然后,只需遵循(明显的)footer... 模式:

$(document).ready( function(){
  $('.foo').hover( 
    function(){
      $('#popup' + this.id.substr(6,this.id.length-6) ).animate({...});
      //                          ^remove "footer" portion of id
    },
    function(){
      $('#popup' + this.id.substr(6,this.id.length-6) ).animate({...});
    }
  );
});

根据文档的结构,您还可以通过其容器来标识“footer...”元素,而不是通过其容器来识别“footer...”元素。按班级。

I'll assume you have a class foo on the items to which you want to add the hover behavior. Then it's just a matter of following the (apparent) footer... pattern:

$(document).ready( function(){
  $('.foo').hover( 
    function(){
      $('#popup' + this.id.substr(6,this.id.length-6) ).animate({...});
      //                          ^remove "footer" portion of id
    },
    function(){
      $('#popup' + this.id.substr(6,this.id.length-6) ).animate({...});
    }
  );
});

Depending on how your document is structured, you could also identify the "footer..." elements by their container, rather than by class.

水晶透心 2024-09-21 07:17:56

由于偏移量不是固定的,因此不可能通过一次调用获得相同的结果,但是,像这样的函数可以解决问题:

function hoverIt(name, topIn, topOut, duration)
    duration = (duration != undefined) ? duration : 500;

    $('#footer' + name).hover(function(){
        $('#popup' + name).animate({top: topIn + 'px'},
                            {queue: true, duration: duration});
    }, function(){
        $('#popup' + name).animate({top: topOut + 'px'},
                            {queue: true, duration: duration});
    });
}

然后只需为每个动画调用该函数:

hoverIt('Network', -80, 30, 300);
hoverIt('Port', -62, 30);
hoverIt('Airport', -62, 30);

我猜会更好。当它们很多时,您可以使用类似以下内容的内容:

var hovers = [['Network', -80, 30, 300],
              ['Port', -62, 30],
              ['Airport', -62, 30]];

for (var i = 0; i < hovers.length; i++) {
    hoverIt(hovers[i][0], hovers[i][1], hovers[i][2], hovers[i][3]);
}

注意:未测试

Since the offsets are not fixed, it's not really possible to get the same result with one call, but, a function like this will do the trick:

function hoverIt(name, topIn, topOut, duration)
    duration = (duration != undefined) ? duration : 500;

    $('#footer' + name).hover(function(){
        $('#popup' + name).animate({top: topIn + 'px'},
                            {queue: true, duration: duration});
    }, function(){
        $('#popup' + name).animate({top: topOut + 'px'},
                            {queue: true, duration: duration});
    });
}

Then just call the function for each animation:

hoverIt('Network', -80, 30, 300);
hoverIt('Port', -62, 30);
hoverIt('Airport', -62, 30);

Is much better I guess. When there'll be a lot of them, you could use something like:

var hovers = [['Network', -80, 30, 300],
              ['Port', -62, 30],
              ['Airport', -62, 30]];

for (var i = 0; i < hovers.length; i++) {
    hoverIt(hovers[i][0], hovers[i][1], hovers[i][2], hovers[i][3]);
}

note: Not tested

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