-1

I've got a jQuery plugin scroll effect on my website, which I would like to turn off for screen sizes under 740px. I found a post that was similar to mine and tried what they suggested there, but all it does is turing off my plugin completely, no matter what screen size. I'm not sure which part of the script is malfunctioning.

Here's the script:

if(screen.width < 740) {
    return;
} else {
    $(function() {
        $.scrollify({
            section : "section",
        });
    });

    $.scrollify({
        section : "section",
        sectionName : "section-name",
        easing: "easeOutExpo",
        scrollSpeed: 1100,
        offset : 0,
        scrollbars: true,
        before:function() {},
        after:function() {},
        afterResize:function() {}
    });
}

Does anyone have an idea what's wrong with this?

Thanks, Lisa

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Lisa
  • 25
  • 1
  • 6
  • 3
    it's actually a syntax error for having a `return;` statement without wrapping it inside a function, check your console.log after loading the website – Icepickle Aug 27 '15 at 08:47
  • 1
    I'm getting really tired of people posting Javascript problems without first checking the console for errors. – Barmar Aug 27 '15 at 08:49
  • @Barmar At the end of the day, if the user poster is a *complete* beginner, at least you've taught them something – jonny Aug 27 '15 at 08:50
  • I'm sorry, I'm very new to this. – Lisa Aug 27 '15 at 08:51
  • 1
    I may say you will also have a problem when facing the case user resize manually the window. You should hear for the [.resize()](https://api.jquery.com/resize/) event handler instead. – Anwar Aug 27 '15 at 08:51
  • I've taken out the return statement, now the plugin is working again but it's not turning off on screen sizes under 740px. – Lisa Aug 27 '15 at 08:54

3 Answers3

3

Why you call $.scrollify function two times? By the way, can you try this one?

$(function() {
    if($(screen).width() >= 740){
        $.scrollify({
            section : "section",
            sectionName : "section-name",
            easing: "easeOutExpo",
            scrollSpeed: 1100,
            offset : 0,
            scrollbars: true,
            before:function() {},
            after:function() {},
            afterResize:function() {}
        });
    }
});
Daniele
  • 161
  • 5
1

Remove the return that's a syntax error and check for width >= 740 only:

if(screen.width >= 740) {
    $(function() {
        $.scrollify({
            section : "section",
        });
    });

    $.scrollify({
        section : "section",
        sectionName : "section-name",
        easing: "easeOutExpo",
        scrollSpeed: 1100,
        offset : 0,
        scrollbars: true,
        before:function() {},
        after:function() {},
        afterResize:function() {}
    });
}
michelem
  • 14,430
  • 5
  • 50
  • 66
0

You should wrap your whole code into a function to make the return; statement work. For example you could make your code be executed when your document is ready:

<script>

$(document).ready(function(){
    if(screen.width < 740) {
        return;
    } else {
        $.scrollify({
            section : "section",
        });
        $.scrollify({
            section : "section",
            sectionName : "section-name",
            easing: "easeOutExpo",
            scrollSpeed: 1100,
            offset : 0,
            scrollbars: true,
            before:function() {},
            after:function() {},
            afterResize:function() {}
        });
    }
});

</script>
Daniel
  • 3,541
  • 3
  • 33
  • 46