Code Monkey home page Code Monkey logo

smooth-scroll's Introduction

DEPRECATION NOTICE:

Smooth Scroll is, without a doubt, my most popular and widely used plugin.

But in the time since I created it, a CSS-only method for smooth scrolling has emerged, and now has fantastic browser support. It can do things this plugin can't (like scrolling to anchor links from another page), and addresses bugs and limitations in the plugin that I have never gotten around to fixing.

This plugin has run its course, and the browser now offers a better, more feature rich and resilient solution out-of-the-box.

Learn how to animate scrolling to anchor links with one line of CSS, and how to prevent anchor links from scrolling behind fixed or sticky headers.

Thanks for the years of support!


Smooth Scroll Build Status

A lightweight script to animate scrolling to anchor links. Smooth Scroll works great with Gumshoe.

View the Demo on CodePen →

Getting Started | Scroll Speed | Easing Options | API | What's new? | Known Issues | Browser Compatibility | License

Quick aside: you might not need this library. There's a native CSS way to handle smooth scrolling that might fit your needs.


Want to learn how to write your own vanilla JS plugins? Check out my Vanilla JS Pocket Guides or join the Vanilla JS Academy and level-up as a web developer. 🚀


Getting Started

Compiled and production-ready code can be found in the dist directory. The src directory contains development code.

1. Include Smooth Scroll on your site.

There are two versions of Smooth Scroll: the standalone version, and one that comes preloaded with polyfills for closest(), requestAnimationFrame(), and CustomEvent(), which are only supported in newer browsers.

If you're including your own polyfills or don't want to enable this feature for older browsers, use the standalone version. Otherwise, use the version with polyfills.

Direct Download

You can download the files directly from GitHub.

<script src="path/to/smooth-scroll.polyfills.min.js"></script>

CDN

You can also use the jsDelivr CDN. I recommend linking to a specific version number or version range to prevent major updates from breaking your site. Smooth Scroll uses semantic versioning.

<!-- Always get the latest version -->
<!-- Not recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/smooth-scroll/dist/smooth-scroll.polyfills.min.js"></script>

<!-- Get minor updates and patch fixes within a major version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/smooth-scroll@15/dist/smooth-scroll.polyfills.min.js"></script>

<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/[email protected]/dist/smooth-scroll.polyfills.min.js"></script>

<!-- Get a specific version -->
<script src="https://cdn.jsdelivr.net/gh/cferdinandi/[email protected]/dist/smooth-scroll.polyfills.min.js"></script>

NPM

You can also use NPM (or your favorite package manager).

npm install smooth-scroll

2. Add the markup to your HTML.

No special markup needed—just standard anchor links. Give the anchor location an ID just like you normally would.

<a data-scroll href="#bazinga">Anchor Link</a>
...
<div id="bazinga">Bazinga!</div>

Note: Smooth Scroll does not work with <a name="anchor"></a> style anchors. It requires IDs.

3. Initialize Smooth Scroll.

In the footer of your page, after the content, initialize Smooth Scroll by passing in a selector for the anchor links that should be animated. And that's it, you're done. Nice work!

<script>
	var scroll = new SmoothScroll('a[href*="#"]');
</script>

Note: The a[href*="#"] selector will apply Smooth Scroll to all anchor links. You can selectively target links using any other selector(s) you'd like. Smooth Scroll accepts multiple selectors as a comma separated list. Example: '.js-scroll, [data-scroll], #some-link'.

Scroll Speed

Smooth Scroll allows you to adjust the speed of your animations with the speed option.

This a number representing the amount of time in milliseconds that it should take to scroll 1000px. Scroll distances shorter than that will take less time, and scroll distances longer than that will take more time. The default is 300ms.

var scroll = new SmoothScroll('a[href*="#"]', {
	speed: 300
});

If you want all of your animations to take exactly the same amount of time (the value you set for speed), set the speedAsDuration option to true.

// All animations will take exactly 500ms
var scroll = new SmoothScroll('a[href*="#"]', {
	speed: 500,
	speedAsDuration: true
});

Easing Options

Smooth Scroll comes with about a dozen common easing patterns. Here's a demo of the different patterns.

Linear Moves at the same speed from start to finish.

  • Linear

