将hoverIntent添加到jquery下拉菜单脚本

时间:2011-03-22 15:54:45

标签: jquery navigation drop-down-menu hoverintent

大家好我想将hoverIntent事件添加到我的jquery下拉菜单顶部链接,但我不知道我是怎么做的,请帮助下面是我从http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/得到的jquery代码 我无法在此处发布代码,请在此处http://www.dynamicdrive.com/cssexamples/media/jqueryslidemenu.js

结帐

2 个答案:

答案 0 :(得分:0)

我使用doTimeout plugin

doTimeout Hover Intent Page

并编写与此类似的代码

  $("#writer").find('.note, .task').hover(function () {
            $(this).find('.listed').stop(true, true).doTimeout('listed', 250, 'slideDown', 'fast', 'easeInOutCubic');
        }, function(){
            $(this).find('.listed').stop(true, true).doTimeout('listed', 250, 'slideUp', 'fast', 'easeInOutCubic');
        });

答案 1 :(得分:0)

更新以使用动态驱动器的jquery幻灯片菜单:

请参阅此处的示例:http://jsfiddle.net/vnvQn/

在dynamicdrive slidemenu.js中

改变这个:

$curobj.hover(
                function(e) {
                    var $targetul = $(this).children("ul:eq(0)")
                    this._offsets = {
                        left: $(this).offset().left,
                        top: $(this).offset().top
                    }
                    var menuleft = this.istopheader ? 0 : this._dimensions.w
                    menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) : menuleft
                    if ($targetul.queue().length <= 1) //if 1 or less queued animations
                    $targetul.css({
                        left: menuleft + "px",
                        width: this._dimensions.subulw + 'px'
                    }).slideDown(jqueryslidemenu.animateduration.over)
                }, function(e) {
                    var $targetul = $(this).children("ul:eq(0)")
                    $targetul.slideUp(jqueryslidemenu.animateduration.out)
                }) //end hover

到此:

var hoverOver = function(e) {
    var $targetul = $(this).children("ul:eq(0)")
    this._offsets = {
        left: $(this).offset().left,
        top: $(this).offset().top
    }
    var menuleft = this.istopheader ? 0 : this._dimensions.w
    menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) : menuleft
    if ($targetul.queue().length <= 1) //if 1 or less queued animations
        $targetul.css({
            left: menuleft + "px",
            width: this._dimensions.subulw + 'px'
        }).slideDown(jqueryslidemenu.animateduration.over)
}
var hoverOut = function(e) {
    var $targetul = $(this).children("ul:eq(0)")
    $targetul.slideUp(jqueryslidemenu.animateduration.out)
} //end hover

var config = {    
    over: hoverOver, // function = onMouseOver callback (REQUIRED)    
    timeout: 500, // number = milliseconds delay before onMouseOut    
    out: hoverOut // function = onMouseOut callback (REQUIRED)    
};

$curobj.hoverIntent(config);

初步回复:

来自Soh Tonaka的Mega Dropdown Menu by Soh Tanaka

的例子

使用HTML菜单结构,如:

<ul id="topnav"> 
        <li> 
            <a href="#" class="home">Home</a> 
        </li> 
        <li> 
            <a href="#" class="products">Products</a> 
            <div class="sub"> 
                <ul> 
                    <li><h2><a href="#">Desktop</a></h2></li> 
                    <li><a href="#">Navigation Link</a></li> 
                    <li><a href="#">Navigation Link</a></li> 
                    <li><a href="#">Navigation Link</a></li> 
                    <li><a href="#">Navigation Link</a></li> 
                    <li><a href="#">Navigation Link</a></li> 
                    <li><a href="#">Navigation Link</a></li> 
                    <li><a href="#">Navigation Link</a></li> 
                </ul> 
            </div>
        ...

使用jquery,如:

function megaHoverOver(){
        $(this).find(".sub").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function($) { 
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() {                    
                    rowWidth += $(this).width(); 
                }); 
            };
        })(jQuery); 

        if ( $(this).find(".row").length > 0 ) { //If row exists...
            var biggestRow = 0; 
            //Calculate each row
            $(this).find(".row").each(function() {                             
                $(this).calcSubWidth();
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({'width' :biggestRow});
            $(this).find(".row:last").css({'margin':'0'});

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({'width' : rowWidth});

        }
    }

    function megaHoverOut(){ 
      $(this).find(".sub").stop().fadeTo('fast', 0, function() {
          $(this).hide(); 
      });
    }


    var config = {    
         sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
         interval: 100, // number = milliseconds for onMouseOver polling interval    
         over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
         timeout: 500, // number = milliseconds delay before onMouseOut    
         out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#topnav li .sub").css({'opacity':'0'});
    $("ul#topnav li").hoverIntent(config);