GithubHelp home page GithubHelp logo

thedutchcoder / jquery.scrollto.js Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 168 KB

A jQuery plugin that lets the author scroll to any object, from any trigger.

License: MIT License

JavaScript 100.00%

jquery.scrollto.js's Introduction

jQuery.scrollto.js

A jQuery plugin that lets the author scroll to any object, from any trigger.

Installation

Download the plugin file and put it wherever you like. Simply reference it in your HTML like so:

<script type="text/javascript" src="[path-to-your-scripts]/jQuery.scrollto.js"></script>

Markup (HTML)

The plugin requires only a trigger and a target.

Trigger

The trigger can be virtually anything, but I recommend some sort of navigational menu (like a ul element), or using something like a <button> element.

<!-- Menu example -->
<ul class="menu">
  <li><a href="#books">Books</a></li>
  <li><a href="#movies">Movies</a></li>
  <li><a href="#shows">Shows</a></li>
</ul>

<!-- Button example -->
<button type="button" class="js-scrollto">Scroll down</button>

Target

The target can be any valid jQuery selector or, if left blank, the plugin will try to use the trigger's anchor (E.g. <a href="#details">Details</a>).

<!-- Target example -->
<h1 id="books">Books</h1>
<p>A wide selection of reading materials.</p>

JavaScript

With the markup in place, you can call the plugin from within jQuery like so:

$(document).ready(function() {
    
    // This will use the anchor's href attribute to scroll to.
    $('.menu a').scrollto();

    // Or you can configure a custom trigger.
    $('.js-scrollto').scrollto({
      target: '#books'
    });
    
});

You probably want more control over this pugin, so here's a full list of options you can configure:

$('.menu').scrollto({
  trigger: 'a',
  target: '#books',
  namespace: 'my_namespace',
  speed: 500,
  preventDefault: false,
  stopPropagation: true
});

trigger

This is a (sub) element which will trigger the actual scrolling. So for example, if you attach this plugin to the entire document, you can then specify which element(s) should trigger scrolling behaviour.

// The plugin is attached to the menu, but the links within the menu are the
// actual triggers.
$('.menu').scrollto({
  trigger: 'a'
});

target

A custom target to scroll to. This will be used for all the elements that this plugin instance is bound to. So you probably want to use this on a single case basis.

// With a specific target, ALL links will go scroll to that target. This is
// something you probably don't want in most cases.
$('.menu a').scrollto({
  target: '#books'
});

namespace

All events in this plugin are handled in their own namespace. You don't have to do anything with this setting, but it can be useful for advanced users. The default namespace is jQuery_scrollto.

// This plugin uses a namepsace for events, so if you need to do something with
// the events in this plugin, you can interact with them through this namespace.
$('.menu a').scrollto({
  namespace: 'my_namespace'
});

speed

The speed at which to scroll to the target (in milliseconds).

// This will cause any scroll to take 500ms.
$('.menu a').scrollto({
  speed: 500
});

preventDefault

By default, this setting is enabled, in order to prevent links to jump to the anchor, instead of scrolling. You normally don't need to change this setting.

// This will enable the default behaviour of an element. You'd probably not use
// this in most cases.
$('.menu a').scrollto({
  preventDefault: false
});

stopPropagation

USE WITH CAUTION! Enabling this setting will cause all events to stop from bubbling up. Only use this setting if you're fully aware of the consequences (it's normally not needed).

// All event bubbling is stopped, if you really need it to.
$('.menu a').scrollto({
  stopPropagation: true
});

jquery.scrollto.js's People

Contributors

thedutchcoder avatar

Watchers

James Cloos 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.