简体   繁体   English

一页滚动:尝试重定向到主页并滚动到div-不起作用?

[英]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的值时,可能会发生两件事:

  • If the new value is located at the current page (by using an anchor), it will jump to that point without reloading the page 如果新值位于当前页面上(使用锚点),它将跳到该点,而无需重新加载页面
  • If the new value is not located at the current page, JavaScript execution will stop and the new page will be requested and loaded 如果新值不在当前页面上,则JavaScript执行将停止并且将请求并加载新页面

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以获取演示。

重定向到另一个页面并滚动到特定页面<div>没有一起工作</div><div id="text_translate"><p>好的,这是我在这个论坛上发布的第一个问题,所以请善待.. :)</p><p> 我正在使用 ASP.NET MVC 5,并且在单击图标时尝试执行两步过程。</p><ol><li> Select 正确的页面</li><li>向下滚动到该页面上的某个部分。</li></ol><p> 这是我到目前为止得到的:</p><pre> &lt;a class="sidebar-brand d-flex align-items-center justify-content-start" &gt; &lt;div class="notification-bell" style="color:red"&gt; &lt;i class="fas fa-fw fa-bell fa-2x" title="Number of Unread Comments" alert-count=@ViewBag.TotalUnreadComments.ToString() onclick='scrollToElement("CommentSection");'&gt;&lt;/i&gt; &lt;/div&gt; &lt;/a&gt;</pre><p> 和 Javascript</p><pre> &lt;script type='text/javascript'&gt; function scrollToElement(id) { // Set correct page window.location.replace("/TodoListDashboard"); //Get target var target = document.getElementById(id).offsetTop; //Scrolls to that target location window.scrollTo(0, target); } &lt;/script&gt;</pre><p> 有趣的是,这些动作中的任何一个都可以单独起作用,但它们不会一起起作用。</p><p> 任何想法将不胜感激!!!</p></div> - Redirect to another page and scroll to specific <div> are not working together

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery单页滚动不起作用 - jQuery one-page scroll not working 一页滚动导航无法使用Javascript吗? - One-page scroll nav not working with Javascript? 一页滚动问题 - One-page scroll problems WordPress:单页滚动JQuery脚本不起作用? - WordPress: One-page scroll JQuery script not working? 如果不存在滚动到div,则重定向到主页 - Redirecting to home page if scroll to div not exist 重定向到另一个页面并滚动到特定页面<div>没有一起工作</div><div id="text_translate"><p>好的,这是我在这个论坛上发布的第一个问题,所以请善待.. :)</p><p> 我正在使用 ASP.NET MVC 5,并且在单击图标时尝试执行两步过程。</p><ol><li> Select 正确的页面</li><li>向下滚动到该页面上的某个部分。</li></ol><p> 这是我到目前为止得到的:</p><pre> &lt;a class="sidebar-brand d-flex align-items-center justify-content-start" &gt; &lt;div class="notification-bell" style="color:red"&gt; &lt;i class="fas fa-fw fa-bell fa-2x" title="Number of Unread Comments" alert-count=@ViewBag.TotalUnreadComments.ToString() onclick='scrollToElement("CommentSection");'&gt;&lt;/i&gt; &lt;/div&gt; &lt;/a&gt;</pre><p> 和 Javascript</p><pre> &lt;script type='text/javascript'&gt; function scrollToElement(id) { // Set correct page window.location.replace("/TodoListDashboard"); //Get target var target = document.getElementById(id).offsetTop; //Scrolls to that target location window.scrollTo(0, target); } &lt;/script&gt;</pre><p> 有趣的是,这些动作中的任何一个都可以单独起作用,但它们不会一起起作用。</p><p> 任何想法将不胜感激!!!</p></div> - Redirect to another page and scroll to specific <div> are not working together 捕获Magento一页结帐重定向 - Catching Magento one-page checkout redirect jQuery Sticky Div并滚动到仅在一页上工作的顶部插件 - jQuery Sticky Div and scroll to top plugins working on one page only 当我单击一页滚动导航中的菜单项时,在控制台中触发 TypeError - TypeError fired in the Console when I click a menu item in a one-page scroll navigation 自定义一页滑块不起作用 - A custom one-page slider is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM