【问题标题】:Leave page on button click, after new page loads initiate a smooth scroll to a specified location点击按钮离开页面,新页面加载后启动平滑滚动到指定位置
【发布时间】:2014-02-21 02:06:48
【问题描述】:

我已经在这个场景上工作了几个小时,但我终于不知所措了。我很确定我以前见过这样做,但这可能是不可能的,如果不可能的话,我不想浪费更多时间。

我想单击一个按钮,加载一个新的 url,然后在文档准备好后,我希望它使用动画自动平滑滚动到该页面上的指定位置(因此它滚动平滑并且我可以控制过渡速度)。

我认为这样的事情会奏效。我可能很接近但没有雪茄!

JS:

$("#backToWorkBtn").click(function() {

    window.location.href = "index.php";

    $(window).on("load", function () {
        $('html,body').animate({
            scrollTop: $("#workSection").offset().top
        }, 800);
    });
});

我也试过了……

$("#backToWorkBtn").click(function() {

    window.location.href = "index.php";

    $(document).ready(function() {
        $('html,body').animate({
            scrollTop: $("#workSection").offset().top
        }, 800);
    });
});

HTML:

下面是gallery.php

<a id="backToWorkBtn">BACK TO MY WORK</a>

下面是 index.php

<section id="introSection">
    Intro Area
</section>

<section id="aboutSection">
    About Me Area
</section>

<section id="workSection">
    <a href="gallery.php">Web Design</a>
    <a href="gallery.php">Print Design</a>
    etc etc etc..
</section>

【问题讨论】:

    标签: jquery onclick onload scrollto scrolltop


    【解决方案1】:

    切换它们。 .click() 处理程序应该进入就绪函数。

    $(document).ready(function() {
        $('html,body').animate({
            scrollTop: $("#workSection").offset().top
        }, 800);
    
        $("#backToWorkBtn").click(function() {
            window.location.href = "index.php";  
        });
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用 Ariel Flesler 的 jQuery.scrollTo 插件来完成此操作。

      查看:Live Demo


      Page 1:

      JS

      $('#myButton').click(function(){
          window.location.href = 'http://fiddle.jshell.net/TLeHz/3/show/?scroll=scrollToMe';
      }); 
      

      HTML

      <input id='myButton' type='button' value='navigate' />
      

      Page 2:

      JS

      $(function(){
          //From: http://stackoverflow.com/a/901144/402706
          function getParameterByName(name) {
              name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
              var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                  results = regex.exec(location.search);
              return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
          }
      
          //Scroll based on the value of the Query String param 'scroll'
          var scrollToArea = getParameterByName('scroll');
          if(scrollToArea){
              $.scrollTo('#' + scrollToArea, 800 );        
          }
      
      });
      

      HTML

      <div>test</div>
      <div>test</div>
      <div>test</div>
      <div>test</div>
      <div id='scrollToMe'>scroll to me</div>
      

      CSS

      div{
          height:500px;
      }
      

      【讨论】:

      • 感谢布兰登的回复,感谢。当按钮与容器位于同一页面时,我实际上已经滚动到工作....但我想要做的是单击gallery.php上的一个按钮,它会将您带到另一个页面(index.php),在 index.php 加载文档之后,它会在 index.php 上启动 scrollto 并从顶部滚动到 #workSection。该按钮不在同一页面上,希望有意义。
      • 你描述的不正是我展示的功能吗?也许我误解了你。你的带按钮的gallery.php 是我的带按钮的Page 1 (navigate)。您的index.php 应该加载并启动从顶部滚动到#workSection 是我的Page 2,它启动从顶部到#scrollToMe 的滚动。单击我的Live Demo 链接(也是Page 1)并单击navigate 按钮显示这些页面如何协同工作。您应该能够使用这种精确的方法来完成您的目标。让我知道我错过了什么。
      • 啊,不,你已经很接近了......但我只想在第 1 页 (gallery.php) 上的按钮被点击时启动第 2 页 (index.php) 上的滚动.在您的示例中,每次页面加载时,无论是否单击按钮,滚动都将在页面上启动。提供一些上下文...索引页面是一个非常长的页面,其中大约在页面中间有一个指向画廊的链接。当您转到图库并单击后退按钮时,默认操作会将您返回到索引页面,但到顶部,然后用户必须手动一直向下滚动到他们刚刚离开的图库部分。
      • 我希望它带您回到索引,然后在页面加载后自动滚动到中间部分(这就是为什么我尝试在点击内准备好文档 [以及 onLoad]功能,但该逻辑不正确)。另外,我想避免 URL 中的主题标签,我应该注意这一点。这是一个有趣的挑战。
      • 同样,这是一个有趣的问题。我想我明白你在说什么。在 URL 中不使用主题标签,但仍然使用查询字符串,我想我已经完成了你所追求的。让我知道这是否适合您。
      【解决方案3】:

      好的,我今天终于解决了这个问题。这有点痛苦,但总的来说,我现在可以正常工作了。我正在使用一个查询字符串(如果您想花时间构建该逻辑,您可以在 URL 中屏蔽它)。这至少是建立和改进的起点。感谢 Brandon Boone 的帮助!

      第 1 页 (index.php)

      <script>
      function GetQueryVariable(query, name) {
          if (query.indexOf("?") == 0) { query = query.substr(1); }
          var pairs = query.split("&");
          for (var i = 0; i < pairs.length; i++) {
              var pair = pairs[i].split("=");
              if (pair[0] == name) {
                  return pair[1];
              }
          }
          return "";
      }
      
      var pageMark = GetQueryVariable(location.search, "id");
      
      $(function() {
          $('html, body').animate({
              scrollTop: $('#' + pageMark).offset().top
          }, 2000);
          return false;
      });
      </script>
      
      <section id="introSection">
          Stuff Here
      </section>
      
      <section id="aboutSection">
          Stuff Here
      </section>
      
      <section id="workSection">
          <a href="gallery.php">Web Work</a>
      </section>
      
      <section id="faqSection">
          Stuff Here
      </section>
      

      第 2 页 (gallery.php)

      <a href="index.php?id=workSection">GO BACK TO MAIN PAGE</a>
      

      【讨论】:

        猜你喜欢
        • 2015-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多