javascript - One-Page Scroll: Trying to redirect to home page and scroll to the div - Not Working? -
right, i've been @ while now. 1 page scroll works on home page (where div ids defined).
problem: blog page, if user clicks 'contact', want them redirected home page animated scroll #contact-view
div.
attempt: i've tried check if user clicks a
tag, check if there blog page using if statement
, redirect them home page (and div) using
window.location.href = home +sectionid;
not working.
header.php (only nav bit..)
<nav class="header-nav"> <!-- create navigation called primary in header (front-end) --> <?php $args = array('theme_location' => 'primary'); ?> <?php wp_nav_menu( $args) ?> </nav>
front-page.php (home)
<div id="about-view" class="bg-1-wrapper"> #content </div> <div id="contact-view"> #content </div>
javascript
jquery(document).ready(function() { // add click listener each <a> tags setbindings(); // burger nav jquery(".burger-nav").on("click", function() { jquery(".header-nav").toggleclass("open"); }); }); function redirecttohome() { } /* 1 page navigation function */ function setbindings() { jquery('a[href^="#"]').on('click', function(e) { e.preventdefault(); var home = "http://localhost/wordpress/"; // href attribute, includes '#' var sectionid = jquery(this).attr('href') + "-view"; // if you're on blog page, first redirect home -> div: #contact-view if(document.url.indexof("http://localhost/wordpress/blog") >= 0){ window.location.href = home +sectionid; console.log(location.href); } // animate scroll jquery("html, body").animate({ // find target element scrolltop: jquery(sectionid).offset().top }, 1000) }); }
any ideas how fix problem?
two things can happen when updating value of window.location.href
:
- if new value located @ current page (by using anchor), jump point without reloading page
- if new value not located @ current page, javascript execution stop , new page requested , loaded
the problem animate
function not executing latter in case.
a solution request new page appended non-existent anchor (#contact-view-scroll, example). on new page, use javascript check particular anchor value in url , execute scroll functionality if necessary.
hope helps in understanding , solving problem.
update: added example code below
step 1: on blog page, update links refer home page prepending following content hashtag: #scroll:
(such http://localhost/wordpress/home#contact-view becomes http://localhost/wordpress/home#scroll:contact-view)
step 2: add following code snippet home page. search #scroll:
tag , activate animate
function if present.
jquery(document).ready(function() { // hash url var hash = window.location.hash; // if hash exists && starts "#scroll:" if (hash.length , hash.indexof('#scroll:') === 0) { // anchor name scrolling var selectionid = hash.substr('#scroll:'.length); // call jquery scroll function jquery("html, body").animate({ scrolltop: jquery('#'+selectionid).offset().top }, 1000); } });
step 3: enjoy!
also, see https://jsfiddle.net/qcarmk2w demo.
Comments
Post a Comment