I'm trying to resize a background image on my Square space website (link here) based on the size of the user's browser window. More specifically, if the user's window size drops below a certain width then the image adjusts to a smaller size and maintains its aspect ratio.
This seems to be a common issue. I've been digging for hours now and can't seem to find a solution. I'm using JavaScript to do the work, but it won't get it done.
Mind you all, I'm talking about something different than fluid resizing (i.e. using percentage values).
I've run this code through a quality validation and it came back with no syntax errors. So beyond this, I'm lost.
I injected this (jQuery) script into the page header:
$(document).ready(function() {
function imageresize() {
var contentwidth = $(window).width();
if ((contentwidth) < 1500) {
$(".widescreen").attr("height", "480px");
} else {
$(".widescreen").attr("height", "768px");
}
}
imageresize(); //Triggers when document first loads
$(window).on("resize", function() {
imageresize();
});
});
.widescreen {
background: url(https://i.imgur.com/96pxGnd.jpg) center top no-repeat;
background-size: cover;
width: 100vw;
position: relative;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
height: 768px;
/* Trying to change this value so the rest of the CSS adjusts accordingly */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Is the syntax wrong? Please help! And thank you in advance.