jQuery 悬停功能
您好,我正在尝试创建一个 jquery 函数,当单击其中一个 div 时,该函数会淡出所有其他 div。我的代码无法正常工作,我不知道如何正确编写它。这是我到目前为止所拥有的:
$("#slider div.popup").hover(
var ind = $(this).index();
$("#slider div.popup").each(function() {
if ($(this).index() != ind) {
//fades all other .popup divs
$(this).animate({
opacity: 0
});
}
}), $("#slider div.popup").each(function() {
if ($(this).index() != ind) {
//unfades all other .popup divs
$('span', this).animate({
opacity: 1
});
}
}
));
这里还有一个示例:http://jsfiddle. net/7j3mk/
有人可以指导我如何让这段代码工作吗?
Hi I am trying to create a jquery function that fades all other divs when one of the div is clicked on. My code isn't working and I'm not sure how to properly write it. Here is what I have so far:
$("#slider div.popup").hover(
var ind = $(this).index();
$("#slider div.popup").each(function() {
if ($(this).index() != ind) {
//fades all other .popup divs
$(this).animate({
opacity: 0
});
}
}), $("#slider div.popup").each(function() {
if ($(this).index() != ind) {
//unfades all other .popup divs
$('span', this).animate({
opacity: 1
});
}
}
));
There is also an example here : http://jsfiddle.net/7j3mk/
Can someone give me guidance on how to get this code working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除了您在悬停方法中使用的错误语法之外(它需要两个函数作为参数)
您需要使用
.not()
docs 方法更新示例位于http://jsfiddle.net/gaby/7j3mk/ 11/
Beside the wrong syntax you use for the hover method (it takes two functions as parameters)
You need to use the
.not()
docs methodupdated example at http://jsfiddle.net/gaby/7j3mk/11/
试试这个: fiddle
try this: fiddle
您的代码中有几个错误:
span
选择器,可以防止 if 查找任何元素。工作代码: http://jsfiddle.net/7j3mk/7/
There are several errors in your code:
ind
is only defined in the first function, you need to define it in the second also.span
selector in the second function that keeps if from finding any elements.Working code: http://jsfiddle.net/7j3mk/7/
您使用悬停功能是错误的。您向悬停传递两个函数,第一个用于鼠标进入,第二个用于鼠标退出。悬停函数的工作原理如下:
除了这两个函数之外,hover() 内不能有任何过度代码,因此在您的示例中,该行
导致了错误。我建议您进一步阅读 jQuery 文档上的 hover()
You are using the hover function wrong. You pass in two functions to the hover, the first is for the mouse enter, and the second for mouse out. The hover function works some what like this:
You can't have any over code inside the hover() besides those two functions, so in your example the line
Is causing the error. I suggest you read up further on hover() on the jQuery Docs
为什么不尝试像
在悬停功能内一样。看一下文档 - http://api.jquery.com/hover/ - 你通过2 个函数 - 一个要执行“onmouseover”,另一个要执行“onmouseout”。
Why not try something like
Inside your hover function. Take a look at the docs - http://api.jquery.com/hover/ - you pass 2 functions - one to be executed 'onmouseover' and the other to be executed 'onmouseout'.
http://jsfiddle.net/RL6s8/:
编辑:
添加
.stop(true,true)
和 jQuery 的500
ms 来消除无限排队。http://jsfiddle.net/RL6s8/:
EDIT:
Added
.stop(true,true)
and500
ms to the jQuery to eliminate infinite queuing.