Giter VIP home page Giter VIP logo

doormat's Introduction

NPM

Build Status img img styled with prettier

Doormat ๐Ÿ‘ž๐Ÿ‘ก

an alternative way to traverse through your site content ๐Ÿ˜Ž

alt tag

Index

How

The trick is possible by making sections of the page position: fixed and position: absolute whilst setting a height for the container element equal to the combined height of the page sections.

Page sections are given an appropriate top value that gives some buffer space for scrolling and give the parallax like illusion against whichever section is currently position: fixed.

Dependent on the panel mode, as the window is scrolled panels come into or move out of the viewport to either reveal or cover up fixed position content.

Features

  • Provides alternative way to traverse content
  • Configurable behavior
  • Inbound and outbound traversal modes
  • Panel snapping to either viewport or as a means of traversing panels
  • API for programmatically traversing content
  • Responsive
  • Animated scrolling
  • Simple to create custom effects/behavior with CSS with many possibilities.
  • Supports overflowing content in outbound mode(refer to caveats๐Ÿ˜…)
  • No dependencies! ๐ŸŽ‰
  • Scroll/Resize optimization via requestAnimationFrame

alt tag

Browser support

Chrome Firefox Safari Opera Edge IE
๐Ÿ˜„ ๐Ÿ˜„ ๐Ÿ˜„ ๐Ÿ˜„ ๐Ÿ˜„ ๐Ÿ˜„

v5 makes use of requestAnimationFrame, viewport units and the Event API. Most browsers should play nice except Opera Mini ๐Ÿ‘Ž. IE support should be fine in version 11.

Usage

To create your doormat.

  1. Include doormat{.min}.js and doormat{.min}.css in your page/bundle.

  2. Create your DOM/Page structure. The containing element needs the classname dm. This could be body. doormat uses panels for each section of content. Therefore, the children of our container need the class dm-pnl. ol and ul make good container elements ๐Ÿ˜‰ A structure like the following is ideal.

  <body>
    <ol class="dm">
      <li class="dm__pnl">Awesome</li>
      <li class="dm__pnl">Site</li>
      <li class="dm__pnl">Content</li>
    </ol>
    <script src="doormat.min.js"/>
  </body>
  1. Invoke the Doormat function passing in desired options(explained below) as a parameter;
document.addEventListener('DOMContentLoaded', function() {
  var myDoormat = new Doormat();
});

Options

  • {number} snapDebounce - defaults to 150: No need to trigger snapping behavior on every scroll so snapDebounce defines a debouncing duration in ms.
  • {number} scrollDuration - defaults to 250: Defines the default duration in ms that a panel takes to snap to place or the duration of a programmatic doormat scroll that isn't overridden with the optional parameter.
  • {string} snapMode - defaults to 'viewport': Defines the "snap" style of Doormat panels. Whether it be viewport style, travel style or disabled with false. Setting to false will mean that users can scroll content so that it doesn't sit flush in the viewport.
  • {string} mode - defaults to 'outbound': Defines the traversal style. outbound will make panels slide out to the top of the viewport revealing content underneath. inbound will make panels slide in from the bottom covering content.
  • {number} snapThreshold - defaults to 30: Define the percentage of the viewport that will trigger a "snap". For example, you start scrolling a panel but no more than 30% of the viewport height. You stop scrolling and because snapMode is set to viewport, the panel scrolls back to fit flush with the viewport again ๐Ÿ‘

alt tag

API

  • {number} activeIndex: Getter for current active panel index. Useful for conditionals where content should only appear at certain times etc.
  • {Object} active: Getter for current active panel element.
  • next: Function to scroll to next panel.
  • prev: Function to scroll to previous panel.
  • {Array} panels: Getter for panels collection.
  • reset: Function to reset Doormat instance. Used on viewport changes. Will instantly scroll to top with no animation and set first panel as active.
  • {number} scrollPos: Getter for current scroll position of instance.
  • scrollTo({number} scrollPosition, ?{number} speed): Function for scrolling to a specific position within instance. Accepts optional speed parameter in ms.
  • scrollToPanel({number} panelIndex, ?{number} speed): Function for scrolling to a specific panel. Accepts optional speed parameter in ms.

alt tag

Events

Doormat offers an even that can be bound to for observing when a new panel becomes active. The event is doormat:update. Consider the following example that can be used for customisation to show controls once scrolled past the first panel ๐Ÿ˜Ž

window.addEventListener('doormat:update', (e) => {
  if (doormatInstance.activeIndex > 1)
    document.body.classList.add('show-controls')
  else
    document.body.classList.remove('show-controls')
})

curtain.js?

Unfortunately, curtain.js is no longer maintained and there were reports of issues with newer versions of jQuery.

curtain.js is a more feature rich solution and provides some different behavior and hooks. It does however depend on jQuery.

I was unaware of curtain.js when creating Doormat. Initially, I was experimenting with different implementations of parallax style effects and messing about with viewport units.

My aim with Doormat was to create the effect but keep the solution minimal with no dependencies. It was a result of curiosity and playing with code.

Customisation

UI Effects

One feature I like with Doormat is the ability to create custom effects with relative ease using CSS or conditionally based on the current active panel index of a Doormat instance.

alt tag

For example, the effects in the above GIF are created by leveraging CSS transitions based on the current active panel of an instance. The important parts are;

