$ (window) .load OR $ (window) .scroll in the same function?

I implemented stickyfloat (http://plugins.jquery.com/files/stickyfloat_0.htm) on the site. It works great with one of them. The function runs on $(window).scroll, not on $(window).load. I want this to work either because I link to the anchor points on the page (http://tre-stage.wdogsystems.com:8000/faq/#does-the-sale-of-my-receivables-have-a -negative-effect-on-my-credit-report), and I would like the side menu to appear when the page loads, and not just when the scroll starts.

If you look at the page above, it works the way I want it. However, this only happens because I repeated the same function with $(window).load. It seems to me very inefficient. So, is there a way to tie them together?

For instance:

$(window).scroll || $(window).load (function () ...
+3
source share
4 answers

The jQuery .bind() help method allows multiple events to be attached at once.

$(window).bind('scroll load', function() {
    // code here triggers for both, scroll & load events
});
+2
source

just a chain in bind, for example:

$(window).bind("scroll load", ...)

however a very bad idea to attach to a scroll event

very good explanation why and a great solution: http://ejohn.org/blog/learning-from-twitter/

+2
source

:

$(document).bind('ready load scroll', function() { ... });
+1

? , ...

$(window)
   .on('scroll.myscroll', function () {
      // do something on scroll event
   })
   .trigger('scroll.myscroll'); // trigger scroll event on pageload

But if you really want to run it when the window loads (assuming the DOM is fully loaded, including images, etc.), then the other examples mentioned in this document are great. But use the .on () method, not the .bind () method.

$(window).on('scroll.myscroll load.myload', function () {
   // do something on scroll and load events
});
0
source

Source: https://habr.com/ru/post/1788223/


All Articles