jetpack icon indicating copy to clipboard operation
jetpack copied to clipboard

Carousel: closing carousel view brings you back to the top of the page

Open jeherve opened this issue 12 years ago • 47 comments

Steps to reproduce:

  1. Open a page with a gallery
  2. Scroll down, and click on one of the images
  3. The Carousel view appears
  4. Close the carousel view: you're back at the top of the page.

It'd be nice if closing the Carousel view brought you back to where you were on the page before you opened Carousel.

Suggested in 1924086-t

jeherve avatar Sep 22 '14 18:09 jeherve

Sorry, but If I may ask are you talking about focus going back to the top? I can see that, but actual view isn't moving back to the top of the page for me (unless I start tabbing afterwards of course).

chrisdc avatar Sep 22 '14 21:09 chrisdc

The actual view moves back to the top of the page for me, on 3 different test sites of mine.

jeherve avatar Sep 22 '14 21:09 jeherve

Haven't got it yet sorry. Don't suppose it could be browser specific? (tested Chrome and Firefox in Windows 7)

chrisdc avatar Sep 22 '14 22:09 chrisdc

Behaviour seems to change depending on the theme you use, but I can't figure out what's causing the issue.

jeherve avatar Sep 24 '14 08:09 jeherve

Not sure if I'll be able to get it or not, but I'd like to have a go. Is there a publicly available theme where you are experiencing the jump to top? On 24 Sep 2014 09:55, "Jeremy Herve" [email protected] wrote:

Behaviour seems to change depending on the theme you use, but I can't figure out what's causing the issue.

— Reply to this email directly or view it on GitHub https://github.com/Automattic/jetpack/issues/1125#issuecomment-56642386.

chrisdc avatar Sep 24 '14 09:09 chrisdc

You can reproduce the problem on all posts on this site: http://tsadri.hu/en/en-route-uton/

jeherve avatar Sep 24 '14 09:09 jeherve

I don't quite get why yet, but acting on a hunch it seems to go away if you take height: 100% off the body tag in your css.

chrisdc avatar Sep 24 '14 09:09 chrisdc

I can confirm the height: 100% solution for this issue. In a custom theme of my own, the view is back to top if height: 100% is specified for the body element and it does not when it isn't.

Luegge avatar Nov 06 '14 12:11 Luegge

Reported in #2296217-t

csonnek avatar Aug 10 '15 20:08 csonnek

I have just spent hours debugging this issue, until I came here. I do not have a definite explanation, but let me summarize my findings. First of all, all these are related:

  • https://wordpress.org/support/topic/carousel-scrolls-back-to-top-of-page?replies=2
  • https://github.com/jquery/jquery/issues/2215
  • http://stackoverflow.com/questions/14611591/window-scrolls-top-after-wrapper-gets-overflowhidden-via-jquery
  • http://greensock.com/forums/topic/11336-html-overflow-hidden-preventing-javascript-scrolling-in-parallax-application/
  • https://forum.jquery.com/topic/animate-scrolltop-blocked-by-overflow-hidden-in-chrome
  • https://github.com/kswedberg/jquery-smooth-scroll/issues/53

The issue seems to be depending on the exact combination of browser (only Chrome has been affected in my tests) and the body/html height and width and overflow setting. The issue also seems to be subject to "timing", because slowly stepping through the JS code via breakpointing "fixed" the issue for me, sometimes.

In my special case, I am using the Brooklyn theme (quite established!) together with the Jetpack Carousel, and the page scrolls to the top after having closed the lightbox/overlay. Breakpointing has shown me that the following lines in jetpack/blob/master/modules/carousel/jetpack-carousel.js are not working reliably:

// make sure to stop the page from scrolling behind the carousel overlay, so we don't trigger
// infiniscroll for it when enabled (Reader, theme infiniscroll, etc).
originalOverflow = $('body').css('overflow');
$('body').css('overflow', 'hidden');
// prevent html from overflowing on some of the new themes.
originalHOverflow = $('html').css('overflow');
$('html').css('overflow', 'hidden');

Debug output via

var scroll = $(window).scrollTop();
console.log("scroll position:", scroll);

before and after the mentioned code block has shown that the quoted code lines can reset the scroll position to 0 (top).

From the comments in this code one can already infer that this method is a little "flaky" and maybe (w/o offense) not perfectly thought-through. At least the comments do not explain why this should work in all cases. The comments also suggest that the body change has been implemented at first, and then the html change has been added to fix something later.

Now, it looks like working on both, html and body has added the unreliability on Chrome, especially considering https://github.com/jquery/jquery/issues/2215 and https://github.com/kswedberg/jquery-smooth-scroll/issues/53

A quote:

But this bug occurs only if the html and the body have the overflow-x, so you can just remove the overflow-x for the html and it's okay for you I think.

Considering a thread on StackOverflow about preventing page scrolling, one can infer that the topic is rather complex and that the safest method is to actually, temporarily, store the scroll position:

