今、私はしばらくそれをしてきました。 1ページのスクロールはホームページ(div idが定義されている場所)で動作します。ワンページスクロール:ホームページにリダイレクトしてdivにスクロールしようとしています - 動作していませんか?
問題:ブログのページから、ユーザーが「連絡先」をクリックすると、ホームページにリダイレクトされます。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)
});
}
問題を解決する方法はありますか? window.location.href
の値を更新する際に
これは、JavaScriptを初めて使用しています。あなたは言った:余分な存在しないアンカーによって追加された新しいページを要求する。このソリューションを実装する方法のコード例を教えてもらえますか? – Shaz
確かに、私がその記事に行った変更を見てください。 –