简体   繁体   English

更改悬停以单击菜单

[英]Changing hover to click on menu

I am having trouble changing the function to be called on 'click' rather than 'hover'. 我在更改要在“单击”而不是“悬停”上调用的函数时遇到了麻烦。 I have tried changing 'hover' to 'click' as well as 'toggle' with no positive results 我尝试将“悬停”更改为“点击”以及“切换”,但没有取得积极的结果

$('#menu li').hover(
    function(){
            $(this).stop().animate({height: '100px'},1000,function(){});
            $(this).find('div').show(600);
        }
    ,
    function(){
            $(this).stop().animate({height: '20px'},1000,function(){});
        } 
  ); 

Do I have to bind the event if I want to change it to 'click'? 如果要将事件更改为“单击”,是否必须绑定事件? Any help is greatly appreciated. 任何帮助是极大的赞赏。

Here's the fiddle: http://jsfiddle.net/GANeX/7/ 这是小提琴: http : //jsfiddle.net/GANeX/7/

try this: 尝试这个:

http://jsfiddle.net/GANeX/52/ http://jsfiddle.net/GANeX/52/

var clicked = 0;
$('#menu li').click(
   function(){
        if(clicked === 0){
            $(this).stop().animate({height: '100px'},1000,function(){});
            $(this).find('div').show(600);
            clicked = 1;
        } else if(clicked === 1){
            $(this).stop().animate({height: '20px'},1000,function(){});  
            clicked = 0;
        }
    }
 );

set a variable to keep track of clicked state 设置变量以跟踪单击状态

You are passing two parameters to $(...).click(clickHandle) . 您要将两个参数传递给$(...).click(clickHandle) Just pass one function. 只需传递一个功能。

Check http://api.jquery.com/click/ 检查http://api.jquery.com/click/

In two arguments case, 在两种情况下,

http://api.jquery.com/click/

当我更改时, 单击只需要一个参数就可以正常工作。

hover() takes two callbacks, one for mouseenter and one for mouseleave . hover()需要两个回调,一个用于mouseenter ,一个用于mouseleave If you want to change it to click, and the first click starts the first animation and the second starts the second one, you need to add a state variable to determine whether your element has been clicked once or twice. 如果要将其更改为单击,并且第一次单击将启动第一个动画,第二次单击将启动第二个动画,则需要添加一个状态变量以确定您的元素是否被单击了一次或两次。

$('#menu li').click(
function(){
        if(this.clicked)
        {
         //callback 1
        }
        else
       {
        //callback 2
       }
       this.clicked = !this.clicked;
    } 

); );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM