Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to scroll to anchor #2562

Closed
1 task done
JoeyBling opened this issue Aug 10, 2020 · 11 comments
Closed
1 task done

Unable to scroll to anchor #2562

JoeyBling opened this issue Aug 10, 2020 · 11 comments
Labels
has PR Has a related PR type: bug Something isn't working

Comments

@JoeyBling
Copy link
Contributor

JoeyBling commented Aug 10, 2020

  • I confirm that this is an issue rather than a question.

Bug report

Steps to reproduce

  1. Create a test folder
  2. Put a README.md file in it
  3. put the following text:
## 联系方式
Content ....
## 关于我
Content ....
## 开源推荐
Content ....
  1. Launch vuepress dev
  2. Enter the URL http://localhost:8080/#关于我

What is expected?

The browser should display the page scrolled at that 关于我 title position.

What is actually happening?

No reaction, at the top

Other relevant information

The title contains non ANSI characters

Console error:

[Vue warn]: Error in nextTick: "SyntaxError: Failed to execute 'querySelector' on 'Document': '#%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F' is not a valid selector."

  • Output of npx vuepress info in my VuePress project:
  System:
    OS: Windows 10 10.0.18363
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
  Binaries:
    Node: 14.7.0 - E:\nodejs\node.EXE
    Yarn: 1.22.4 - E:\nodejs\node_global\yarn.CMD
    npm: 6.14.7 - E:\nodejs\npm.CMD
  Browsers:
    Chrome: Not Found
    Edge: Spartan (44.18362.449.0)
  npmPackages:
    @vuepress/core:  1.5.3
    @vuepress/theme-default:  1.5.3 (1.1.0)
    vuepress: ^1.5.3 => 1.5.3
  npmGlobalPackages:
    vuepress: Not Found
@dos077
Copy link

dos077 commented Aug 12, 2020

I think it has to do with scrollBehavior in core/lib/client/app.js -> createApp -> router = newRouter

It returns a selector of to.hash for scroll point. However, the hash is encodedURI, you can see it in router section of the vue debug tool. I think the selector should return decodeURI(to.hash) for satety.

@JoeyBling
Copy link
Contributor Author

@dos077 Is it more appropriate to consider using the decodeURIComponent function

@dos077
Copy link

dos077 commented Aug 12, 2020

Right, there maybe special characters used in the hash. Fixed.

@elringus
Copy link

I wonder if #2558 is related to this issue somehow. No non-ANSI characters in my case, though.

@JoeyBling
Copy link
Contributor Author

@elringus
I've tested that scrolling doesn't work even without special characters

@elringus
Copy link

Does #2566 actually fix this? scrollBehavior() doesn't seem to invoke when opening a page for the first time.

@ycs77
Copy link

ycs77 commented Aug 19, 2020

I also have the same problem, before #2566 is merged, can temporarily solve it with the following code:

If #2566 is merged and then updated to the new version

.vuepress/theme/components/Layout.vue

<script>
// ...
export default {
  // ...
  methods: {
    scrollTo(selector) {
      if (!selector || selector === '#') return
      const el = document.querySelector(decodeURIComponent(selector))
      if (el && el.offsetTop) {
        window.scrollTo(0, el.offsetTop)
      }
    }
  },
  mounted() {
    this.scrollTo(location.hash)
  }
}
</script>

@kidonng
Copy link
Contributor

kidonng commented Sep 3, 2020

Just want to share my workaround:

// enhanceApp.js
export default () => {
  if (typeof document === 'undefined') return
  document.onreadystatechange = () => {
    if (document.readyState === 'complete') {
      const { hash } = location
      const decoded = decodeURIComponent(hash)
      if (hash !== decoded) {
        document.querySelector(decoded).scrollIntoView()
      }
    }
  }
}

@billyyyyy3320
Copy link
Collaborator

Closed by #2639

@sweihub
Copy link

sweihub commented Jan 10, 2023

Just want to share my workaround:

// enhanceApp.js
export default () => {
  if (typeof document === 'undefined') return
  document.onreadystatechange = () => {
    if (document.readyState === 'complete') {
      const { hash } = location
      const decoded = decodeURIComponent(hash)
      if (hash !== decoded) {
        document.querySelector(decoded).scrollIntoView()
      }
    }
  }
}

This fixed my issue, thanks! I am wondering why the author dose not fix this long-lasting bug?

@smirnoffmv
Copy link

how can fix it for vuepress version 2.0.0-rc.7?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has PR Has a related PR type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants