Grow your CSS skills. Land your dream job.

Script not disabling jQuery for Mobile Devices

  • # January 9, 2013 at 12:45 pm

    I am wanting to disable an Overlay Slider script that I downloaded from ThumbsFX for mobile devices (smaller than 768px). I tried the following script but it’s not disabling.

    $(document).ready(function() {
    if ( $(window).width() > 767) {
    $(‘.code-trigger’).click(function(){
    $(this).next().slideToggle();
    });
    }

    });

    # January 9, 2013 at 1:11 pm

    Can you create a [codepen](http://codepen.io/pen)

    # January 9, 2013 at 1:39 pm

    Here’s the link to the codepen…however I am not sure if it will help because I couldn’t see where to upload the image.

    http://codepen.io/anon/full/xmfbj

    # January 9, 2013 at 1:53 pm

    Here’s the link to the site so you can see this in action:

    debbierking.com/bootstrap

    # January 9, 2013 at 2:30 pm

    I’m not entirely sure what I’m looking at here. As far as I can tell, the class used in the quoted script is not present on the page, so I’m not sure what the script is supposed to be controlling, exactly?

    Also, are you wanting this to work on window resize, or only on page load?

    # January 9, 2013 at 2:45 pm

    Josh,

    I was using the script from their demo and then adding a window resize option. Since the overlay function worked, I figured that their plugin .js file (jQuery.thumbFX.js) referred to .code-trigger in some way.

    But I have changed that now to .overlayer .overlay to refer directly to the HTML and CSS…but am still having the same problem.

    And yes, I do want it this to be disabled on both window resize and loading in mobile devices.

    $(document).ready(function() {
    if ( $(window).width() > 767) {
    $(‘.overlayer .overlay’).click(function(){
    $(this).next().slideToggle();
    });
    }

    });

    # January 9, 2013 at 3:14 pm

    Okay, the first problem I’m seeing is that the script you’re basing this on has absolutely no relation to the plugin. That bit of code on their demo page allows an h3 below an example under the “Advanced Examples” section to, on click, open a small code preview.

    I’m not positive how to do this, as the script doesn’t need initialization from the HTML page, per se. You _can_ initialize it, but it effectively seems to just serve to set the various options. Plus, you’d need a way to **un**-initialize it if the window resized smaller.

    Here’s a basic example of what you need to do to get resizing working in the first place, but there’s no guarantee this is going to do what you want it to:

    var $winWidth = {
    width: null
    };

    $(function () {

    $(window).bind( “resize”, function ( event ) {
    var $this = $(this);
    $.each( $winWidth, function (key) {
    $winWidth[ key ] = $this[ key ]();
    });
    if ( $winWidth.width > 767 ) {
    $(‘[data-overlayer]’).overlayer();
    } else {
    // May need a method to disable/unitialize the script?
    }
    });

    });

    A Codepen to demonstrate basically how the code is supposed to work, with live width reading: http://codepen.io/JoshBlackwood/pen/DvuKq

    # January 9, 2013 at 3:45 pm

    Oops, nevermind. I’ll edit this post when I get results.

    # January 9, 2013 at 4:03 pm

    Thanks Josh for all your efforts on this. I tried both of these but neither work… :(

    # January 9, 2013 at 4:09 pm

    And thanks for the code pen …that’s awesome! Will be very helpful.

    # January 9, 2013 at 4:29 pm

    Aha! I had it backwards. It’s the class that activates the overlay, not the attribute. Can you try this?

    var $winWidth = {
    width: $(window).width()
    };

    function removeOverlay() {
    if ( $winWidth.width < 768 ) {
    $(‘a[data-overlayer]’).removeClass(‘overlayer’);
    } else {
    $(‘a[data-overlayer]’).addClass(‘overlayer’);
    }
    };

    $(function () {

    $(window).bind( “resize”, function ( event ) {
    var $this = $(this);
    $.each( $winWidth, function (key) {
    $winWidth[ key ] = $this[ key ]();
    });
    removeOverlay();
    });

    removeOverlay();

    });

    I consolidated a few things down, made a new function, and tidied up all ’round. Also fixed it so that it’ll work on page resize AND on page load/reload. You can see the CodePen for an example of how it dynamically adds/removes the class on resize (open Developer Tools and watch the p tag): http://codepen.io/JoshBlackwood/pen/DvuKq

    # January 9, 2013 at 7:30 pm

    I’m afraid that didn’t work either :( I’m trying to get it so that anything smaller than an ipad will display the text automatically, rather having it slide up only on hover.

    # January 10, 2013 at 10:18 am

    This is the problem:

    $function removeOverlay() {

    Should be:

    function removeOverlay() {

    This is line 330 of your source code.

    >I’m trying to get it so that anything smaller than an ipad will display the text automatically, rather having it slide up only on hover.

    Additionally, I really wish you’d made that goal more clear early on . . . that’s a whole ‘nother animal, honestly. You could probably go about it in roughly the same way, but you’ll have to look closely at how the plugin shows/hides the overlay. It’s not as simple as setting the display property, as it involves positioning.

    # January 10, 2013 at 11:37 am

    The width thing just isn’t working for some reason, either on this script or the overlay script. I tried a variation of what you wrote here yesterday and it didn’t work either…Now no padding is added even for > 980.

    # January 10, 2013 at 11:59 am

    ooops that comment was meant to be on the other post. I’m sorry I about not making my goal more clear. I made the fix but the sliding is still happening at < 767.

Viewing 15 posts - 1 through 15 (of 24 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".