Ease-In Gradually increases in speed.

  • easeInQuad
  • easeInCubic
  • easeInQuart
  • easeInQuint

Ease-In-Out Gradually increases in speed, peaks, and then gradually slows down.

  • easeInOutQuad
  • easeInOutCubic
  • easeInOutQuart
  • easeInOutQuint

Ease-Out Gradually decreases in speed.

  • easeOutQuad
  • easeOutCubic
  • easeOutQuart
  • easeOutQuint

You can also pass in your own custom easing pattern using the customEasing option.

var scroll = new SmoothScroll('a[href*="#"]', {
	// Function. Custom easing pattern
	// If this is set to anything other than null, will override the easing option above
	customEasing: function (time) {

		// return <your formulate with time as a multiplier>

		// Example: easeInOut Quad
		return time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time;

	}
});

API

Smooth Scroll includes smart defaults and works right out of the box. But if you want to customize things, it also has a robust API that provides multiple ways for you to adjust the default options and settings.

Options and Settings

You can pass options and callbacks into Smooth Scroll when instantiating.

var scroll = new SmoothScroll('a[href*="#"]', {

	// Selectors
	ignore: '[data-scroll-ignore]', // Selector for links to ignore (must be a valid CSS selector)
	header: null, // Selector for fixed headers (must be a valid CSS selector)
	topOnEmptyHash: true, // Scroll to the top of the page for links with href="#"

	// Speed & Duration
	speed: 500, // Integer. Amount of time in milliseconds it should take to scroll 1000px
	speedAsDuration: false, // If true, use speed as the total duration of the scroll animation
	durationMax: null, // Integer. The maximum amount of time the scroll animation should take
	durationMin: null, // Integer. The minimum amount of time the scroll animation should take
	clip: true, // If true, adjust scroll distance to prevent abrupt stops near the bottom of the page
	offset: function (anchor, toggle) {

		// Integer or Function returning an integer. How far to offset the scrolling anchor location in pixels
		// This example is a function, but you could do something as simple as `offset: 25`

		// An example returning different values based on whether the clicked link was in the header nav or not
		if (toggle.classList.closest('.my-header-nav')) {
			return 25;
		} else {
			return 50;
		}

	},

	// Easing
	easing: 'easeInOutCubic', // Easing pattern to use
	customEasing: function (time) {

		// Function. Custom easing pattern
		// If this is set to anything other than null, will override the easing option above

		// return <your formulate with time as a multiplier>

		// Example: easeInOut Quad
		return time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time;

	},

	// History
	updateURL: true, // Update the URL on scroll
	popstate: true, // Animate scrolling with the forward/backward browser buttons (requires updateURL to be true)

	// Custom Events
	emitEvents: true // Emit custom events

});

Custom Events

Smooth Scroll emits three custom events:

  • scrollStart is emitted when the scrolling animation starts.
  • scrollStop is emitted when the scrolling animation stops.
  • scrollCancel is emitted if the scrolling animation is canceled.

All three events are emitted on the document element and bubble up. You can listen for them with the addEventListener() method. The event.detail object includes the anchor and toggle elements for the animation.

// Log scroll events
var logScrollEvent = function (event) {

	// The event type
	console.log('type:', event.type);

	// The anchor element being scrolled to
	console.log('anchor:', event.detail.anchor);

	// The anchor link that triggered the scroll
	console.log('toggle:', event.detail.toggle);

};

// Listen for scroll events
document.addEventListener('scrollStart', logScrollEvent, false);
document.addEventListener('scrollStop', logScrollEvent, false);
document.addEventListener('scrollCancel', logScrollEvent, false);

Methods

Smooth Scroll also exposes several public methods.

animateScroll()

Animate scrolling to an anchor.

var scroll = new SmoothScroll();
scroll.animateScroll(
	anchor, // Node to scroll to. ex. document.querySelector('#bazinga')
	toggle, // Node that toggles the animation, OR an integer. ex. document.querySelector('#toggle')
	options // Classes and callbacks. Same options as those passed into the init() function.
);

Example 1

var scroll = new SmoothScroll();
var anchor = document.querySelector('#bazinga');
scroll.animateScroll(anchor);

Example 2

