# Scroll position issue for topic with 1 unread post

**URL:** https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683
**Category:** Bug
**Created:** [3월 10, 2018, 3:50오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683 "2018-03-10T15:50:50Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [3월 10, 2018, 3:50오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/1 "2018-03-10T15:50:51Z")

</div>

If I navigate to a topic with only 1 unread post, then the scroll position ends up about halfway down the post.

I’ve noticed this on various topics on Meta, and other forums, so I don’t think it’s topic specific. I have only noticed this on mobile (iPhone 7, latest iOS)

Here’s a video of the behaviour:

[https://global.discourse-cdn.com/meta/original/3X/4/c/4ce0c5e5b855d34d3c95a2848d4c8d265fe42e5d.MP4](https://global.discourse-cdn.com/meta/original/3X/4/c/4ce0c5e5b855d34d3c95a2848d4c8d265fe42e5d.MP4)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [3월 11, 2018, 8:59오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/2 "2018-03-11T20:59:30Z")

</div>

I believe I can repro this on mobile for all topics @tgxworld — every topic I enter I am seeing the bottom half of latest post ☹

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 12, 2018, 12:19오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/3 "2018-03-12T00:19:46Z")

</div>

Does anyone know if it happens on android too? Curious if this is a jQuery related regression. Note not seeing any issues when entering on last post on my iPhone, so somehow this is only for 1 unread.

---

<div class="post-metadata">

### Author: ![Cool\_Cool](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cool_cool/32/86967_2.png) [@Cool\_Cool](https://meta.discourse.org/u/Cool_Cool)
#### Post date: [3월 12, 2018, 12:52오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/4 "2018-03-12T00:52:35Z")

</div>

I have noticed something similar to this.  
If you don’t read all the new posts in a topic, when you visit it again it will be wear you last read all the post up to last time.  
For example-  
One day I read all 50 posts in a topic  
10 more posts are created in that topic  
I only read 8 of those new posts  
I go back to the 50 post mark

---

<div class="post-metadata">

### Author: ![Cool\_Cool](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cool_cool/32/86967_2.png) [@Cool\_Cool](https://meta.discourse.org/u/Cool_Cool)
#### Post date: [3월 12, 2018, 12:53오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/5 "2018-03-12T00:53:01Z")

</div>

I have an Android phone. I can test it for you.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [3월 12, 2018, 2:57오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/6 "2018-03-12T02:57:23Z")

</div>

Yes it does. At least on the Samsung Galaxy S7  
Google Chrome 65

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 12, 2018, 5:35오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/9 "2018-03-12T05:35:19Z")

</div>

OK I am going to have to hand this off to @eviltrout, it seems that a lot of the pain here is coming from scrollTopFor (which is calling offsetCalculator 3 times for some reason - do we know why ?)

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/offset-calculator.js.es6](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/offset-calculator.js.es6)

It is entirely possible that this is due to some internal changes in jQuery regarding `#offset` however I can oddly get reasonable results bypassing it altogether and just using this in lock on:

```plaintext
elementTop() {
    const selected = $(this.selector);
    if (selected && selected.offset && selected.offset()) {
      const result = selected.offset().top;
      return (result - $('header').outerHeight()) - 10;
      //return result - Math.round(scrollTopFor(result));
    }
  }

```

The repro of the issue happens when you make a very narrow window, it trades in percentages of height so being off a few pixels gets amplified big time on narrow displays.

So I guess my big question is, do we need to carry all this fancy or should we simply amend it to always try to place the post highlighted a few pixels below header? I get that there may be some value in displaying 1 previous post IF you can fit current post entirely on-screen, but this does feel somewhat more complex to wrangle.

Note, the regression is likely my fault and in particular jQuery changes, however I think we should revisit the basics here and decide how much fancy we need?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [3월 12, 2018, 3:16오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/11 "2018-03-12T15:16:51Z")

</div>

