Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More efficient scroll function #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JangoSteve
Copy link

I just tried using this plugin for the first time, so I don't know the ins and outs necessarily. But the scroll event triggers a lot when scrolling, and in this case the detectTarget function that is bound to the scroll event is somewhat inefficient.

Without actually changing anything in the way the plugin or that function work, we can make it much more efficient by using plain JavaScript where possible.

In this case, jQuery is definitely not needed to get an element's id. Also, as far as I can tell scope will always be a jQuery object already because the plugin is called via the jQuery selector (e.g. $('#my-element').endlessScroll().

So, 1) we don't need to re-wrap $(this.target), since this.target is always a jQuery object already (again, I could be wrong here, I don't know the ins and outs of the plugin), and 2) we can get the plain JS dom element using .get(0) and then just call the plain JS id property on it; much more efficient.

Ideally though, this function should probably just return true if this.target and this.targetId are already present, but that actually changes the way the function works and could mess up on pages that are constantly having their DOM updated and changed.

I just tried using this plugin for the first time, so I don't know the ins and outs necessarily. But the `scroll` event triggers a *lot* when scrolling, and in this case the `detectTarget` function that is bound to the `scroll` event is somewhat inefficient.

Without actually changing anything in the way the plugin or that function work, we can make it much more efficient by using plain JavaScript where possible.

In this case, jQuery is definitely not needed to get an element's `id`. Also, as far as I can tell `scope` will always be a jQuery object already because the plugin is called via the jQuery selector (e.g. `$('#my-element').endlessScroll()`.

So, 1) we don't need to re-wrap `$(this.target)`, since `this.target` is always a jQuery object already (again, I could be wrong here, I don't know the ins and outs of the plugin), and 2) we can get the plain JS dom element using `.get(0)` and then just call the plain JS `id` property on it; much more efficient.

Ideally though, this function should probably just return true if `this.target` and `this.targetId` are already present, but that actually changes the way the function works and could mess up on pages that are constantly having their DOM updated and changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant