Last active
August 30, 2019 22:39
-
-
Save PoTHuYJoHN/6525579 to your computer and use it in GitHub Desktop.
full screen background image of any dimension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.bg { | |
display: block; | |
height: 100%; | |
left: 0; | |
overflow: hidden; | |
position: fixed; | |
top: 0; | |
width: 100%; | |
z-index: 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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