Skip to content

Instantly share code, notes, and snippets.

@PoTHuYJoHN
Last active August 30, 2019 22:39
Show Gist options
  • Save PoTHuYJoHN/6525579 to your computer and use it in GitHub Desktop.
Save PoTHuYJoHN/6525579 to your computer and use it in GitHub Desktop.
full screen background image of any dimension
.bg {
display: block;
height: 100%;
left: 0;
overflow: hidden;
position: fixed;
top: 0;
width: 100%;
z-index: 0;
}
var bg = {
settings : {
imageW : 1600,
imageH : 1608
},
$img : false,
init: function () {
this.$wrap = $('body');
this.$img = $('.bg').children('img')
this.ratio = this.settings.imageW / this.settings.imageH;
this.setBg();
this.$wrap.on('resize', bg.setBg());
},
setBg : function() {
this.bgRatio = this.$wrap.width() / this.$wrap.height();
if (bg.bgRatio < bg.ratio) {
this.$img.css({
'height': '100%',
'width': 'auto'
});
} else {
this.$img.css({
'width': '100%',
'height': 'auto'
});
}
}
}
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="bg.css">
<script src="bg.js" type="text/javascript">
</head>
<body>
<div class="bg">
<img src="/images/bg.jpg" height="1608" width="1600" alt="Background">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment