Javascript 如何在相同状态下结合单击和悬停功能?;
我试图添加点击功能,但它没有运行Javascript 如何在相同状态下结合单击和悬停功能?;,javascript,jquery,Javascript,Jquery,我试图添加点击功能,但它没有运行 $("#menu2 li").hover(function() { //On hover... $(this).find("span").stop().animate({ marginTop: "-40" //Find the span tag and move it up 40 pixels }, 250); $("#menu2 li").click(function() { //On click...
$("#menu2 li").hover(function() { //On hover...
$(this).find("span").stop().animate({
marginTop: "-40" //Find the span tag and move it up 40 pixels
}, 250);
$("#menu2 li").click(function() { //On click...
$(this).find("span").stop().animate({
marginTop: "-40" //Find the span tag and move it up 40 pixels
}, 250); }
, function() { //On hover out...
$(this).find("span").stop().animate({
marginTop: "0" //Move the span back to its original state (0px)
}, 250);
});
});
以下是链接:更新:在看到您的链接并阅读了说明后,我会这样做: 创建选定的CSS类
:
.selected span {
marginTop: "-40";
}
单击时将类添加到元素,并且仅在未选择元素时执行mouseleave
操作:
$("#menu2 li").click(function() {
if(!$(this).hasClass('selected')) {
$(this).siblings('.selected').removeClass('selected').mouseleave();
$(this).addClass('selected');
}
}).hover(function() {
$(this).find("span").stop().animate({
marginTop: "-40" //Find the span tag and move it up 40 pixels
}, 250);
}, function() {
if(!$(this).hasClass('selected')) {
$(this).find("span").stop().animate({
marginTop: "0" //Move the span back to its original state (0px)
}, 250);
}
});
我为你创造了一个新的世界
旧答案:
可能是:
function mouseEnterHandler {
$(this).find("span").stop().animate({
marginTop: "-40" //Find the span tag and move it up 40 pixels
}, 250);
}
function mouseLeaveHandler() {
$(this).find("span").stop().animate({
marginTop: "0" //Move the span back to its original state (0px)
}, 250);
}
$("#menu2 li").click(mouseEnterHandler)
.hover(mouseEnterHandler, mouseLeaveHandler);
我想你说的是将悬停和单击绑定在一起。试试这个:
$('#menu2 li').bind('click hover', function(){
// do stuff
});
你能再解释一下你想做什么吗?是否要执行与在单击上执行的mouseenter
(悬停)相同的函数?单击菜单时,单击函数与悬停相同。如何组合它?好的…我尝试添加单击函数,但没有运行。$(“#menu2 li”).hover(函数(){//on hover…$(此)。查找(“span”).stop().animate({marginTop:“-40”//找到span标记并将其向上移动40像素},250);$(“#menu2 li”)。单击(函数(){//单击…$(此)。查找(“span”).stop()。动画({marginTop:-40”//找到span标记并将其向上移动40像素},250);},函数(){//悬停…$(此)。查找(“span”).stop()。动画({marginTop:”0//将跨度移回其原始状态(0px)},250);});});请看一下我的最新答案。下一次,请准确回答您的问题,并提供所有必要的信息。这很好,我会创建一个单独的函数,并使用标准的click&hover API将其与click&hover绑定,但这样更好。不知道我们能做到+1-1$('sel').bind('hover',function(){/*…*/})
不起作用(对于所有想知道的人:)。即使如此,它也将与$('sel')相同。悬停(in)
,而不是$('sel')。悬停(in,out)
,所以cooooool!非常感谢Felix Kling,你帮了我很多忙~这就是我想要的。