简体   繁体   English

如何在2个单独的页面之间创建平滑滚动加载效果

[英]How to create effect of smoothscroll loading between 2 separate pages

I have a standard implementation of bootstrap's smoothscroll.js on my page, which works fine moving between different anchors. 我在页面上有bootstrap的smoothscroll.js的标准实现,可以在不同锚点之间很好地移动。

However, the page is quite long to load, and I've been advised best practice to split this into 2 pages. 但是,该页面的加载时间很长,因此建议我最好的做法是将该页面分为2个页面。 Any ideas how I could modify the JS to still achieve the same appearance of smooth scroll between pages? 有什么想法可以修改JS来实现页面之间的平滑滚动外观吗?

I'm stumped at how I might try to do this; 我对如何尝试做到这一点感到困惑; fairly new to coding! 对编码来说还很新!

Thanks 谢谢

如果将其拆分为2个不同的HTML,请在两个页面上均使用<script src="yourscript.js"></script>

You can pass the scrollto value in the url as a hash, and in your doc.ready function for both pages you can check the url for a scrollto value. 您可以将URL中的scrollto值作为哈希值传递,并且在两个页面的doc.ready函数中,您都可以检查URL中的scrollto值。 If found, execute a smoothscroll to that location. 如果找到,请执行平滑滚动到该位置。

Here's a simple 'hash fetching' function I use: 这是我使用的一个简单的“哈希获取”功能:

// GET URL HASH VALUES ( USE:  getUrlVars() - RETURNS ARRAY... getUrlVars()["hashName"] - RETURNS STRING  ... [ url.html?something=1&otherThing=ABC ] )
function getUrlVars(){var a={};return window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(b,c,d){a[c]=d}),a}

And in your doc.ready: 并在您的doc.ready中:

$(document).ready(function(){
  var pageGoto = getUrlVars()["scrollTo"];

  if (pageGoto){
    smoothScroller(pageGoto);
  }
});

And your links to target a location on a different page would be something like this: 您将链接定位到其他页面上的位置将是这样的:

<a href="mypage.html?scrollTo=some-content">My Link</a>

If you're looking to use ajax to append additional information on the page you can set an attribute on the page to indicate which links need to append new content prior to executing your scroll. 如果要使用ajax在页面上附加其他信息,则可以在页面上设置一个属性,以指示在执行滚动之前哪些链接需要附加新内容。 Either in bulk (there's just 1 other page with all the secondary content) or al e carte (loading only the additional content relevant to that link). 批量(只有一个页面包含所有次要内容)或单点菜单 (仅加载与该链接相关的其他内容)。

html: 的HTML:

<a href="#my-content' data-fetch="my-content.html">Goto My Content</a>

js: js:

$('a').filter('[data-fetch]').click(function(e){
  var clicked = $(e.target).closest('a')
  ,   fetch = clicked.attr('data-fetch')
  ;

  $.ajax({
    url: fetch,
    success: function(data){

      $('body').append(data);
      smoothScroller(clicked.attr('href'));

    });
  });
});

Please show us your code. 请向我们显示您的代码。 We cannot tell you for sure what is wrong if we cannot see the issue, ourselves. 如果我们自己看不到问题,我们不能肯定地告诉您出了什么问题。

My best guess would be that you are loading the content into an element that you have selected with jQuery on $(document).ready . 我最好的猜测是,您正在将内容加载到$(document).ready上使用jQuery选择的元素中。 The selector's content may not have updated after loading (use-case dependent), meaning that the JavaScript would only see the content that was originally in the selected element. 加载后,选择器的内容可能尚未更新(取决于用例),这意味着JavaScript仅会看到原来在所选元素中的内容。 As such, it would only scroll the length of the selected element's original height. 这样,它只会滚动所选元素原始高度的长度。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM