如何对数组中存储的元素进行动画处理?除了我悬停的那一个?
这就是我所拥有的:
jQuery.each(shapes, function(i) {
this.hover(function(event) {
this.animate({
fill: "#fff"
}, 500);
}, function(event) {
this.animate({
fill: "#555"
}, 500);
});
});
我正在使用 raphael.js,但我认为这个问题对于 jQuery 语法来说是普遍存在的。
因此,当我将鼠标悬停在某个元素(存储在形状中)上时,我希望该元素保持原样,但随后更改所有其他元素的不透明度。我不知道从哪里开始。=\
编辑:
所以,我觉得,就语义而言,这应该有效:
jQuery.each(shapes, function(i) {
current_shape = this;
this.hover(function(event) {
jQuery.each(shapes, function(j){
if (shapes[users_with_direct_employees[j][0]] != current_shape){
shapes[users_with_direct_employees[j][0]].animate({
fill: "#fff"
}, 500);
}
});
}, function(event) {
jQuery.each(shapes, function(j){
if (shapes[users_with_direct_employees[j][0]] != current_shape){
shapes[users_with_direct_employees[j][0]].animate({
fill: "#555"
}, 500);
}
});
});
});
但只有最后一个触摸的形状才会有动画。我将在这里稍微做一个 js fiddel
This is what I have:
jQuery.each(shapes, function(i) {
this.hover(function(event) {
this.animate({
fill: "#fff"
}, 500);
}, function(event) {
this.animate({
fill: "#555"
}, 500);
});
});
I'm using raphael.js, but I figure this issue is general to jQuery syntax.
So, when I hover over an element (stored in shapes) I want that element to remain as-is, but then change che opacity of all the other elements. I don't know where to start.=\
EDIT:
so, I feel like, as far as semantics goes, this should work:
jQuery.each(shapes, function(i) {
current_shape = this;
this.hover(function(event) {
jQuery.each(shapes, function(j){
if (shapes[users_with_direct_employees[j][0]] != current_shape){
shapes[users_with_direct_employees[j][0]].animate({
fill: "#fff"
}, 500);
}
});
}, function(event) {
jQuery.each(shapes, function(j){
if (shapes[users_with_direct_employees[j][0]] != current_shape){
shapes[users_with_direct_employees[j][0]].animate({
fill: "#555"
}, 500);
}
});
});
});
but only the last touched shape does the animation. I'll make a js fiddel here in a bit
fiddle: http://jsfiddle.net/G5mTx/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设,shapes数组是一个DOM元素数组,我认为你可以使用这样的东西,为每个形状设置一个悬停事件处理程序,然后在传递给悬停事件处理程序的每个函数中,迭代shapes数组并如果它不是您悬停的那个,您就制作动画。
或者使用本地函数可能会更清晰:
编辑:我看到你现在已经在你的问题中添加了实际的代码(因为我写了我的答案)并且形状不是 DOM 对象的数组(它如果您最初披露了这一点,那就太好了)所以显然这段代码不会完全按原样工作,但希望您可以从这段代码中得到关于如何迭代所有其他形状并仅排除当前形状的想法悬停在上面,然后您可以将其调整为您的特定形状数据结构。
Assuming, the shapes array is an array of DOM elements, I think you can use something like this where you set up a hover event handler for each shape and then within each function passed to the hover event handler, you iterate over the shapes array and if it's not the one that you are hovering over, you do the animation.
or it might be cleaner with a local function:
EDIT: I see you've added actual code now to your question (since I wrote my answer) and shapes isn't an array of DOM objects (it would be nice if you had disclosed that originally) so obviously this code won't work exactly as it is, but hopefully you can get the idea from this code for how you can iterate over all the other shapes and just exclude the current one you are hovering on and you can then adapt it to your particular shapes data structure.
您可以使用方法.not():
You can use the method .not():