开发者

Mouseover - mouseout - Javascript

开发者 https://www.devze.com 2022-12-28 22:57 出处:网络
what i\'m trying to do: When a user hovers over an image, a little x (image) should appear in the top right corner. If the user clicks on this little x the image should be deleted and when the user d

what i'm trying to do:

When a user hovers over an image, a little x (image) should appear in the top right corner. If the user clicks on this little x the image should be deleted and when the user does a mouseout the little x should dissapear. I've tried several things:

html structure is an ul with li's and an image in it

Javascript:

//On all the li's in the ul

$("li",$flickrKeyUlPreview).开发者_C百科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");
};

The reason this doesn't work: When the user hovers over the little image the mouseover is triggered.

I've also tried:

$("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.

Tried this too:

$("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);
    };

This gave the same effect, it was still flickering

Does anyone have another idea how to do this (don't need specific codes, concepts are appreciated too ;) ) ?


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


Replace mouseover and mouseout with mouseenter and mouseleave.

0

精彩评论

暂无评论...
验证码 换一张
取 消