鼠标悬停 - mouseout - Javascript

时间:2010-04-30 14:43:29

标签: javascript image mouseover mouseout

我正在尝试做什么:

当用户将鼠标悬停在图像上时,右上角会出现一个小x(图像)。如果用户点击这个小x,则应该删除图像,当用户执行mouseout时,小x应该消失。我尝试了几件事:

html结构是带有li的ul和其中的图像

使用Javascript:

//On all the li's in the ul

$("li",$flickrKeyUlPreview).mouseover(addExternalImage);

var addExternalImage = function(){

        //Get the offset of the image the user is hovering over
        var offset = $(this).offset();

        //Move the little x button to the image
        $flickrDetailButton.offset(offset);

        //Set it visible
        $flickrDetailButton.css("visibility","visible");

        //Bind the event for the mouseout
        $flickrDetailButton.mouseout(removeExternalButton);
};

var removeExternalButton = function(){

        //Hide the little x
        $flickrDetailButton.css("visibility","hidden");
};

这不起作用的原因:当用户将鼠标悬停在小图像上时,会触发鼠标悬停。

我也试过了:

$("li",$flickrKeyUlPreview).mouseover(addExternalImage);

     var addExternalImage = function(){
        $(this).unbind('mouseover');
        var emptyObject = {};
        $(this).append($.TemplateRenderer($flickrDetailButton,emptyObject));
        $flickrDetailButton = $('#flickr_detail_button',rootel);
        $(this).mouseout(removeExternalButton);
    };


   var removeExternalButton = function(){
        $(this).unbind('mouseout');
        $flickrDetailButton = $('#flickr_detail_button',rootel);
        if ($($flickrDetailButton, $(this))) {
            $($flickrDetailButton, $(this)).remove();
        }
        $(this).mouseover(addDelBtn);
    };

This doesn't work that well, the little x starts flickering.

也试过这个:

$("li",$flickrKeyUlPreview).mouseenter(addExternalImage);

    var removeExternalButton = function(){
        $flickrDetailButton = $('#flickr_detail_button', rootel);
        if ($($flickrDetailButton, $(this))) {
            $($flickrDetailButton, $(this)).remove();
        }
        $(this).mouseenter(addExternalImage);
    };


    var addExternalImage = function(){
        var emptyObject = {};
        $(this).append($.TemplateRenderer($flickrDetailButtonTemplate,emptyObject));
        $flickrDetailButton = $('#flickr_detail_button',rootel);
        $(this).mouseout(removeExternalButton);
        $flickrDetailButton.mouseleave(removeExternalButton);
    };

这产生了同样的效果,它仍然在闪烁

有没有人有另外的想法如何做到这一点(不需要特定的代码,概念也受到赞赏;))?

2 个答案:

答案 0 :(得分:1)

$('selector')。hover(addExternalImage,removeExternalButton);

答案 1 :(得分:0)

mouseovermouseout替换为mouseentermouseleave