http://stackoverflow.com/a/3656618

Considering the references above and the discussion in this thread here so far, it seems clear that the body element CSS is critical. In my specific scenario, the website behaves properly if only one of $('body').css('overflow', 'hidden'); and $('html').css('overflow', 'hidden'); is used.

So, outcommenting either the one or the other fixes the problem for my scenario.

I think this piece of code should be designed more reliably. It should be compatible with as many themes as possible out-of-the-box, and I think we do not want to require the theme to not have a 100 % height setting for the body.

There are two options now I think:

  • only use $('html').css('overflow', 'hidden'); -- would that be enough in all cases? Can we reliably test this?
  • temporarily store the scroll position as proposed in the SO thread, and restore it in the close handler.

Feedback welcome! I hope someone can come up with the perfect explanation and a conceptually safe solution.

Cheers!

jgehrcke avatar Aug 19 '15 18:08 jgehrcke

I've been troubleshooting my theme for hours looking for a solution to the scroll to top on click.

I found the solution recommended here actually conflicts with our theme and makes it scroll to the top now. Is there a way around this?

// make sure to stop the page from scrolling behind the carousel overlay, so we don't trigger
// infiniscroll for it when enabled (Reader, theme infiniscroll, etc).
originalOverflow = $('body').css('overflow');
$('body').css('overflow', 'hidden');
// prevent html from overflowing on some of the new themes.
originalHOverflow = $('html').css('overflow');
$('html').css('overflow', 'hidden');

Example site that has the issue - http://cpteam.bypronto.com/examples/image-galleries/tiled-gallery/

alexphelps avatar Oct 22 '15 06:10 alexphelps

Hello guys,

I don't know why, with the last update of JetPack, I got the same problem of going back to top when clicking the close button.

I tried to downgrade JetPack, disable all my plugins, switch to the Twenty Fifteen theme... nothing works. I still had the issue. Maybe I think a problem with the last release of JQuery !? I don't know.

So, I read you different ideas and this is what I did to solve the problem :

line 484 :

just after

            originalOverflow = $('body').css('overflow');
            $('body').css('overflow', 'hidden');
            // prevent html from overflowing on some of the new themes.
            originalHOverflow = $('html').css('overflow');
            $('html').css('overflow', 'hidden');

add this

            // Save the scroll top position and track opening
            console.log('open !');
            scrollPos = $(window).scrollTop();
            console.log("scroll position:", scrollPos);

line 538 :

just after

            return container
                .trigger('jp_carousel.beforeClose')
                .fadeOut('fast', function(){
                    container.trigger('jp_carousel.afterClose');

add this

                    // After close, set again the scroll top position 
                    $(window).scrollTop(scrollPos);
                    console.log('close final !');

It works fine for me now. I'm just not relax with the idea to change my js JetPack file.

MisterWP avatar Nov 05 '15 18:11 MisterWP

This issue seems worse in Jetpack 3.8, as an extra # is added to the URL when you close the Carousel view. It brings you back to the top of the page every time.

This could be related to #2719.

jeherve avatar Nov 17 '15 16:11 jeherve

Reported here: https://wordpress.org/support/topic/since-latest-update-closing-gallery-goes-back-to-top-of-post?replies=2&view=all

jeherve avatar Nov 17 '15 16:11 jeherve

@jeherve, fwiw js history.pushState is not supported in < IE10, so this (leaving the hash, and probably the scrolling to top) was still happening in those browsers. When we removed that, it re-broke it for current browsers, which is what you're seeing.

This fix will work for all browsers. and I can add in the history check to remove the hash from browsers that are able to.

@MisterWP can you check out #3033?

dereksmart avatar Nov 18 '15 09:11 dereksmart

I'm a little puzzled, apparently this issue has been fixed? But its still very much there for me. I checked #3033, and the changes made there are already present in my version of jetpack-carousel.js. Am I missing something?

zehawki avatar Apr 06 '16 12:04 zehawki

@zehawki what browser are you experiencing this problem in?

dereksmart avatar Apr 06 '16 13:04 dereksmart

@dereksmart Pretty much any device, any browser. Tested on Chrome, FF and Opera on desktop, laptop (both Win 10) and mobile (Android). (E.g. http://zehawk.in/blog/travel/see-the-golden-temple-in-a-new-light/)

zehawki avatar Apr 06 '16 16:04 zehawki

@zehawki hm might be a windows thing. we will do some more testing and will get back to you -- Thanks for the report!

dereksmart avatar Apr 06 '16 20:04 dereksmart

Hrm. Well, what I am seeing on Windows10/Edge and Windows10/Chrome is that when I close the overlay, the page scrolls not to the position it was at before the overlay opened, but rather to maybe 20px of the thumbnail visible at the bottom of the current scroll position -- so there definitely is a change.

Perhaps for images closer to the top of the page, it's scrolling all the way back up, and that could be leading to the confusion?

georgestephanis avatar Apr 07 '16 14:04 georgestephanis