diff --git a/4chan_x.user.js b/4chan_x.user.js index 72ffc7796..808f94e1a 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -5094,19 +5094,21 @@ thumb = a.firstChild; if (thumb.hidden) { rect = a.getBoundingClientRect(); - if ($.engine === 'webkit') { - if (rect.top < 0) { - d.body.scrollTop += rect.top - 42; - } - if (rect.left < 0) { - d.body.scrollLeft += rect.left; - } - } else { - if (rect.top < 0) { - d.documentElement.scrollTop += rect.top - 42; - } - if (rect.left < 0) { - d.documentElement.scrollLeft += rect.left; + if (rect.bottom > 0) { + if ($.engine === 'webkit') { + if (rect.top < 0) { + d.body.scrollTop += rect.top - 42; + } + if (rect.left < 0) { + d.body.scrollLeft += rect.left; + } + } else { + if (rect.top < 0) { + d.documentElement.scrollTop += rect.top - 42; + } + if (rect.left < 0) { + d.documentElement.scrollLeft += rect.left; + } } } return ImageExpand.contract(thumb); diff --git a/script.coffee b/script.coffee index 3c2c86073..a82e65cc7 100644 --- a/script.coffee +++ b/script.coffee @@ -4116,12 +4116,15 @@ ImageExpand = thumb = a.firstChild if thumb.hidden rect = a.getBoundingClientRect() - if $.engine is 'webkit' - d.body.scrollTop += rect.top - 42 if rect.top < 0 - d.body.scrollLeft += rect.left if rect.left < 0 - else - d.documentElement.scrollTop += rect.top - 42 if rect.top < 0 - d.documentElement.scrollLeft += rect.left if rect.left < 0 + if rect.bottom > 0 # should be at least partially visible. + # Scroll back to the thumbnail when contracting the image + # to avoid being left miles away from the relevant post. + if $.engine is 'webkit' + d.body.scrollTop += rect.top - 42 if rect.top < 0 + d.body.scrollLeft += rect.left if rect.left < 0 + else + d.documentElement.scrollTop += rect.top - 42 if rect.top < 0 + d.documentElement.scrollLeft += rect.left if rect.left < 0 ImageExpand.contract thumb else ImageExpand.expand thumb