Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating background picture

i'm becoming desperate.. i try to build a site with a rotating background image, that automatically adjusts to the size of the window. i'm not really that much into html, css, javascript etc, but i managed to get so far, that the picture rotates and adjusts to the windows size.. BUT when i scale the window really small, the picture rotates out of the window.. i hope you understand what i mean.

This is my code:

Fiddle

HTML

<div id="bgcon">
    <div id="bgimg">
    </div>
</div>

CSS

html, body {
    padding:0;
    margin:0;
    width:100%;
    height:100%;
}
#bgcon {
    width:100%;
    height:100%;
    overflow:hidden;
}
#bgimg {
    content:"";
    position: fixed;
    width: 250%;
    height: 250%;
    top: -80%;
    left: -80%;
    z-index: -1;
    overflow:hidden;
    background-image: url("http://sidekickwallpapers.com/wp-content/uploads/2014/03/space-wallpapers-1920x1080-design-ideas-space-wallpapers-s3vgbbit.jpg");
    -webkit-animation:spin 150s linear infinite;
    -moz-animation:spin 150s linear infinite;
    animation:spin 150s linear infinite;
}
}
@-moz-keyframes spin {
    100% {
        -moz-transform: rotate(360deg);
    }
}
@-webkit-keyframes spin {
    100% {
        -webkit-transform: rotate(360deg);
    }
}
@keyframes spin {
    100% {
        -webkit-transform: rotate(360deg);
        transform:rotate(360deg);
    }
}
}

and this is the problem:
Glitch

i'm not sure if it's possible to create it with only css, but like i said, im not really into it much.

Thanks for your help!

like image 667
user3524991 Avatar asked Nov 26 '22 08:11

user3524991


1 Answers

You should set the width & height of bgcon to a higher proportion of the max value of the window's width and height, keeping it square:

var bg = document.getElementById("bgimg");

window.onresize = function() { 
    /* Compare values and set width/height based on higher value */ 
    var max = Math.max(window.innerWidth, window.innerHeight);
    bg.style.width = 2.5*max + "px";
    bg.style.height = 2.5*max + "px";
    bg.style.marginTop = -1.25*max + "px"; // Half of width/height
    bg.style.marginLeft = -1.25*max + "px"; // Half of width/height
}
window.onresize(); // Run on load

HTML:

<div id="bgimg"></div>

and CSS:

#bgimg {
    position:fixed;
    /* Center element on page */
    top:50%; left:50%;
    /* You have to center the image using center center */
    background: url("http://sidekickwallpapers.com/wp-content/uploads/2014/03/space-wallpapers-1920x1080-design-ideas-space-wallpapers-s3vgbbit.jpg") center center no-repeat;
    animation:spin 150s linear infinite;
    transform-origin:center center; /* Makes it rotate about the center */
    /* Others */
}

Demo

The reason why the demo above is not perfect in large browsers is because the image is too small. To counteract that you could remove the no-repeat on the image and it will repeat itself until the space it taken. Or you could use a larger image. Here is a demo with a larger, different image showing it working correctly

like image 196
Zach Saucier Avatar answered Dec 23 '22 04:12

Zach Saucier