var scroll = new SmoothScroll();
var anchor = document.querySelector('#bazinga');
var toggle = document.querySelector('#toggle');
var options = { speed: 1000, easing: 'easeOutCubic' };
scroll.animateScroll(anchor, toggle, options);

Example 3

// You can optionally pass in a y-position to scroll to as an integer
var scroll = new SmoothScroll();
scroll.animateScroll(750);

cancelScroll()

Cancel a scroll-in-progress.

var scroll = new SmoothScroll();
scroll.cancelScroll();

Note: This does not handle focus management. The user will stop in place, and focus will remain on the anchor link that triggered the scroll.

destroy()

Destroy the current initialization. This is called automatically in the init method to remove any existing initializations.

var scroll = new SmoothScroll();
scroll.destroy();

Fixed Headers

If you're using a fixed header, Smooth Scroll will automatically offset scroll distances by the header height. Pass in a valid CSS selector for your fixed header as an option to the init.

If you have multiple fixed headers, pass in the last one in the markup.

<nav data-scroll-header>
	...
</nav>
...
<script>
	var scroll = new SmoothScroll('.some-selector',{
		header: '[data-scroll-header]'
	});
</script>

What's new?

Scroll duration now varies based on distance traveled. If you want to maintain the old scroll animation duration behavior, set the speedAsDuration option to true.

Known Issues

Reduce Motion Settings

This isn't really an "issue" so-much as a question I get a lot.

Smooth Scroll respects the Reduce Motion setting available in certain operating systems. In browsers that surface that setting, Smooth Scroll will not run and will revert to the default "jump to location" anchor link behavior.

I've decided to respect user preferences of developer desires here. This is not a configurable setting.

<body> styling

If the <body> element has been assigned a height of 100% or overflow: hidden, Smooth Scroll is unable to properly calculate page distances and will not scroll to the right location. The <body> element can have a fixed, non-percentage based height (ex. 500px), or a height of auto, and an overflow of visible.

Animating from the bottom

Animated scrolling links at the very bottom of the page (example: a "scroll to top" link) will stop animated almost immediately after they start when using certain easing patterns. This is an issue that's been around for a while and I've yet to find a good fix for it. I've found that easeOut* easing patterns work as expected, but other patterns can cause issues. See this discussion for more details.

Scrolling to an anchor link on another page

This, unfortunately, cannot be done well.

Most browsers instantly jump you to the anchor location when you load a page. You could use scrollTo(0, 0) to pull users back up to the top, and then manually use the animateScroll() method, but in my experience, it results in a visible jump on the page that's a worse experience than the default browser behavior.

Browser Compatibility

Smooth Scroll works in all modern browsers, and IE 9 and above.

Smooth Scroll is built with modern JavaScript APIs, and uses progressive enhancement. If the JavaScript file fails to load, or if your site is viewed on older and less capable browsers, anchor links will jump the way they normally would.

Note: Smooth Scroll will not run—even in supported browsers—if users have Reduce Motion enabled. Learn more in the "Known Issues" section.

Polyfills

Support back to IE9 requires polyfills for closest(), requestAnimationFrame(), and CustomEvent(). Without them, support starts with Edge.

Use the included polyfills version of Smooth Scroll, or include your own.

License

The code is available under the MIT License.

smooth-scroll's People

Contributors

a-v-l avatar alexbeletsky avatar alexguzman avatar bethrezen avatar bulatnsbln avatar cferdinandi avatar constantm avatar desandro avatar dingyi1993 avatar follesoe avatar itspg avatar joelpittet avatar jonashavers avatar mgreter avatar pguan-phl avatar rikukissa avatar sghoweri avatar thibaudcolas avatar toddmotto 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  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

smooth-scroll's Issues

Easing not working

Maybe I'm missing something, but the easing doesn't seem to be working for me. Clicking on a link simply jumps the anchored point to the top of the page, with no animation taking place.

I'm using these settings to test the functionality, but I'm not getting any response.

:data=>{:speed=>"1500",:easing=>"linear",:url=>"false"}

I've also tried other easing types and different speeds.

Any ideas why this might be happening?

Can't get it to work properly with AngularJS

Do you have any idea if the script could fail if interacting with angular.js (or any other scenario?) I'm currently having a little trouble to get it going. Right now it just jumps to my href='#idname'.

