Coder Social home page Coder Social logo

elm-scroll's Introduction

Elm Scroll

An Elm Library for scrolling through a page and handling events. Meant to be used alongside StartApp.

Usage

Installation

$ elm package install abrykajlo/elm-scroll

Examples

$ git clone https://github.com/abrykajlo/elm-scroll
$ cd elm-scroll/examples
$ make elm

Open index.html in your browser.

Breaking down the example

First of all, the module is relying on the window.onscroll event, thus a port is needed:

index.html

<!DOCTYPE html>
<html>
<head>
	<title>Adam Brykajlo</title>
	<link rel="stylesheet" type="text/css" href="css/main.css">
	<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div id="main"></div>
<script>
	var scroll = window.pageYOffset || document.body.scrollTop;
	var mountNode = document.getElementById('main');
	var myApp = Elm.Main.embed(mountNode);

	window.onscroll = function() {
		var newScroll = window.pageYOffset || document.body.scrollTop;
		myApp.ports.scroll.send([scroll, newScroll]);
		scroll = newScroll;
	};
</script>
</body>
</html>

Elm files

Create a separate file for the ports:

-- Ports.elm
port module Ports exposing (..)

import Scroll exposing (Move)

port scroll : (Move -> msg) -> Sub msg

In your main file:

-- app definition
main =
    App.program
        { init = init
        , subscriptions = subscriptions
        , update = update
        , view = view
        }

Need to define at least two type of actions, one for the incoming port's messages and one for the animation frame:

type Action
    = Header Move             -- the message emitted by the input port
                              -- brings a tuple with previous and current scroll values
    | Shrink                  -- message to be sent when scrollTop < 400px
    | Grow                    -- message to be sent when scrollTop > 400px
    | Animate Animation.Msg   -- animation's tick

For the port to be handled, a new subscriptions has to be created:

subscriptions : Model -> Sub Action
subscriptions model =
    Sub.batch
        [ scroll Header
        , Animation.subscription Animate [ model.style ]
        ]

Finally, we need to handle properly all the messages:

update action model =
    case action of
        Grow ->
	    -- scrolling down, under 400px
	    -- ...
	    (model, Cmd.none)
        Shrink ->
	    -- scrolling up, over 400px
	    -- ...
	    (model, Cmd.none)
        Animate animMsg ->
	    -- here you can apply new styles, to be animated
	    --- ...
	    (model, Cmd.none)
        Header move ->
            Scroll.handle
                [ update Grow                   -- when scrollTop > 400px, send Grow message
                    |> Scroll.onCrossDown 400
                , update Shrink                 -- when scrollTop < 400px, send Shrink message
                    |> Scroll.onCrossUp 400
                ]
                move model

Enjoy!

elm-scroll's People

Contributors

abrykajlo avatar bitterjug avatar pietro909 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

elm-scroll's Issues

Update for Elm v0.17

Elm has recently been updated for Elm 0.17 and thus I have plans on updating this library for the new version

Update to Elm 0.18

Currently this library is grayed out in the package website due to being a little out of date.

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.