.dm-panel__content {
  opacity: 0;
  transform: scale(0);
  transition: opacity .25s ease 0s, transform .25s ease 0s;
}

.dm-panel--active .dm-panel__content {
  opacity: 1;
  transform: scale(1);
}

Caveats

  • If you use inbound mode, content larger than the viewport will get cut off as opposed to when using outbound mode. Solution? Make the content of the active panel scrollable ๐Ÿค“
  .dm-panel--active .dm-panel__content {
    overflow: auto;
  }

Roll your own classes

The classes for Doormat are config driven with doormat.config.json. In here you can alter the classnames for elements to your desire.

Important:: If you change the classnames in the config, you will also need to update any reference to them from within the JavaScript source. For example, by altering the classes property on the Doormat class.

Change the config and then run the build task with make build.

Development

Want to play with the code?

  1. Fork and clone or simply clone the repo direct.

     git clone https://github.com/jh3y/doormat.git
    
  2. Navigate into the repo and run the setup task;

     cd doormat
     make setup
    
  3. Run the develop task to get up and running with browser-sync etc.

     make develop
    

NOTE:: See all available build tasks by simply running make in the root of the directory ๐Ÿ‘

  make

alt tag

Contributing

Don't hesitate to post an issue or suggestion or get in touch by tweeting me @_jh3y!

License

MIT

jh3y 2017


Images courtesy of Unsplash ๐Ÿฑ

doormat's People

Contributors

jh3y avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

doormat's Issues

Behaves oddly when panels are bigger than viewport height

Panels don't always behave as intended when they are bigger than the viewport height.

This isn't ideal and it seems that to counteract the issue, there are several complicated calculations having to be made on scroll/snap.

Look to rework the current implementation so that the maximum size of a panel is the viewport height and the inner content is overflow:auto when appropriate.

This should simplify the script and reduce it's size.

@jh3y

Programmatic way to go to a slide?

Hi,

Thanks for all your help before, the snapping is working very well. I am wondering if there is a way to go programmatically to a slide, if I call a JS event (preferable) or call going to an anchor (also cool). I.e. to make a table of contents that can be clicked to bring you to a slide. Thank you!

Snap travel

Discussed in #15, it would be good to implement some form of "snap travel".

The believed behavior to be as follows;

  • Given a set threshold, when scrolling past this threshold, snap to the next panel.
  • Given a set threshold, if a previous panel is scrolled into view over defined threshold, snap that panel into view.

@jh3y

Deprecate CoffeeScript use in favor of Babel

Look to swap out the current build process and use of CoffeeScript.

Opt to move over to Make and the use of Babel.

CoffeeScript is hard to return to when you haven't been using it regularly... ๐Ÿ˜ข

@jh3y

Content flash on load

On first load of a page. You see content flash before the script alters classes of the elements.

The solution here is most likely to create the correct classes when generating markup to use. Will refactor to work in this way.

@jh3y

Script not working after update

Hi,
Thanks for all the awesome work with snapping. However I have some implementation problem. Here is the index.html I have which worked fine at commit 9244c92

https://gist.github.com/HongPong/00afde69a2340bdc7e04549e01ccbef4

When I update to commit 8bc1b2b it doesn't do anything and I don't get any errors in the console.

I also tried doing : make setup; make build; make compile-scripts
which did not have an effect. I'm heading home but can provide any other needed info in the AM to reproduce if needed. Thanks again for your close attention, I really appreciate it.

Scrollable Li

Hi,

I was wondering if it's possible to make the last li content scrollable. I made a new class which contains the same as the doormat panel but only with a relative position. But the problem is that the margin-bottom creates a huge gap..

Thank you.

Fix screen jumping and scroll reset for mobile

On mobile devices or devices that support orientation change where a search bar may trigger a resize event on window, use onorientationchange instead of onresize.

In addition, use screen.height instead of window.innerHeight or window.outerHeight as these do not take into consideration the moving search bar.

@jh3y

Are there any good integrations for snapping?

I would like to be able to give a kind of snap behavior between sections, so they kind of lock in place and jump to the next one while scrolling. Is there an easy way to integrate this with another library that provides more of a snapping effect?

Fix snap travelling behavior

"snap travel" is buggy when scrolling up through previous slides. This is possible just requires sitting down with a ๐Ÿ“ and working out all of the necessary logic to trigger the right action points.

@jh3y

index.html reference to the css/flat-ui.css

The reference to the css/flat-ui.css needs a tweak so the demo will work when placed in a sub-folder of a website:

actual: <link  rel="stylesheet" href="/css/flat-ui.css" />
fix:    <link  rel="stylesheet" href="css/flat-ui.css" />

Also the image - doormat.jpg referenced in style.css was not present. Which is fine. It is almost 400kb so I understand not including it. I would suggest adding a color to the first panel so the demo shows more completely by eliminating the transparency:

.header-panel {
    background-image: url('images/doormat.jpg');
    background-color: #ddd;
    background-size: cover;
    background-position: center center;
}

If the image is not there no issues with the demo display.

Change number of panels

How would I change the number for panels from the default 10 to say, 20?
I thought this would do it but no dice.

$(document).ready ( function(){
var myDoormat = new Doormat();
"panels": 10
});

Thanks.

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.