Search code examples
jqueryhtmlcssperformanceanimated-gif

Enhancing performance of a gif background rollover


I made a cool rollover in css which display an animate gif when the parent is hovered.

Here's my code : http://codepen.io/clemeeent/pen/oggzMa

Problem is I will have about 40 .day like that, all playing the animate gif behind the circle at any time. I'm not sure that any browser/computer/connection can handle that much. I tried to figure out a solution like :

$( ".day" ).mouseenter(function() {
  $( ".play" ).append( "<img src="http://media.giphy.com/media/5Vb7xQB7Z3ScE/giphy.gif">" );
});

But i'm definitly not sure it will be better...

If someone as any idea to enhance that code, it would be really appreciated.

PS : The gif is just a sample, the final result will be really great :p


Solution

  • as already mentioned in comments above

    do something like this with css

    img {
        border-radius:50%;
    }
    .day {
        margin: 15px;
        height: 90px;
        width: 90px;
        float: left;
        position: relative;
        cursor: pointer;
    }
    .play {
        height: 90px;
        position: absolute;
        width: 90px;
        left: 50%;
        top: 50%;
        margin: -45px 0 0 -45px;
        z-index: -1;
        transition-duration: 100ms;
        transition-timing-function: ease-in-out;
        -moz-transition-property: all;
        -moz-transition-duration: 100ms;
        -moz-transition-timing-function: ease-in-out;
        -webkit-transition-property: all;
        -webkit-transition-duration: 100ms;
        -webkit-transition-timing-function: ease-in-out;
        -o-transition-property: all;
        -o-transition-duration: 100ms;
        -o-transition-timing-function: ease-in-out;
    }
    .play img {
        width: 100%;
        border-radius: 50%;
    }
    .badge {
        height: 35px;
        width: 35px;
        background: #ed2c27;
        border-radius: 50%;
        font-size: 18px;
        position: absolute;
        top: 0;
        right: 0;
        text-align: center;
        font-family:'Satisfy', cursive;
    }
    .day:hover .spinner {
        position: absolute;
        display: block;
        height: 95px;
        width: 95px;
        left: -5px;
        top: -5px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: black;
        -webkit-animation: spin 2s linear infinite;
        animation: spin 2s linear infinite;
    }
    .day:hover .spinner:before {
        content:"";
        position: absolute;
        top: 0px;
        left: 0px;
        right: 0px;
        bottom: 0px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: black;
        -webkit-animation: spin 3s linear infinite;
        animation: spin 3s linear infinite;
    }
    .day:hover .spinner:after {
        content:"";
        position: absolute;
        top: 3px;
        left: 3px;
        right: 3px;
        bottom: 3px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: black;
        -webkit-animation: spin 1.5s linear infinite;
        animation: spin 1.5s linear infinite;
    }
    @-webkit-keyframes spin {
        0% {
            -webkit-transform: rotate(0deg);
            -ms-transform: rotate(0deg);
            transform: rotate(0deg);
        }
        100% {
            -webkit-transform: rotate(360deg);
            -ms-transform: rotate(360deg);
            transform: rotate(360deg);
        }
    }
    @keyframes spin {
        0% {
            -webkit-transform: rotate(0deg);
            -ms-transform: rotate(0deg);
            transform: rotate(0deg);
        }
        100% {
            -webkit-transform: rotate(360deg);
            -ms-transform: rotate(360deg);
            transform: rotate(360deg);
        }
    }
    <div class="day">
        <!-- 1 -->
        <div class="spinner"></div>
        <img data-src="holder.js/90x90" class="img-circle" style="width: 90px; height: 90px; background-color:#D3D3D3;" /> <span class="badge">1</span>
    
    </div>