I'd be most grateful if you could help me out, I'm really excited to get this running on my page!

doesnt scroll up from the bottom

Hi,
thanks very much for this project. It was looking great immediately. But I have a problem.
When I scroll down to the absolute bottom of my page there is a fault behavior. When I click any link there (links to a different position on the same site) there is just a little jump upwards approximately a pixel or so. If I hit the link about 15 times it goes up 1 pixel but then suddenly on the 16th time it scrolls smoothly to the right position.
If I first scroll up a little manually it works on the first click. I couldn't figure out what the problem is. Any advice?

Error On Demo/Tutorial Page

The tutorial website has the following code, which is incorrect:

<nav class="scroll-header">

This code should read as follows:

<nav data-scroll-header>

IE10 scroll to top bug.

When at the bottom of IE10 page, can't scroll back up again. All other IE versions worked fine. I think IE10 is rounding up window.pageYOffset (for whatever reason) while other browsers round down, and the following code evaluates to true:

( (window.innerHeight + currentLocation) >= document.body.scrollHeight )

I fixed this in our project by using Math.floor on the position in the function animateScroll.

window.scrollTo( 0, Math.floor(position) );

Hope this helps.

Include option to update animation paramters during tween.

I am using your smooth-scroll, because it is simple and rocks.

On the site I'm working on, there is a large logo in the centre of the above-the-fold content. When the user scrolls down, the large logo gradually leaves the screen (naturally). As it leaves, a header containing a smaller version of the logo slides into position above the nav-bar, proportionally to the amount of the larger logo that is hidden by the top of the page.

When I click the first link in the nav bar, your smooth scroll takes me sailing down to the first anchor. Simultaneously, my weight/counterweight logo animation is changing the height of the header. You can probably see where I'm going with this.

Your smooth-scroll assumes a constant universe, but the real web is always changing. If a user has one sweet animation on their page, chances are they will have two.

This was a pretty easy fix: I just moved some code around in your plugin so that the distance variable is recalculated on every step. No problem!

I suggest that you include this -- perhaps optionally -- so that a user who wants a vivid page in motion can have it. If you are interested, I can prepare a pull request.

Firefox performance is really jumpy

Using firefox 27.0.1 on windows 8 64bit and 28.0 on a windows 7 32bit machines, both of them are high end machines. Scrolling seems to be very sticky at the middle of the scroll, much worse in firefox 27.0.1 than 28.0. Tried changing all the ease settings, time and update url but nothing seems to be helping.

Works flawlessly in google chrome.

www.stephenohara.co.uk/g

Custom selector, Events and RaF

Thanks for the script. Nice and simple (loving vanilla js).

A couple of thoughts:

  • Would there be any benefit in using requestanimationframe over setInterval?
  • It would be awesome to set a personal selector for scrollToggle instead of using the convention of .scroll. This would allow us to use e.g. nav a instead of adding a classname to all our links
  • Since the script prevents the link's default event (such as changing the location.hash) it's harder to e.g. set an .is-active class to highlight the 'current section'. If the script could set a classname to the currently active link that would be great

I know I should probably just do this and issue a PR but just wondering what you think (first) :-)

Go back to one page with the #id.

I want to make a one-page template, but the problem is when I go to other page (the blog page for example) and then I click the links with the #ids from the menu, nothing's happening.

What can I do ?

Thank you very much for your work!

progressive enhancement IE8 - script breaks

I am having some issues with IE8, which I know is not going to smooth scroll with this script. I just want it to behave like a normal anchor link though, but the script has some hard errors in IE8. I'll try and debug a bit more but wondering if you have run into this.

SmoothScroll with skeljs panels

Hello, I try add smoothscroll to page created with skeljs. But it don't work with IE9.
When I add skel scripts into head it don't work in IE9 (in chrome everything works)

To reproduce just add scripts to head in smoothscroll sample page

<script src="js/config.js"></script> <script src="js/skel.min.js"></script> <script src="js/skel-panels.min.js"></script>

"Overflow-x: hidden" in CSS breaks smooth scroll

Hi there,

Are you aware of a way for this to work with overflow-x: hidden set on the html and body elements? Any idea why this breaks the functionality? I'm looking into it now, but thought you may already have a solution. :)

Thanks!

Documentation Request

Would it be possible to update the documentation to include a working example of calling a smooth scroll event using javascript?

I have been trying to implement a fairly simple javascript function that fires when a drop down box is changed. When the user changes the drop down box, I want the page to smoothly scroll down to a predetermined anchor tag.

From within my javascript function I have tried using this:

smoothScroll.animateScroll(anchor: '#bazinga');
or
smoothScroll.animateScroll(anchor, '#bazinga');
or
smoothScroll.animateScroll('anchor: #bazinga');

I have tried several different ways and cannot get the syntax correct. I apologize if this problem is just due to the fact that I find javascript tricky. However, I think potentials users of the script could do with a few more demos.

Thank you, all the best.

Add scrollspy functionality

Hi,
wouldn't it be nice to include the possibility to add the class active to a smooth-scroll link when the viewport is at the links destinations position. Like bootstraps scrollspy method.
I already included this in some of my websites. So if it is in line with your roadmap, I could port the behavior to smooth-scoll

Smooth-scroll interfering with bootstrap dropdown menu

I successfully added smooth-scroll to my html, but it messed up the dropdown button. Since the bootstrap drowdown menu also uses a href="#", it gets picked up by smooth scroll and disables the script for the dropdown. Is there a way to get around this?

Infinite loop FireFox when scrolling to top

Hi,

I've encountered a problem in FireFox caused by a line of code. Line 86 in smooth-scroll.js present in commit: ad413a4 and probably some earlier commits as well.

The following IF-construction causes an infinite loop if you placed an anchor at the top of the page and you want to scroll to that anchor.

if ( travelled <= (endLocation(anchor) || 0) )

When looking into that if statement it's actually expected behaviour for it to cause an infinite loop. ((endLocation(anchor) || 0)) will actually be "false" when the anchor is at position 0 or smaller. So the body of the IF statement will only be executed when "travelled" is smaller or equals "false" which most of the time is never.
And as a result of the use of easing functions "travelled" will most likely never be exactly 0.

You can replace that with the following line to fix the bug.

if ( travelled <= (endLocation(anchor)) || travelled <= 0 )

Regards,

Willem

Provide smooth-scroll.min.js

I know, it's already really small and I can do this by myself.. but why not?

Also consider to use Github's release functionality and maybe provide this library using a cdn like cloudflare, this would simplify the integration process even more :)

Btw. thanks, works like a charm!

It won't scroll to the correct position

Sorry to bother you again, but I have another issue. If you go on rivvr.io or visit my repository, it doesn't scroll to the correct position when I click a scrolling anchor. If you click the link again afterwards, it goes to the correct position.

Enable smooth-scroll for links to other pages

Hey Chris.

Currently, smooth-scroll just works for on-page links. What about adding the possibility to link to different url paths and invoking the scroll animation with the on body load event if a hash (and some additional identifier) is present in the url? This way you could use the same links on multiple pages, e.g. in a navigation.

An example link: /imprint/#.privacy
If the link points to another page, is marked with data-scroll and the hash is present in the url, smooth-scroll would invoke the animation and scroll to the element with the id or class "privacy" on page load event. As for a link to an element on the current page, you should still invoke the animation on the click event. The check could be done using window.location.pathname in this case. If you just use the hash without the additional dot, of course, the browser directly jumps to the element with the id "privacy". So I think you need an additional identifier after the hash which is removed, replaced or interpreted as the class identifier to be used to select the element on the page via the querySelector.

What do you think?

Requirements Clarification

It isn't entirely clear what version of jQuery is required for this? Although, I do note the removal of that dependency but... it is added in the demo. Sorry, I am just a little confused and perhaps the addition of a requirements section for the document file.

Unless I am completely blind...

Change default easing

linear easing feels very jarring as a default. I'm considering using a smarter default, like easeInOutNormal, for a more natural animation.

Add support for smooth scrolling within a div

This project works great against window but doesn't work at all in a div element (or any container tag).
For example:

<style>#list {height: 455px; overflow: scroll; }</style>
<div id="list">
...
lots 
of
content
...
</div>

I poked around and tried to make it work. For one thing you can't do

