I have following code in my Wordpress:
(function ($) {
var $header = $("div.header");
$(window).bind("scroll resize", function () {
if ($(window).scrollTop() > 30) {
$("div.header").stop().animate({
'opacity': 0.24
}, {
duration: 1000
});
} else {
$header.stop().animate({
'opacity': 1
}, {
duration: 1000
});
}
});
})(jQuery);
If statement kicks in when supposed but else never...
BUT
If I enclose it with:
jQuery(document).ready(function($) {
// code here
});
It's all fine. Why?
Thank you
Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.
Naming variables Variable names are pretty flexible as long as you follow a few rules: Start them with a letter, underscore _, or dollar sign $. After the first letter, you can use numbers, as well as letters, underscores, or dollar signs. Don't use any of JavaScript's reserved keywords.
The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an IdentifierName. As such, the $ sign may now be used freely in variable names.
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.
JavaScript Dollar Sign Variable When using jQuery, developers usually prefer to use a variable name with a dollar sign. It’s a personal practice to identify that it contains a jQuery object. Here is a code example of jQuery with a dollar sign variable.
Dollar Sign in Template Literals In JavaScript we use $ sign and then { } to embed expression in string. It allows multi-line string and string interpolation features. Example 1: To Embed and evaluate an arithmetic express inside a string.
The dollar function is a function that takes in some sort of identifier for an HTML element or elements and returns a jQuery object. It’s the main function used by jQuery that’s the basis for the whole framework.
That also means that creating your own dollar sign function that does something different is probably not a good idea. In case you were wondering, the underscore has no special properties either. But let’s face it, $ (‘myVal’) looks more like programming than _ (‘myVal’).
May be you're trying to use jQuery when dom in not build. Try to use $(document).ready
function:
(function ($) {
$(document).ready(function () {
$header = $("div.header");
$header.remove();
});
})(jQuery);
About what you have mantioned in the question:
jQuery(document).ready(function ($) {
// code
});
It works because it do the same thing: it binds event handler on ready
event and pass jQuery
object as a parameter to the function as $
.
Now what you did before:
(function ($) {
$header = $("div.header");
$header.remove();
})(jQuery);
Here you just declare anonymous function with named $
parameter:
function ($) {
}
And call it with jQuery
object as a parameter, which will be available in the function as $
:
(function ($) {
})(jQuery);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With