對,我已經有一段時間了。一個頁面滾動在主頁上(div id被定義)工作。單頁滾動:嘗試重定向到主頁並滾動到div - 不工作?
問題:從博客頁面,如果用戶點擊'聯繫人',我希望他們被重定向到主頁,然後動畫滾動到#contact-View
div。
嘗試:我試圖檢查用戶是否點擊a
標籤,然後檢查是否有使用if statement
現在的博客頁面,然後重定向他們回到使用
window.location.href = home +sectionID;
不工作。
的header.php(只有NAV位..)
<nav class="header-nav">
<!-- Create a navigation called primary in the header (front-end) -->
<?php $args = array('theme_location' => 'primary'); ?>
<?php wp_nav_menu($args) ?>
</nav>
前page.php文件(家)
<div id="about-View" class="bg-1-wrapper"> #content </div>
<div id="contact-View"> #content </div>
的Javascript
jQuery(document).ready(function() {
// add a click listener to each <a> tags
setBindings();
// burger nav
jQuery(".burger-nav").on("click", function() {
jQuery(".header-nav").toggleClass("open");
});
});
function redirectToHome() {
}
/* ONE PAGE NAVIGATION FUNCTION */
function setBindings() {
jQuery('a[href^="#"]').on('click', function(e) {
e.preventDefault();
var home = "http://localhost/wordpress/";
// Get the href attribute, which includes '#' already
var sectionID = jQuery(this).attr('href') + "-View";
// if you're on blog page, first redirect to home -> div: #contact-View
if(document.URL.indexOf("http://localhost/wordpress/blog") >= 0){
window.location.href = home +sectionID;
console.log(location.href);
}
// Animate the scroll
jQuery("html, body").animate({
// Find target element
scrollTop: jQuery(sectionID).offset().top
}, 1000)
});
}
任何想法如何解決這個問題?
好的,JavaScript新手。你說過:請求附加了一個額外的不存在的錨點的新頁面。你能給我一個代碼示例,說明如何實現這個解決方案嗎? – Shaz
當然,請查看我對該帖子所做的更改。 –