How to ensure CSS :hover is applied to dynamically added element

后端 未结 9 1051
广开言路
广开言路 2021-02-07 14:59

I have a script that adds full images dynamically over thumbnails when you hover over them. I\'ve also given the full images a CSS :hover style to make them expand to a larger w

相关标签:
9条回答
  • 2021-02-07 15:54

    Don't let the IMG tag get added to the DOM until it has an image to download. That way the Load event won't fire until the image has been loaded. Here is the amended JS:

    $(function () {
    
        var fullimageurl = 'http://upload.wikimedia.org/wikipedia/commons/3/32/Cairo_International_Stadium.jpg';
    
        var fullimage = $('<img/>')
            .addClass('kiyuras-image')
            .load(function () {
                anchor.show(); // Only happens after IMG src has loaded
            });
    
        var anchor = $('<a/>').hide();
    
        $('body').prepend(anchor);
    
        $("#image").on('mouseenter', function () {
            fullimage.attr('src',fullimageurl); // IMG has source
            $(this).off('mouseenter');
            anchor.append(fullimage); // Append IMG to DOM now.
        });
    
    });
    
    0 讨论(0)
  • 2021-02-07 15:55

    I did that and it worked on Chrome (version 22.0.1229.94 m): I changed the css as that:

    .kiyuras-image{
        position: absolute;
        top: 8px;
        left: 8px;
        max-width: 400px;
    }
    .not-hovered{
        max-width: 220px;
    }
    

    and the script this way:

    $(function(){
        var fullimageurl = 'http://upload.wikimedia.org/wikipedia/commons/3/32/Cairo_International_Stadium.jpg';
    
        var fullimage = $('<img/>')
            .addClass('kiyuras-image')
            .load(function () {
                anchor.show();
            });
    
        var anchor = $('<a/>').hide().append(fullimage);
    
        $('body').prepend(anchor);
    
        $('.kiyuras-image').on('mouseout',function(){
            $(this).addClass('not-hovered');
        });
        $('.kiyuras-image').on('mouseover',function(){
            $(this).removeClass('not-hovered');
        });
    
        $("#image").one('mouseover', function(){
            fullimage.attr('src',fullimageurl);
        });
    });
    

    Basically I think it's a Chrome bug in detecting/rendering the 'hover' status; in fact when I tried to simply change the css as:

    .kiyuras-image{
        position: absolute;
        top: 8px;
        left: 8px;
        max-width: 400px;
    }
    .kiyuras-image:not(:hover) {
        position: absolute;
        top: 8px;
        left: 8px;
        max-width: 220px;
    }
    

    it still didn't worked.

    PS: sorry for my english.

    0 讨论(0)
  • 2021-02-07 15:58

    Most browsers update their hover states only when the cursor moves over an element by at least one pixel. When the cursor enters the thumbnail's img it gets hover applied and runs your mouseenter handler. If you keep your cursor still until the full-sized image loads, your old img (the thumbnail) will keep the hover state and the new one won't get it.

    To get it working in these browsers, move the hover pseudo-class to a common parent element in the CSS; for example, enclose both imgs in a span.

    0 讨论(0)
提交回复
热议问题