Code Monkey home page Code Monkey logo

angular-scrollevents's Introduction

Scroll event directive for Angular.js

At Symphonical.com, we develop a mobile webapp in Angular.js. And we saw the need of having an event for when the scrolling ends. When the user scrolls within our viewport we update a header-bar, and could easily see that the default scroll event used too much resources. And therefor we built this Directive to update the header after the scrolling is done.

How it works

<div ng-scroll-events="updateOnScrollEvents($event, isEndEvent)" class="my-uber-design">
  This area is scrollable
</div>

The ng-scroll-events is the Directive name and calls the scope-defined function updateOnScrollEvents($event, isEndEvent). The function is called with two arguments, the default $event and isEndEvent(true/false). If isEndEvent is false, then it's the scroll-start event. If false it's scroll-end event.

During testing we saw that calling the scroll event and do calculations there was heavy, especially if there was a lot of DOM elements with some expensive CSS. So we unbind the scroll event and start an Interval to see if the scroll position is different from the thre initial scroll or the last Interval tick. If so, we call the ng-scroll-events with isEndEvent = true. The Interval runs every 150ms in our code, but you can easily change this if you want. But this was a good number for our usage.

Working example

You can find a working example here: http://jsbin.com/uropuq/3/

Todo:

  • Write test

angular-scrollevents's People

Contributors

madd avatar

Watchers

James Cloos avatar WÁNG Xuěruì avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.