6

I have the jQuery script below that triggers a div to become floating-fixed. (Which is working and I have no problems with it).

$(document).ready(function() {
    ctop = $('#comment').offset().top - parseFloat($('#comment').css('margin-top').replace(/auto/, 0));
});
$(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();
    var abottom = $('#individual_second_tier').offset().top - parseFloat($('#individual_second_tier').css('margin-top').replace(/auto/, 0));

    // whether that's below the form
    if (y >= ctop) {
        // if so, ad the fixed class
        $('#comment').addClass('fixed');
        if (y > abottom-$('#comment').height()){
            $('#comment').offset({'top': abottom-$('#comment').height()-y});
        }
        else
        {
            $('#comment').offset({'top': 0 });
        }
    } else {
        // otherwise remove it
        $('#comment').removeClass('fixed');
    }
    var newWidth = $('#comment').parent().width();
    $('#comment').width(newWidth);

    });

You can see it in action here. It's the gray box on the right that says "Poll"

My site is responsive so when it goes under 768 pixels, the poll div moves down under the blog content. So in full browser width the script works great, but when I resize it the poll div goes haywire.

I am complete noob when it comes to jQuery -- I am an excellent copy-paster -- but I am imagining that there is a fancy way in the existing script to instruct it to disable it when it matches a media query or browser width, so that I can get rid of the fixed-floating-div functionality.

If anyone wants a local copy to mess with, here is a zip file with the html file (type will look off as I am using webfonts).

2
  • I wanted to mess around with your zip but... authentication required on the ftp. Commented May 30, 2013 at 4:46
  • @Esteban ah sorry about that. Forgot to change the path to be http:// instead of ftp:// — link updated in post. Commented May 30, 2013 at 11:34

6 Answers 6

1

Can you not just force the position of the poll to relative, rather than fixed at a designated screen size using media queries?

@media (min-width: 768) {
    #comment { 
        position: relative !important; 
        top: 0 !important; 
    }
}

Or, you could use jQuery within your function

if ($(window).width() > 768) { 
    $('#comment').removeClass('fixed');
}

enter image description here

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

Comments

1

A fellow without a Stack Overflow account saw my question and provided the answer by e-mail. It's a deceivingly simple solution. Simply adding to the existing code:

if ($(window).width() > 768) {

The final block of code looks like this:

$(document).ready(function() {
    ctop = $('#comment').offset().top - parseFloat($('#comment').css('margin-top').replace(/auto/, 0));
});
$(window).scroll(function (event) {

    if ($(window).width() > 768) {

        // what the y position of the scroll is
        var y = $(this).scrollTop();
        var abottom = $('#individual_second_tier').offset().top - parseFloat($('#individual_second_tier').css('margin-top').replace(/auto/, 0));

        // whether that's below the form
        if (y >= ctop) {
            // if so, ad the fixed class
            $('#comment').addClass('fixed');
            if (y > abottom-$('#comment').height()){
                $('#comment').offset({'top': abottom-$('#comment').height()-y});
            }
            else
            {
                $('#comment').offset({'top': 0 });
            }
        } else {
            // otherwise remove it
            $('#comment').removeClass('fixed');
        }
        var newWidth = $('#comment').parent().width();
        $('#comment').width(newWidth);
    }
});

I tested all the other answers provided here, but none worked the right way. Many thanks, though, for your help.

2 Comments

Don't forget to mark this as the accepted answer other people might have the same issue as you and will eventually look for questions with accepted answers.
Yes, indeed. Since I answered my own question, the system didn't let me check it as answered until 24 hours later.
0

I faced the same issue sometime ago and this is how I addressed it:

Set a media query to hide the element when needed:

/* Somewhere above... */
div#toHide { display: block; /* ... */ }

@media (min-width: 800) {
    div#toHide { display: none; }
}

Then in my .js file I've created a function like this:

function isDivVisible() {
    return $('div#toHide').is(':visible');
}

Now on my method which I didn't want to execute all it's code if the user won't be able to see it:

function someHugeFunction() {
    if(isDivVisible()) {
        // some crazy stuff here.
    }
}

I don't know if this is elegant or not, this worked for me.

2 Comments

If I understand the answer above correctly... It's hiding the DIV altogether. I still want to show the DIV but disable the script.
@Armin: you could change that easily, honestly, as I've said, I did research about this when I had the same issue. But I couldn't find out how to do it without javascript. If you want to change this behavior, just to be sure, would you like this to avoid hiding the div, and only stopping the script's loop?
0

Probably something like:

$(window).resize(function(){
  if( $(this).width() <= 768 ) {
    $('foo').removeClass('bar') //whatever css class is doing the undesired effect at breakpoint
  } 
});

Comments

0

matchMedia may be what you're looking for: http://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/

Add something like this outside your scroll function:

var isWide,
    width768Check = window.matchMedia("(min-width: 768px)");

width768Check.addListener(setWidthValue);
function setWidthValue (mediaQueryList) {
    isWide = width768Check.matches;
}
isWide = width768Check.matches;

$(window).resize(function(){
    if (isWide) {
        $('#comment').addClass('fixed');
    } else {
        $('#comment').removeClass('fixed');
    }
});

And then in your scroll

if (y >= ctop && isWide) {
...
}

Not perfect but does the trick. The resize() function will appropriately add/remove your fixed class when resizing the window.

Comments

0

jQuery: Disable script based on window width/media query Jquery for mobile view only

Add The Javascript

After you have added jQuery you can use the following code in your custom javascript file. You may change the "739" to a different number depending on the device resolution you are targeting.

if ( $(window).width() > 739) {      
  //Add your javascript for large screens here 
} 
else {
  //Add your javascript for small screens here 
}

The End

Yep, we're already done! You can now execute your javascript to target different screen sizes. Let me know if you have any questions or feedback below. Thanks!

Comments

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.