[英]One-Page Scroll: Trying to redirect to home page and scroll to the div - Not Working?
Right, I've been at it for a while now. 是的,我已经有一段时间了。 One page scroll works on home page (where the div ids are defined).
一页滚动可在主页上进行(定义div ID)。
Problem: FROM blog page, if a user clicks 'contact', I want them to be redirected to the home page THEN animated scroll to the #contact-View
div. 问题:在博客页面上,如果用户单击“联系人”,我希望将他们重定向到主页,然后将动画滚动到
#contact-View
div。
Attempt: I've tried to check if user clicks a
tag, then check if there are now blog page using if statement
, then redirect them back to home page (and to the div) using 尝试:我尝试检查用户是否单击
a
标签,然后使用if statement
检查是否存在博客页面,然后使用重定向到主页(和div)
window.location.href = home +sectionID;
Not working. 不工作
header.php (only the nav bit..) header.php(仅导航位..)
<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>
front-page.php (home) front-page.php(主页)
<div id="about-View" class="bg-1-wrapper"> #content </div>
<div id="contact-View"> #content </div>
Javascript 使用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)
});
}
Any ideas how to fix the problem? 任何想法如何解决该问题?
Two things can happen when updating the value of window.location.href
: 更新
window.location.href
的值时,可能会发生两件事:
The problem with the animate
function not executing is the latter in this case. 在这种情况下,
animate
功能无法执行的问题是后者。
A solution would be to request the new page appended by an extra non-existent anchor ( #contact-View-scroll , for example). 一种解决方案是请求新页面附加一个额外的不存在的定位符(例如#contact-View-scroll )。 On the new page, you could use JavaScript to check for this particular anchor value in the URL and execute the scroll functionality if necessary.
在新页面上,您可以使用JavaScript来检查URL中的此特定锚值,并在必要时执行滚动功能。
Hope this helps in understanding and solving your problem. 希望这有助于理解和解决您的问题。
update : added some example code below 更新 :在下面添加了一些示例代码
Step 1: On the blog page, update the links that refer to the home page by prepending the following content to the hashtag: #scroll:
(Such that http://localhost/wordpress/home#contact-View becomes http://localhost/wordpress/home#scroll:contact-View ) 第1步:在博客页面上,通过将以下内容添加到主题标签之前,更新引用主页的链接:
#scroll:
例如, http:// localhost / wordpress / home#contact-View变为http:// localhost / wordpress / home#scroll:contact-View )
Step 2: Add the following code snippet to the home page. 步骤2:将以下代码段添加到主页。 This will search for the
#scroll:
tag and activate the animate
function if present. 这将搜索
#scroll:
标记并激活animate
功能(如果存在)。
jQuery(document).ready(function() {
// Get the hash from the URL
var hash = window.location.hash;
// If a hash exists && starts with "#scroll:"
if (hash.length and hash.indexOf('#scroll:') === 0) {
// Get the anchor name we are scrolling to
var selectionID = hash.substr('#scroll:'.length);
// Call the jQuery scroll function
jQuery("html, body").animate({
scrollTop: jQuery('#'+selectionID).offset().top
}, 1000);
}
});
Step 3: Enjoy! 步骤3:尽情享受!
Also, see https://jsfiddle.net/qcarmk2w for a demo. 另外,请参阅https://jsfiddle.net/qcarmk2w以获取演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.