Paulo Capelo
Paulo Capelo

Reputation: 1092

executing script only when script is in viewport

How could I execute a JavaScript, on a page, only when the script itself it is in the browser viewport, meaning: imagine I have a script at the bottom of the page and I would like that script to load only when the user scroll the page all the way to the end.

I have found some stuff if you want to load images, but not for scripts:

http://luis-almeida.github.io/unveil/

Any Idea if that's possible at all?

cheers

Upvotes: 0

Views: 1724

Answers (1)

Zafar Ahmad
Zafar Ahmad

Reputation: 428

You can trigger scripts on the basis of HTML/DOM elements visiblity as scripts does not actually get into the viewport of the device. You can do something like:

<script type="text/javascript">
if($("element").scrollTop() == 0) { // or some other value, I am taking 0 which means when the element has 0px distance and is currently visible

// Do some action here

}
</script>

Upvotes: 0

Related Questions