stkvtflw
stkvtflw

Reputation: 13587

jQuery calling function on page load

I'm trying to execute this function:

    function scrollDesign () {
    $( window ).scroll(function() {
          $('html, body').animate({ scrollTop: $('#design').offset().top }, 1000);
        });
    };

in this way:

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

but nothing happens.

This function separately works well:

        $( window ).scroll(function() {
          $('html, body').animate({ scrollTop: $('#design').offset().top }, 1000);
        });

Where is my mistake?

Upvotes: 1

Views: 78

Answers (2)

Michas
Michas

Reputation: 9468

The standard way to call a function in JavaScript (and many other languages):

function_name()
function_name(argument)
function_name(argument, second_argument)
function_name(argument, second_argument, third_argument)
// And so on...

The documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

The $() is a short form for jQuery(). (The $ is a common letter in JavaScript.) This function may produce very different results based on arguments.

The form $(function_name) is a short for
$(document).ready(function_name)
or
jQuery(document).ready(function_name).

The documentation:
http://api.jquery.com/jQuery/

Upvotes: 0

garryp
garryp

Reputation: 5776

Change this

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

to this:

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

This first code sample is not doing anything other than wrapping a function in a jQuery selector. It is not invoking it.

Upvotes: 2

Related Questions