0

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?

2 Answers 2

1

For the second option put the if statement that checks the css property inside the window resize event like:

$(window).resize(function(){    
    if($(".link-panel").css("float") == "left"){
        -- Rest of code here --
    }else{
        -- Other option if check is false --
    }
});

This is (in my opinion) the best way to do what you are trying to do as css media queries can be made very specific when working on a responsive site.

Sign up to request clarification or add additional context in comments.

4 Comments

With the resize function, the script only works if the window is resized. If it's not resized, then the script will not run.
Yes it must be run initially. The code I provided is to check on resize. I am answering this part of your question "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?"
How do I make it so that it runs initially? I've tried $(document).ready and inserting some if/else statements in there, but it seems like it only works when you refresh the page
$(function(){ if($(".link-panel").css("float") == "left"){ -- Rest of code here -- }else{ -- Other option if check is false -- } });
0

Wrap your code into function and call twice, via $(document).ready() and $( window ).resize(), call your function twice, you need to re check every time window resize event.

i add sample below, check it, try to resize the screen. using full page and back to normal, you can see the width are updating. because your code runs only 1 time.

function mydocwidth(){
  console.log($(document).width());
  //put your code here
}

$(document).ready(function(){
  mydocwidth();
});

$(window).resize(function(){
  mydocwidth();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

3 Comments

I don't understand what you mean, can you please explain in more detail?
check the console output below.
by the way, this is for first solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.