I am trying to run a script only when the screen size is greater than 750px, if it's not then the script does not run. I've tried writing some code to do this, but it only works on page refresh. For example, my script makes my side panel (.link-panel) stop when it reaches the (.footer). When I put my screen size code, it works when I resize the screen down to 750px, but when I resize back up the script does not work properly and requires page refresh to work again.
jQuery (1st solution)
if ($(window).width() >= 750) {
var windw = this;
$.fn.followTo = function(elem) {
var $this = this,
$window = $(windw),
$bumper = $(elem),
bumperPos = $bumper.offset().top + -40,
thisHeight = $this.outerHeight(),
setPosition = function() {
if ($window.scrollTop() > (bumperPos - thisHeight)) {
$this.css({
position: 'absolute',
top: (bumperPos - thisHeight)
});
} else {
$this.css({
position: 'fixed',
top: 60
});
}
};
$window.resize(function() {
bumperPos = pos.offset().top;
thisHeight = $this.outerHeight();
setPosition();
});
$window.scroll(setPosition);
setPosition();
};
$('.cover').followTo('.footer');
}
jQuery second solution
if ($(".link-panel").css("float") == "left") {
var windw = this;
$.fn.followTo = function(elem) {
var $this = this,
$window = $(windw),
$bumper = $(elem),
bumperPos = $bumper.offset().top + -40,
thisHeight = $this.outerHeight(),
setPosition = function() {
if ($window.scrollTop() > (bumperPos - thisHeight)) {
$this.css({
position: 'absolute',
top: (bumperPos - thisHeight)
});
} else {
$this.css({
position: 'fixed',
top: 60
});
}
};
$window.resize(function() {
bumperPos = pos.offset().top;
thisHeight = $this.outerHeight();
setPosition();
});
$window.scroll(setPosition);
setPosition();
};
$('.cover').followTo('.footer');
}
My second solution only runs the script when my float property on .link-panel is left. My float: left property is removed in a media query when the page is 750px. This solution works only on page refreshes just like the last solution. How can I make it so that if the user resizes from <= 750px to > 750px they don't need to refresh the page in order for the script to work properly?