javascript - Go back one page and scroll to div ID -
first off taking time read question. i'm trying make normal link (href), link 1 page , scroll div id
. i've been looking @ question: here. want. i've linked javascript file in header , placed jquery code in footer. nothing.
this link:
<div class="col-xs-4 text-center"> <a href="/the_liked_page/" data-id="my_id" class="gobackandscroll"><i class="fa fa-th-large fa-2x" aria-hidden="true"></i><br>go back</a> </div>
nothing fancy... , here script:
// read cookie , if it's defined scroll id var scroll = jquery.cookie('scroll'); if(scroll){ scrolltoid(scroll, 1000); jquery.removecookie('scroll'); } // handle event onclick, setting cookie when href != # jquery('.gobackandscroll').click(function (e) { e.preventdefault(); var id = jquery(this).data('id'); var href = jquery(this).attr('href'); if(href === '#'){ scrolltoid(id, 1000); }else{ jquery.cookie('scroll', id); window.location.href = href; } }); // scrolltoid function function scrolltoid(id, speed) { var offset = 65; var obj = jquery('#' + id).offset(); var targetoffset = obj.top - offset; jquery('html,body').animate({ scrolltop: targetoffset }, speed); };
i don't errors in console.log , links page want. there no scrolling.
when delete data , mark both pages this: http://plnkr.co/edit/l2aassm4biynrwncrvuz?p=preview work. javascript correctly loaded , storing cookie.
so thought maybe data-id not stored i've put alert on click using:
alert(id);
this gives correct data-id
doesn't scroll on next page. have idea why isn't working or better suggestion this? of help...
Comments
Post a Comment