jQuery smooth scrolling anchor URL issue -


i have found piece of code css-tricks works have unique situation requires custom coding.

the original script:

$(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {   var target = $(this.hash);   target = target.length ? target : $('[name=' + this.hash.slice(1) +']');   if (target.length) {     $('html,body').animate({       scrolltop: target.offset().top     }, 1000);     return false;   } } }); }); 

the problem face use cms system. url page see is: http://www.sitename.com/home

the actual path page is: http://www.sitename.com/home/index

the html looks like:

<a href="#one"></a> <div id="one"></div> 

what cms converts to:

<a href="home/index#one"></a> <div id="one"></div> 

so first time click anchor link instead of scrolling jumps because changes url /home /home/index#one after smooth scroll works because url /home/index

is there way can scroll between 2 elements without using anchors or without changing url or can script css-tricks altered unique situation.

you solve in few ways. here's couple of options.

simple answer use html5 history api push /home/index, history, , onto browsers url, on load.

history.pushstate({}, "index", "/home/index"); // or history.replacestate({}, "index", "/home/index"); 

.replacestate yield better ux, avoid double-backs.

the other way rework how anchors work. using common classes, data attributes, , managing hashes on own. can avoid relying on our href match our element's id. in instance, href provides fallback when js turned off, , set id attributes.

demo output

see output above best example.

function sethash (h) {    window.location.hash = h;  }    $('.section-link').on('click', function (e) {    e.preventdefault();        var self = $(this),        target = self.data('target'),        subject = $('[data-section-name="' + target + '"]'),        offset = subject.offset().top;        $('html, body').animate({      scrolltop: offset    }, 500);        sethash(target);  });
html,  body {    height: 100%;  }    section {    width: 100%;    height: 100%;        background-color: #eee;  }    section:nth-child(odd) {    background-color: #ccc;  }
<section data-section-name="one">      <h1>one</h1>            <ul>        <li>          <a href="#one" class="section-link" data-target="one">one</a>          <a href="#two" class="section-link" data-target="two">two</a>          <a href="#three" class="section-link" data-target="three">three</a>          <a href="#four" class="section-link" data-target="four">four</a>        </li>      </ul>    </section>    <section data-section-name="two">      <h1>two</h1>            <ul>        <li>          <a href="#one" class="section-link" data-target="one">one</a>          <a href="#two" class="section-link" data-target="two">two</a>          <a href="#three" class="section-link" data-target="three">three</a>          <a href="#four" class="section-link" data-target="four">four</a>        </li>      </ul>    </section>    <section data-section-name="three">      <h1>three</h1>            <ul>        <li>          <a href="#one" class="section-link" data-target="one">one</a>          <a href="#two" class="section-link" data-target="two">two</a>          <a href="#three" class="section-link" data-target="three">three</a>          <a href="#four" class="section-link" data-target="four">four</a>        </li>      </ul>    </section>    <section data-section-name="four">      <h1>four</h1>            <ul>        <li>          <a href="#one" class="section-link" data-target="one">one</a>          <a href="#two" class="section-link" data-target="two">two</a>          <a href="#three" class="section-link" data-target="three">three</a>          <a href="#four" class="section-link" data-target="four">four</a>        </li>      </ul>    </section>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -