Show HN: In-view.js – Get notified when DOM elements enter or exit the viewport
github.com
github.com
This is a really useful problem to solve. But, I would personally prefer to solve it with a polyfill for a standardized approach that will eventually receive native implementation.
The advantages of standardization and eventual native implementation outweigh the immediacy of a JS-based API designed in relative isolation. Why should I invest time learning this micro-library, when the API is certain to be different from the native implementation? The documentation doesn't seem to even acknowledge the existance of the standard (did the author do any research before implementing a one-off library? I have no way of knowing.), much less explain why it differs from the proposed standard.
Intersection Observer has been implemented in Chrome since 51, and Opera since 38. It's currently being implemented in Firefox ( https://bugzilla.mozilla.org/show_bug.cgi?id=1243846 ), and is "likely" from Edge (https://developer.microsoft.com/en-us/microsoft-edge/platfor... ).
in-view.js's API is certainly less likely to be the cross-platform API than Intersection Observer. Also, plenty of APIs are implemented by browsers at the editor's draft stage.
A more comprehensive list is here[1], but the main culprits are HTMLElement.offset{Width,Height}, Element.client{Width,Height}, and Window.getComputedStyle(). Avoid these, _especially_ on events like scroll. You will ruin the experience for many of your users.
some advertising libraries solve that, since the advertising industry deals in in-view ad impressions for a few years now, by being smart about when to do it. the best one is called safeFrames. others like moat detect if the user has a fast computer, abuse it to no end, and then extrapolate the results for the whole audience.
i expect all of them to move to the native APIs soon (but Android fragmentation will make it slow)
More than three years ago I created in-viewport and have perfected it since. https://github.com/vvo/in-viewport it's used on fortune 500 websites along with my lazyloader (https://github.com/vvo/lazyload).
Conversations about what's an app and what's a document aside, I'd much rather documents didn't know anything about how they were being displayed.
https://webmasters.googleblog.com/2016/08/helping-users-easi...
"To improve the mobile search experience, after January 10, 2017, pages where content is not easily accessible to a user on the transition from the mobile search results may not rank as highly."
They define what is acceptable and not right after. I like the move though !
Even if I was the odd one and nobody else would mind those pop-ups: In the context of multiple experiments showing that a few hundred milliseconds additional page load time have a very real impact on engagement rates it stands to reason that interrupting the user experience with unrelated email sign ups should have a similar effect.
I am using onScreen which I found more efficient than alternative solutions.
I've had projects that got really strange error messages when using rollup that completely went away when I switched back to Babel.
One "issue" with rollup is that it is not 100% semantically correct. Neither is Babel in all cases, but the creator argues that if you're not following exact semantics now, you're relying on your tests to ensure proper behavior, so use something that is at least more minimal and keeps your bundle size down.
So when you're writing code and bundling with rollup, you can pretty much ensure everything works fine, but as soon as you pull in an extensive third-party library you have no assurances that it has been tested with rollup and will work correctly in all cases. In the worst case, it will seem to work fine but in weird situations will actually error out. This was my experience with rollup.
kudos for in-view's demo page though
Does Rollup produce a more efficient bundle in your experience?
import throttle from 'lodash/throttle';
rather than import { throttle } from 'lodash';You can generally get smaller bundles using `lodash/xyz` modules over the `lodash.xyz` packages because of plugins like:
https://github.com/lodash/babel-plugin-lodash
https://github.com/lodash/lodash-webpack-plugin
Though in the future lodash-webpack-plugin may support `lodash.xyz` packages too.
Minified (1.49kb) and gzipped (677b).
Whether lodash functions should be used instead of naive functions is up for debate. My opinion is if the lodash functions are 5x the size of the entire library, it's probably best to not include it, or to include it as a build option (if the user's project already includes lodash for example).
Assuming the current setup of babel+webpack the difference between your naive version is just ~0.5 kB.
Or Bublé[1] ;) which i've found to be much faster than Babel, even for relatively small codebases (~ a couple thousand LoC).
Most of the code here is actually from lodash due to the use of `throttle`, not either of the projects you mentioned.
The #1 killer he found responsible for "jankiness" (that horrible scroll feeling where the page scroll is unresponsive and not 60 fps smooth) is by people attaching event handlers on the scroll event that take more than a few ms to complete, thus reducing the frame rate of the page.
It seems like this library is throttling the evaluation of the event handler but you still need to be careful as to not solve one performance problem and create another. I know I personally hate slow scrolling web pages.
This seems like something that should be implemented in some as yet unreleased CSS selector. But that still won't stop people from abusing it.
This isn't true no. Scrolling is an asynchronous event, in this case meaning it will happen regardless (without waiting on any JS event handlers), the JS event triggers after its happened. This is the reason you can't prevent scrolling from javascript.
Unlike clicking links, or submitting a form, the browser doesn't wait for any JS event handlers to return before doing this action. This was conscious decision to stop scrolling becoming too heavy or slow, or worse...crashing the page.
Some here have mentioned the webpack overhead, and suggested rollup.js. I've had great results with rollup for these teeny-tiny browser-focused projects.
And I don't want to spend my time learning to write it correctly to handle all case and be performant.
https://github.com/camwiegert/in-view/blob/master/src/in-vie...
Note: this is definitely not the same as a recycled view. But can help accomplish one aspect
Echo.js (https://github.com/toddmotto/echo) is another small no-dependency library (1.89 KB minified), which detects elements appearing on screen, and goes a step further to swap the "src" attribute with a placeholder for lazy loading and unloading.
Disclosure: I've submitted a pull request to it.