简体   繁体   English

使用客户端路由时,如何在页面内实现链接?

[英]How can I implement links within a page, while using Client-side routing?

I'm working on an SPA, using crossroads.js for the client-side routing. 我正在使用crossroads.js进行客户端路由的SPA。 Everything is working perfectly, with one exception. 除了一个例外,一切都工作正常。

I have one page within this SPA that is quite large, and I'd like to be able to use internal links, to provide a table of contents at the top. 我在此SPA中有一个很大的页面,并且我希望能够使用内部链接在顶部提供一个目录。 In the past, I've done something like this: 过去,我做过这样的事情:

<ul>
  <li><a href="#Introduction">Introduction</a></li>
  <li><a href="#Chap1">Chapter 1</a></li>
  <li><a href="#Chap2">Chapter 2</a></li>
  <li><a href="#Chap3">Chapter 3</a></li>
</ul>

These would link to elements within the same page with the corresponding ID. 这些将链接到同一页面中具有相应ID的元素。

However, now that I'm using client-side routing, this doesn't seem work work as well. 但是,由于我正在使用客户端路由,因此似乎也无法正常工作。 The page that this I'm using this on has a URL like: 我正在使用此页面的页面具有如下网址:

http://myserver.com/#/Books/12/Full

Clicking one of the links above does move the page to the correct location on screen, but it changes the URL to: 单击上面的链接之一不会将页面移至屏幕上的正确位置,但会将URL更改为:

http://myserver.com/#Chap2

Is there a standard way of handling internal links in an SPA, while preserving the URL? 在保留URL的同时,是否存在处理SPA中内部链接的标准方法?

Here's a FIDDLE 这是一块

<nav>
  <ul>
    <li data-rel="div1">DIV 1</li>
    <li data-rel="div2">DIV 2</li>
    <li data-rel="div3">DIV 3</li>
  </ul>
</nav>


<div id="div1">


</div>

<div id="div2">


</div>

<div id="div3">


</div>


$(function() {
  $('nav ul li').click(function() {
    $('html, body').animate({ scrollTop: $('#'+$(this).data('rel')).offset().top }, 900); 
  });
});

@mdesdev got me pointed in the right direction, but here's what ultimately wound up working for me: @mdesdev让我指出了正确的方向,但这最终为我工作了:

Using an SPA, the page in question is loaded after the document is loading, so I couldn't get using the jQuery document ready function to add the click handler to work. 使用SPA,有问题的页面会在文档加载后加载,因此我无法使用jQuery文档就绪功能来添加点击处理程序。 Instead, I created the following function: 相反,我创建了以下函数:

function scrollToInternal(target) {
  // #MyContent is the div that needs to be scrolled
  $('#MyContent').scrollTop($(target).position().top);
};

Then in my markup, I called this function with the onclick attribute: 然后在标记中,我使用onclick属性调用了此函数:

<ul>
  <li><a href="#" onclick="scrollToInternal('#Introduction');return false">Introduction</a></li>
  <li><a href="#" onclick="scrollToInternal('#Chap1');return false">Chapter 1</a></li>
  <li><a href="#" onclick="scrollToInternal('#Chap2');return false">Chapter 2</a></li>
  <li><a href="#" onclick="scrollToInternal('#Chap3');return false">Chapter 3</a></li>
</ul>

Not as elegant a solution as mdesdev's, but it's working. 没有mdesdev的解决方案那么优雅,但是它正在工作。

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

相关问题 我可以将参数传递给客户端HTML页面吗? - Can I pass parameters to a client-side HTML page? 如何更改客户端网页的字体? - How can I change the font-face of a web page that is on the client-side already? 如何使用browserify在客户端html文档中使用Node.js包 - How to use Node.js packages within a client-side html document using browserify 使用 document.location.href 保存客户端数据时,如何向浏览器建议文件名? - When using document.location.href to save client-side data, how can I suggest a filename to the browser? 在HTML页面中使用VBScript在客户端打印.msg文件 - Printing .msg files client-side using VBScript in a HTML page 我可以使用/包含 HTML 部分,客户端,使用 EJS(或类似的)吗? - Can I use/include HTML partials, client-side, using EJS (or similar)? 能否使内部链接在客户端生成的HTML页面中起作用? - Can one make internal links work within a client-side-generated HTML page? 如何隐藏/保护我的客户端 HTML/JS 代码? - How can I hide/protect my client-side HTML/JS code? 如何使客户端脚本响应电子表格上的点击? - How can I make a client-side script respond to a click on a spreadsheet? 如何保存带有文本输入的仅客户端网页? - How can I save a client-side only webpage with text inputs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM