0

Hi I would like to know how I can make my page scroll when arrow keys are pressed. Like this page: http://spacexstats.xyz. Java script and jQuery are accepted.

Thanks, Ivan.

  • 1
    By default up and down keys are binded to scroll of the page. You don't need to setup anything. – Praveen Govind Jul 18 '17 at 12:20
  • 1
    Possible duplicate of [Smooth scrolling when clicking an anchor link](https://stackoverflow.com/questions/7717527/smooth-scrolling-when-clicking-an-anchor-link) – Daniel Beck Jul 18 '17 at 12:24
  • (That's a "close-enough" duplicate -- you'd fire it on catching key codes instead of a click event, but that's simple enough) – Daniel Beck Jul 18 '17 at 12:25

2 Answers2

0

Each and every page will could be scrolled with arrow up/down keys by default.

If you are talking about smooth custom scroll - the page you asking is using Angular scroll plugin

There are many libs and plugins which can help achieve similar user experience, consider using google for smooth scroll [on arrow keys]

Example libraries:

Mihey Egoroff
  • 1,542
  • 14
  • 23
0

This is an angularJS application. They are using angular-scroll (https://github.com/oblador/angular-scroll) library to smoothly scroll to elements on the page. Here is a small angular application that demonstrates smooth scrolling to multiple elements on the page.

(function() {
  var app = angular.module("soDemo", ['duScroll']);
  app.controller("soDemoController", SoDemoController);

  SoDemoController.$inject = ['$scope', '$document'];

  function SoDemoController($scope, $document) {
    var vm = {
      getContent: getContent,
      scrollTo: scrollTo
    };

    $scope.vm = vm;
    return;

    ////////// IMPLEMENATION ////////
    function getContent(size) {
      return 'blah '.repeat(size);
    }

    function scrollTo(selector) {
      // Note: normally we don't want to interact with the DOM in a controller. A directive is the proper way to do this but for this example we'll let it slide.
      var target = $(selector)[0];
      $document.scrollToElementAnimated(target, 0, 1000);
    }
  }
})();
a {
  color: blue;
  text-decoration: underline;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-scroll/1.0.2/angular-scroll.js"></script>

<div class="sample" ng-app="soDemo" ng-controller="soDemoController">
  <h1 id="title1">Title 1</h1>
  <a ng-click="vm.scrollTo('#title2')">Go to Title 2</a>
  <p>{{vm.getContent(1000)}}</p>

  <h1 id="title2">Title 2</h1>
  <a ng-click="vm.scrollTo('#title3')">Go to Title 3</a>
  <p>{{vm.getContent(1000)}}</p>

  <h1 id="title3">Title 3</h1>
  <p>{{vm.getContent(100)}}</p>
  <a ng-click="vm.scrollTo('.sample')">Go to Top</a>
</div>
flyer
  • 1,414
  • 12
  • 13
  • Why the downvote? This is, from a high level, how they accomplished scrolling on that website. I didn't say that learning how to do this from scratch would be easy. – flyer Jul 18 '17 at 12:27
  • 1
    See comments on Mihey Egoroff's answer (in addition, this answer was redundant to his). Also: https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Daniel Beck Jul 18 '17 at 12:33
  • oh! I totally didn't get that. I've updated my answer with a demonstration of how to use their library. Thanks for taking the time to fill me in!! – flyer Jul 18 '17 at 13:07