Home jquery Smooth scrolling to anchor jQuery

Smooth scrolling to anchor jQuery

Author

Date

Category

Hello, I’m still just learning a little … do not catch up, in general, there is a menu on the page

& lt; ul & gt;
& Lt; li & gt; & lt; a class = "link active" href = / home & gt; Home & lt; / a & gt; & lt; / li & gt;
& Lt; li & gt; & lt; a class = "link" href = / service & gt; Services & lt; / a & gt; & lt; / li & gt;
& Lt; li & gt; & lt; a class = "link" href = / reserv & gt; Reservation & lt; / a & gt; & lt; / li & gt;
& lt; / ul & gt;

When you scroll wheel or slider down the URL of the web takes the form moysayt / # / home ie there is a division moysayt + / # / + the appropriate section of the site (div ID) using the function attr ( ‘href’). It is this separation is crucial. Now you just need to make a smooth scrolling to the anchor (div ID) or Name in general to a specific section of the scroll and the URL when it has changed accordingly. I will be glad to any advice, tips and guidance. Thank you


Answer 1, Authority 100%

To use smooth scrolling usually here this little feature, simply paste it into the code and everything.

/ *! Smooth Scrolling http://css-tricks.com/snippets/jquery/smooth-scrolling/ * /
$ (Function () {$ ( "a [href * = #]: not ([href = #])") click (function () {if (location.pathname.replace (/ ^ \ //, "". ) == this.pathname.replace (/ ^ \ //, "") & amp; & amp; location.hostname == this.hostname) {var t = $ (this.hash); if (t = t.length? t: $ ( "[name =" + this.hash.slice (1) + "]"), t.length) return $ ( "html, body") animate ({scrollTop:. t.offset () top. } 1000) ,! 1}})});

Answer 2

A small function on the Vanilla JS :
jsFiddle

/ **
* ScrollTo scrolls to the specified place in it
* @Param {HTMLElement} to the element to which the scrolls
* @Param {Number} time the animation in ms
* /
function scrollTo (to, time) {
  var from = window.pageYOffset;
  var to = to.getBoundingClientRect () top.;
  var speed = time / (to - from);
  var start = null;
  requestAnimationFrame (step);
  function step (timestamp) {
    if (start === null) start = timestamp;
    var progress = timestamp - start;
    var y = (to & lt; 0 Math.max (from - progress / speed, current + to):? Math.min (from + progress / speed, from + to));
    window.scrollTo (0, y);
    if (y & lt; from + to) {
      requestAnimationFrame (step)
    }
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions