简体   繁体   English

如何在 Javascript / HTML 中保持重定向的滚动位置

[英]How to keep scroll position on redirect in Javascript / HTML

I'm currently working on a mp3 player website for a local server.我目前正在为本地服务器开发 mp3 播放器网站。 It includes a "player" and a list of songs underneath.它包括一个“播放器”和下面的歌曲列表。

The issue is when redirecting to another song (either via clicking or javascript redirect), it scrolls to the top of the page.问题是当重定向到另一首歌曲时(通过单击或 javascript 重定向),它会滚动到页面顶部。 It's quite annoying when I'm far down the list and clicks on a song.当我在列表的最下方并点击一首歌曲时,这非常烦人。

It keeps the position when refreshing the page, but not with redirect.它在刷新页面时保持位置,但不使用重定向。


The url looks like this:网址如下所示:

http://192.168.1.130/music/listen.php?id=SongName

The only thing that changes during a redirect is the song name for the id.重定向期间唯一改变的是 id 的歌曲名称。

For the redirect itself, it's either a click or JavaScript that does the action.对于重定向本身,执行操作的是点击或 JavaScript。

HTML: HTML:

<tr>
    <td><a href="/music/listen.php?id=AnotherSong">AnotherSong</a></td>
    <td>11.04.2019 15:52</td>
</tr>

JavaScript JavaScript

window.location.href = "/music/listen.php?id=AnotherSong";

Are there any JavaScript / HTML features that keeps the scrolling position?是否有任何 JavaScript / HTML 功能可以保持滚动位置? Or would I maybe need to store the scrolling position somewhere and set the position again after the redirect has been done?或者我可能需要将滚动位置存储在某处并在重定向完成后再次设置位置?

Maintaining Y Offset Position when redirecting on the same page在同一页面上重定向时保持 Y 偏移位置

No javascript needed.不需要javascript。

Simply use the # anchor.只需使用#锚点。

If a page contains:如果页面包含:

<h2 id="section-heading-1">

// [... CODE HERE...]

<h2 id="section-heading-2">

// [... CODE HERE...]

<h2 id="section-heading-3">

Then the links:然后是链接:

  • <a href="#section-heading-1">
  • <a href="#section-heading-2">
  • <a href="#section-heading-3">

Will link to the various <h2> section headings.将链接到各种<h2>部分标题。


Maintaining Y Offset Position when redirecting to another page重定向到另一个页面时保持 Y 偏移位置

One approach would be to use localStorage .一种方法是使用localStorage

You can apply an onscroll event to the window (with throttling) and repeatedly update the y-offset value in localStorage .您可以将onscroll事件应用于window (带限制)并重复更新localStorage中的y-offset值。

Then, whenever a new page loads, you can have the browser refer to localStorage and, if it finds a y-offset value, adjust the page scroll within the browser viewport.然后,每当加载新页面时,您可以让浏览器引用localStorage ,如果找到y-offset值,则在浏览器视口内调整页面滚动。


Working Example:工作示例:

// ORIGIN PAGE
// ===========

// Save Y Offset Position to localStorage
const recordVerticalOffset = () => {

  localStorage.setItem('pageVerticalPosition', window.scrollY);
}

// Only save window position after scrolling stops
const throttleScroll = (delay) => {

  let time = Date.now();

  const checkScroll = setInterval(() => {

    if (Date.now() > (time + delay)) {

      clearInterval(checkScroll);
      return recordVerticalOffset();
    }
  }, 300);
}

// Scroll Event Listener
window.addEventListener('scroll', throttleScroll(1000));


// DESTINATION PAGE
// ================

const repositionPage = () => {

  let pageVerticalPosition = localStorage.getItem('pageVerticalPosition') || 0;

  window.scrollTo(0, pageVerticalPosition);
}

window.addEventListener('load', repositionPage);

You can use jQuerys.animate(), .offset() and scrollTop.您可以使用 jQuerys.animate()、.offset() 和 scrollTop。 Like

$(document.body).animate({
'scrollTop':   $('#anchorName').offset().top}, 2000);

example link: http://jsbin.com/unasi3/edit示例链接:http: //jsbin.com/unasi3/edit

If you don't want to animate use.scrollTop() like如果你不想像 use.scrollTop() 这样的动画

$(document.body).scrollTop($('#anchorName').offset().top);

or javascripts native location.hash like或javascripts native location.hash like

location.hash = '#' + anchorid;

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

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