var container = document.getElementById('list');
var startLocation = container.pageYOffset;

you have to do:

var container = document.getElementById('list');
var startLocation = container.scrollTop;

I would attempt a fork but right now I'm struggling to scroll to the very bottom of the div.

Use a switch case to keep things DRY

Was going to make a pull request but the index.html seems to be broken, none of the links work as the names don't seem to correspond with what's in the JavaScript.

Anyways, there is a lot of repeated code in one of the functions, specifically a batch of if statements, a better implementation would be using a switch and case, one variable to capture the pattern and one return statement.

Instead of this:

var easingPattern = function (type, time) {
  if ( type == 'linear' ) return time; // no easing, no acceleration
  if ( type == 'easeInQuad' ) return time * time; // accelerating from zero velocity
  if ( type == 'easeOutQuad' ) return time * (2 - time); // decelerating to zero velocity
  if ( type == 'easeInOutQuad' ) return time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time; // acceleration until halfway, then deceleration
  if ( type == 'easeInCubic' ) return time * time * time; // accelerating from zero velocity
  if ( type == 'easeOutCubic' ) return (--time) * time * time + 1; // decelerating to zero velocity
  if ( type == 'easeInOutCubic' ) return time < 0.5 ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1; // acceleration until halfway, then deceleration
  if ( type == 'easeInQuart' ) return time * time * time * time; // accelerating from zero velocity
  if ( type == 'easeOutQuart' ) return 1 - (--time) * time * time * time; // decelerating to zero velocity
  if ( type == 'easeInOutQuart' ) return time < 0.5 ? 8 * time * time * time * time : 1 - 8 * (--time) * time * time * time; // acceleration until halfway, then deceleration
  if ( type == 'easeInQuint' ) return time * time * time * time * time; // accelerating from zero velocity
  if ( type == 'easeOutQuint' ) return 1 + (--time) * time * time * time * time; // decelerating to zero velocity
  if ( type == 'easeInOutQuint' ) return time < 0.5 ? 16 * time * time * time * time * time : 1 + 16 * (--time) * time * time * time * time; // acceleration until halfway, then deceleration
};

You would use this:

var easingPattern = function (type, time) {
  var pattern;
  switch (type) {
  case 'linear':
      pattern = time; // no easing, no acceleration
      break;
  case 'easeInQuad':
      pattern = time * time; // accelerating from zero velocity
      break;
  case 'easeOutQuad':
      pattern = time * (2 - time); // decelerating to zero velocity
      break;
  case 'easeInOutQuad':
      pattern = time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time; // acceleration until halfway, then deceleration
      break;
  case 'easeInCubic':
      pattern = time * time * time; // accelerating from zero velocity
      break;
  case 'easeOutCubic':
      pattern = (--time) * time * time + 1; // decelerating to zero velocity
      break;
  case 'easeInOutCubic':
      pattern = time < 0.5 ? 4 * time * time * time : (time - 1) * (2 * time - 2) * (2 * time - 2) + 1; // acceleration until halfway, then deceleration
      break;
  case 'easeInQuart':
      pattern = time * time * time * time; // accelerating from zero velocity
      break;
  case 'easeOutQuart':
      pattern = 1 - (--time) * time * time * time; // decelerating to zero velocity
      break;
  case 'easeInOutQuart':
      pattern = time < 0.5 ? 8 * time * time * time * time : 1 - 8 * (--time) * time * time * time; // acceleration until halfway, then deceleration
      break;
  case 'easeInQuint':
      pattern = time * time * time * time * time; // accelerating from zero velocity
      break;
  case 'easeOutQuint':
      pattern = 1 + (--time) * time * time * time * time; // decelerating to zero velocity
      break;
  case 'easeInOutQuint':
      pattern = time < 0.5 ? 16 * time * time * time * time * time : 1 + 16 * (--time) * time * time * time * time; // acceleration until halfway, then deceleration
      break;
  }
  return pattern;
};

readme.md