> [@sam](#):
>
> which is calling offsetCalculator 3 times for some reason - do we know why ?

It is super ugly, but it was the only thing that worked consistently when I wrote the code. I had many bugs with it displaying in the wrong place and noticed it worked properly if you tried a few times in a row.

> [@sam](#):
>
> So I guess my big question is, do we need to carry all this fancy or should we simply amend it to always try to place the post highlighted a few pixels below header?

The logic is used in several places, notably in the scrolling post stream to determine what post you’re currently looking at. What worries me if we replace lock-on with a different algorithm, is our eyeline tracking broken now?

Replacing this could cause a lot of regressions, particularly when the header has been customized by CSS to be larger or to contain custom content.

I have a lot on my plate right now, so my vote would be to revert jQuery for now if that’s the cause and revisit when we have more time.

---

<div class="post-metadata">

### Author: ![Slowhand](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slowhand/32/57436_2.png) [@Slowhand](https://meta.discourse.org/u/Slowhand)
#### Post date: [3월 12, 2018, 6:30오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/12 "2018-03-12T18:30:18Z")

</div>

Just to add to this, I’m using a desktop (Windows 10/Edge). Any topic I click on, I’m having this issue with the last post.

Continually have to scroll up to see the post.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [3월 12, 2018, 10:48오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/13 "2018-03-12T22:48:08Z")

</div>

I’ve noticed some weirdness with desktop Vivaldi that does not happen with desktop Chrome. When I go to a topic that has a new unread post the “top” is under the header (it is “top” but not offset to allow for the header height) and I need to scroll to see the first bit of the post. It happens here at meta, but not at SitePoint if that helps any.

EDIT  
I no sooner posted this, and it is no longer happening. in fact they are a bit low if anything. 😕

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [3월 12, 2018, 11:03오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/14 "2018-03-12T23:03:43Z")

</div>

@sam, not sure if this is related, but I’ve found that after entering a topic that has 1 unread reply, using `j` or `k` takes me to the bottom of suggested topics instead of to the post I am reading (going to the first suggested topic would be okay too, but the bottom… that’s a bit much)

---

<div class="post-metadata">

### Author: ![1101010](https://avatars.discourse-cdn.com/v4/letter/1/6de8d8/32.png) [@1101010](https://meta.discourse.org/u/1101010)
#### Post date: [3월 12, 2018, 11:15오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/15 "2018-03-12T23:15:30Z")

</div>

Just want to register that this is an ongoing issue on the Drowned in Sound boards too on mobile, being flicked to just below the post you’re notified of or to the very bottom of the last post when you open a new topic.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [3월 13, 2018, 1:47오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/16 "2018-03-13T01:47:51Z")

</div>

Did you revert this @sam? It’s quite bad the way it is now, so you either need to own the fix, or revert.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 13, 2018, 3:11오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/17 "2018-03-13T03:11:57Z")

</div>

This is fixed per:

[https://github.com/discourse/discourse/commit/8c1d145f0eaa8539a920c44537c0a64192650e1d](https://github.com/discourse/discourse/commit/8c1d145f0eaa8539a920c44537c0a64192650e1d)

This also removes the 3 times a charm hack so cause I want to see if it is actually needed, can not repro a problem with it.

The issue was actually quite straightforward `.height()` in the new jQuery returns `undefined` if you call it on a non element whereas in the past it returned 0, I guess it is more correct now, but we need to be careful.

I also corrected some calculations that incorrectly accounted for title height and normalized keyboard shortcuts and highlight so they behave in the same way.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [3월 13, 2018, 4:25오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/18 "2018-03-13T04:25:33Z")

</div>

Awesome, thank you for this .. cleaner code FTW

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [3월 13, 2018, 10:38오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/19 "2018-03-13T10:38:15Z")

</div>

> [@cpradio](#):
>
> @sam, not sure if this is related, but I’ve found that after entering a topic that has 1 unread reply, using `j` or `k` takes me to the bottom of suggested topics instead of to the post I am reading (going to the first suggested topic would be okay too, but the bottom… that’s a bit much)

For the record, this is still happening for me. It must not be related to the scroll position though, definitely happens on Desktop, Chrome v65.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 13, 2018, 11:01오전 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/20 "2018-03-13T11:01:05Z")

</div>

Yeah I have a repro of this but it is unrelated. Can you open a new topic on this.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [3월 13, 2018, 2:30오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/21 "2018-03-13T14:30:37Z")

</div>

> [@sam](#):
>
> This also removes the 3 times a charm hack so cause I want to see if it is actually needed, can not repro a problem with it.

It was definitely needed. We should keep a close eye out for regressions in the next couple of weeks just to be sure we didn’t lose something.

---

<div class="post-metadata">

### Author: ![Slowhand](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slowhand/32/57436_2.png) [@Slowhand](https://meta.discourse.org/u/Slowhand)
#### Post date: [3월 13, 2018, 4:58오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/22 "2018-03-13T16:58:19Z")

</div>

Mine is fixed! Thank you so much for your hard work! 🙃

---

<div class="post-metadata">

### Author: ![Slowhand](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slowhand/32/57436_2.png) [@Slowhand](https://meta.discourse.org/u/Slowhand)
#### Post date: [3월 13, 2018, 8:46오후 UTC](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683/23 "2018-03-13T20:46:59Z")

</div>

**^^^^**

Spoke too soon.

I’m right back where I started. Last post is completely out of the screen.

[다음 페이지](https://meta.discourse.org/t/scroll-position-issue-for-topic-with-1-unread-post/82683.md?page=2)
