css: avoid image hover first time blinking

前端 未结 12 592
深忆病人
深忆病人 2020-12-07 19:52

I have an anchor that changes its background image when hovered with a class class-btn that contains a background-image.

When hovered, it

相关标签:
12条回答
  • 2020-12-07 20:38

    This is a non-CSS solution: if the hover images are in one directory and have a common naming convention, for example contain a substring '-on.', it is possible to select the file names and put it into the HTML as a series of:

    <img src='...' style='display: none' />
    
    0 讨论(0)
  • 2020-12-07 20:40

    The "double up the original background image" trick didn't work for me so I used another css trick:

    .next {
        background: url(../images/next.png) center center no-repeat;        
    }
    .next:hover {
        background: url(../images/next-hover.png) center center no-repeat;
    }
    .next:after {
        content: url(../images/next-hover.png);
        display: none;
    }
    
    0 讨论(0)
  • 2020-12-07 20:43

    A simple trick I use is to double up the original background image making sure to put the hovered image first

    .next {
      background: url(../images/next-hover.png) center center no-repeat;
      background: url(../images/next.png) center center no-repeat;
        &:hover{
          background: url(../images/next-hover.png) center center no-repeat;
        }
     }
    

    No performance hit and very simple

    Or if you're not using SCSS yet:

    .next {
      background: url(../images/next-hover.png) center center no-repeat;
      background: url(../images/next.png) center center no-repeat;        
     }
     .next:hover{
      background: url(../images/next-hover.png) center center no-repeat;
     }
    
    0 讨论(0)
  • 2020-12-07 20:44

    If you do this:

    #the-button {
    background-image: url('images/img.gif');
    }
    #the-button:before {
      content: url('images/animated-img.gif');
      width:0;
      height:0;
      visibility:hidden;
    }
    
    #the-button:hover {
      background-image: url('images/animated-img.gif');
    }
    

    This will really help!

    See here:

    http://particle-in-a-box.com/blog-post/pre-load-hover-images-css-only

    P.S - not my work but a solution I found :)

    0 讨论(0)
  • 2020-12-07 20:49

    This technique works nicely for me and ensures not only is the hover image pre-loaded, but it's also ready and waiting to be displayed. (Most other solutions rely on switching the background image on hover, which just seems to take the browser a bit of time to figure out, however much the image is pre-loaded.)

    Create :before and :after pseudo elements on the container with the two images, but hide the one you want to see on hover. Then, on hover, switch the visibility.

    So long as they both share the same size and positioning rules, you should see a neat swap.

    .image-container {
        &:before { display: block; background-image: url(uncovered.png); }
        &:after { display: none; background-image: url(uncovered.png); }
    }
    .image-container:hover {
        &:before { display: none; }
        &:after { display: block; }
    }
    
    0 讨论(0)
  • 2020-12-07 20:50

    You can preload images

    function preloadImages(srcs, imgs, callback) {
    var img;
    var remaining = srcs.length;
    for (var i = 0; i < srcs.length; i++) {
        img = new Image();
        img.onload = function() {
            --remaining;
            if (remaining <= 0) {
                callback();
            }
        };
        img.src = srcs[i];
        imgs.push(img);
    }
    }
    // then to call it, you would use this
    var imageSrcs = ["src1", "src2", "src3", "src4"];
    var images = [];
    preloadImages(imageSrcs, images, myFunction);
    
    0 讨论(0)
提交回复
热议问题