It still says you're using jquery, it's vanilla js now right? (:

iPad scrolling

On my iPad in Safari, scrolls nicely down to the anchor, but when you try to get back to the top, as soon as you release, the page snaps back down ... impossible to get back to the top of the page.

Scroll does not work on external or complete links

I'm using mvc 4 to develop a website. I tried using the code and it works with anchors on same page but one problem is if I navigate to another page and try to come back to the anchor point on previous page using a complete link and the #anchor at end the scroll does not function at all and the link stops navigating.

P.S I have a main navigation bar on top of the pages.
for instance this works:
a href="#home" class="scroll"
but this doesnt:
a href="http://site.com/Index#how" class="scroll"

Opposite Reaction than Intended

My code is correct, but I'm getting the exact opposite response - when I click my anchor link I scroll back to its location instead of the id. Any ideas on why?

Possible to add offset?

In sites with fixed navbar, it needs an offset option to get the headers out from under the nav. How would one do that?

4.7.1

scroll not working...

Scroll to absolute position which is over to the right side

Hi THere

I am trying to do something fun with absolutley positioned divs and would like to move from anchor to anchor within a large page area of about 8000pxs square. I start, for various reasons, by loading the page at an anchor offset 1000px to the right of the left page edge (in a small window). I want to scroll downwards from here to my next anchor. However when I do with smooth scroll it jumps the page back to the left hand side so as opposed to going to top: 2000px left:1000px - I end up on the left page edge.

Is this a bug / me messing up or is there anything I can do so it scrolls horizontally smoothly too?

Thanks

Toby

Highlight active link using smooth scroll

Excellent plugin. Really love it.
I am trying to use it in navigation bar. it works fine. Is there a way to highlight active link when scrolling? Assign a class on active link or so....
Any help is highly appreciated.

Offset Scroll Location

I am using this in a site with a fixed header, which means that in it's out-of-the-box form scrolling to section IDs leaves some of the section covered by the header.

Is there any way to offset the location that the scroll heads to when triggered by an ID? Like a negative margin or something?

Scroll gets stuck in Chrome and Safari when Browser Zoom is other than 100%

Hello,

We are building a page and utilizing smooth-scroll.js. The problem was discovered in Chrome and Safari browsers. On machines that have their browsers with a zoom of less or more than 100%, the focus gets stuck to one area of the page after the first use of smooth-scroll, if I try to scroll up or down, it won't let me, and kind of forces me to stay at that part of the page until I refresh. It seemed to work fine with firefox and IE.

This was tested on Chrome Version 32.0.1700.102 m on Windows 7, Windows 8
and Chrome version 32.0.1700.102 and Safari 7.0.1 on Mac OS X Version 10.9.1

I would like to know if there is a work around or a fix to this issue.

Thank you very much,

Scrolls back to bazinga on the demo page

On the demo page, when you press one of the anchor links and it scrolls down to bazinga—at this moment, if you immediately scroll up (back to the links list), it won't let you. The script will return you back to bazinga (with animation). Reproduceable on iPhone, didn't try other devices.

Back button doesn't work in Firefox when using the dataURL feature

Hello again,

Works fine in Chrome, but the old broken behavior is back in Firefox and IE. This happens:

  1. Load the page.
  2. Click a link.
  3. Click back.
  4. Note the URL changes back, but your position doesn't move.

This may actually turn out to be "by design" since at the end of those steps you are at your last position on the page reflected by the URL bar. That it works like I want it in Chrome is probably a Chrome bug.

I think I could fix this by adding the onpopstate from #18 back, but I don't have time to do appropriate testing tonight. I also want to think about other scenarios besides my personal site in which "fixing" this may cause problems. I'll try to figure it out this weekend.

Thanks,

Robert

p.s.
I think this is unrelated, but I should also remove the check for history.pushState in updateURL() since that function no longer calls history.pushState as of version 2.17.

It wont scroll down, only up.

I have a problem where on my website, which is arranged in sections which take up the height and width of your viewport, it cannot scroll to a section below the current position, but it is fine scrolling up. I've tried everything, please help!

Scrolls to different places on same anchor

Hi,
I'm using this in conjunction with a sticky nav menu and when scrolling from the original position (unstuck) we land in a different place then when the scroll is fired from anywhere else. I've tried the data-header thing but it seems to measure the container before it's been horizontally styled. It's got to have something to do with the stuck/unstuck states.
Thoughts?
Nice script BTW

http://69.195.124.213/~gjdcorg/center.html

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.