对,我已经有一段时间了。一个页面滚动在主页上(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
当然,请查看我对该帖子所做的更改。 –