모바일 크롬이 스크롤시 크기 조정 이벤트를 발생시킵니다.


79

나는 galaxy s4, android 4.2.2에서 크롬 모바일 브라우저를 사용하고 있으며 어떤 이유로 페이지를 아래로 스크롤 할 때마다 jquery.cycle2 슬라이드 쇼의 이미지 크기 조정으로 확인 된 크기 조정 이벤트가 발생합니다.

왜 이런 일이 일어나는지 아십니까?


1
크기 조정 이벤트에 문제가 발생했지만 요소 높이가 뷰포트의 100 %로 설정 되었기 때문에 스크롤시 다시 계산되었습니다.
KobeBryant 2013-06-26

답변:


48

호기심을 위해 재현하려고했는데 맞다면 내비게이션 크롬 바 때문입니다.

아래로 스크롤하고 크롬이 브라우저 내비게이션 바를 숨기면 창 크기가 조정되지만, 그 후에는 브라우저 내비게이션 바에 남은 여유 공간으로 인해 더 큰 창 크기가 생기기 때문에 이것은 정확합니다.

관련 기사 : https://developers.google.com/web/updates/2016/12/url-bar-resizing


3
의도 된 동작이든 아니든, 크기 조정 기능을 실행하지 않기를 바랍니다. 그렇다면 다른 답변이 제안한 긴 코드의 경로를 따르지 않고 여기서 해결책은 무엇입니까?
Studocwho

모바일 용 Chrome에서이 동작을 처리하는 방법에 대한 자세한 정보를 제공하는 내 답변에 대한 링크를 추가했습니다.
AlbertoFdzM

가장 간단한 방법은 뷰포트 리스너가 실행 된 후 높이와 너비를 비교하는 것입니다. 높이 만 변경된 경우 기본 브라우저 도구 모음이 뷰포트 높이에 영향을 미칠 가능성이 높습니다. 사용자가 높이에 의해서만 뷰포트 치수에 영향을 미치는 경우가 있다면 더 나아가 거리와 시간을 분석하여 뷰포트를 변경하는 사용자 입력인지 시스템 입력인지 식별 ​​할 수 있습니다.
MarsAndBack 2011

72

이상하게 들리지만 다른 브라우저에서 본 적이 있습니다. 이와 같이이 문제를 해결할 수 있습니다.

var width = $(window).width(), height = $(window).height();

그런 다음 크기 조정 이벤트 핸들러에서 할 수 있습니다.

if($(window).width() != width || $(window).height() != height){
  //Do something
}

I don't know the scope of your functions and all that, but you should get the gist from this.


5
Saved my day! I only need to check for width
Yuriy Kvartsyanyy

14
The problem is caused by the scrollbar hiding or showing, causing the height of the screen to change, hence triggering the $(window).resize(). I've suggested an edit.
Sander Koedood

3
Just wanted to add that we also observed similar behavior with iOS Safari.
Kris Krause

4
since it's the height that's changing the if condition should only check for change in width. Also the condition is not trigger until width and height both have changed. which is not always the case but almost all screen re-size involve change in width so I suggest not to use height part. if($(window).width() != width){ }
Imran Bughio

7
it should be || instead of &&
Maysam Torabi

5

I don't know is it still interesting, but My solution is : )

var document_width, document_height;

$(document).ready(function()
{
	document_width=$(document).width(); document_height=$(document).height();
    // Do something
}

$(window).resize(function()
{
    if(document_width!=$(document).width() || document_height!=$(document).height()) 
    {
        document_width=$(document).width(); document_height=$(document).height();
        // Do something
    }
}	


2
I think you should use $(window).height() not $(document).height() , and width.
Tarek El-Mallah

1

The question has already been answered, but since this question do not bring up the question of responsive sites I would like to add some information on that.

I encountered this issue in Chrome on android when developing a responsive web site. When resizing the window I want to hide the menus (due to some design elements needing proper positioning) but the Chrome for android behaviour to trigger a resize event on scroll made that somewhat difficult..

Switching to start using onOrientationChange was not an option since this is a responsive site, there is no orientation change on a desktop PC, but I still needed the code to work on both regular PC:s, tablets and smartphones.

I could had started to do browser sniffing and such but I have so far been able to avoid that..

I tried to implement the solution suggested by CWitty but since scrolling up or down in fact triggers a height-change that did not work either.

I ended up adding a condition that only hides the menu when the width is changed, not when the height has changed. This works in my case since I only need to rewrite the menu when the width is changed.


1
my suggestion would be to omit checking the height, and check only the width.
bertie

The accepted answer still applies; it's up to you to continue the logic according to your needs. Ultimately, you discovered this yourself, and this should just be a comment under the accepted answer.
MarsAndBack

1

Browsers in mobile devices often hide their horizontal top navigation bar when someone scrolls down, and show it again when scrolling up. This behavior affects the size of the client, firing the resize event. You just need to control not executing your code because of height changes or, at least, because of that height change.


0

This is a duplicate of Javascript resize event on scroll mobile

In order to fix it, use onOrientationChange and window.orientation property. See the related answer: here


Incorrect. This question is about the resize event firing unexpectedly in a mobile browser.
user2867288

5
Actually, this isn't necessarily incorrect. using onOrientationChange is a legitimate workaround since on a mobile device that's when you're most likely to see a screen resize event fire. This answer is fine. I'm not upvoting it simply because there's another case that this won't catch - and that's when browser chromes appear/disappear causing resize events (which is my problem ATM). Otherwise this would be a fine solution.
dudewad

Suppose you should catch resize events to adjust elements on screen; you open your developer tools and everything works just fine with window resizing, you can even scroll up-down! Now, you decide to open the page on a tablet, you scroll up-down and everything is re-adjusting giving earthquakes on your screen! You still need to handle window resize events though at your PC! So, this is not a correct answer!
centurian

I was using window.resize to track the orientation change. Thus facing this issue. This is a great idea.
A.M.N.Bandara

There's no proper sample code in the answer or even in the linked answer, and the external links are dead. In any case, the OP question deals with basic scrolling, with no orientation change, where a resize event is being triggered regardless of orientation. Simply checking for height change is the most efficient universal thing to do. onOrientationChange is useful elsewhere..
MarsAndBack

0

Turns out there are many things which can fire resize in various mobile browsers.

I know it's not ideal to link to an external resource, but QuirksMode has a readable table that I don't want to duplicate (or maintain) here: http://www.quirksmode.org/dom/events/resize_mobile.html

Just to give an example, the one that was getting us: apparently in many browsers, opening the soft keyboard fires the event.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.