-1

I have a javascript function that calculates some stuff, and I need to initiate this (call the function) but at the same time I need to scroll to where I have the results.

I need smooth scrolling so I have a separate function handling this.

I've tried the following:

<button id="btn" onClick="calculate(); window.location='#results';">Calculate</button>

While it both calculates AND scrolls, it doesn't activate my smooth scrolling function, for some reason, and just hard/regular jumps to .

I am using this https://github.com/Yappli/smooth-scroll for smooth scrolling, where it turns every #link into a smooth scroll.

How do I make this work?

I am doing all of this with no jQuery, as it needs to be super light weight.

Trace DeCoy
  • 649
  • 6
  • 16
  • 1
    where are you calling the smooth scrolling function? no evidence of that in the snippet you posted, just calculate() and a JUMP to #results – Jaromanda X Jul 04 '15 at 11:17
  • Ahh yes, sorry. I am using this https://github.com/Yappli/smooth-scroll where it turns all # links into smooth scrolling. The code snippet is physically placed before – Trace DeCoy Jul 04 '15 at 11:20
  • THought I had an answer - but it was wrong – Jaromanda X Jul 04 '15 at 11:37
  • http://stackoverflow.com/questions/5007530/how-do-i-scroll-to-an-element-using-javascript/20670708#20670708 - you wont need your smooth-scroll javascript for that one – Jaromanda X Jul 04 '15 at 11:52

1 Answers1

0

You could modify the Smooth-Scroll so that it looks for a class instead of a elements.

Change line 19 from:

links = document.getElementsByTagName('a'),

To:

links = document.getElementsByClassName('scroll-to'),

You could do both, but you'll need to merge the two HTMLCollections, something like this:

links = document.getElementsByTagName('a'),
classes = document.getElementsByClassName('scroll-to'),
links = [].concat(Array.prototype.slice.call(links), Array.prototype.slice.call(classes));
PiranhaGeorge
  • 999
  • 7
  • 13