Skip to content

Commit eb906e8

Browse files
committed
Ot_popup: improve hack for preventing background scroll when a popup is open on mobile devices
Now setting position:fixed and manually change top property
1 parent 52e0b83 commit eb906e8

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

css/ot_popup.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
html.ot-with-popup, html.ot-with-popup > body {
2+
overflow: hidden;
3+
position: fixed ; /* To prevent scroll on mobile devices.
4+
One must also set top property manually
5+
to prevent scrolling page to top. */
6+
}
17
.ot-popup-background {
28
position: fixed ;
39
right: 0 ;

src/widgets/ot_popup.eliom

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ let%client popup
3838
?(onclose = fun () -> Lwt.return_unit)
3939
?(close_on_background_click=false)
4040
?(close_on_escape=close_button <> None)
41-
?(ios_scroll_pos_fix=true)
4241
gen_content =
4342
let a = (a :> Html_types.div_attrib attrib list) in
4443
let gen_content =
@@ -59,18 +58,20 @@ let%client popup
5958
| None -> ()
6059
in
6160

62-
if ios_scroll_pos_fix then scroll_pos := Dom_html.document##.body##.scrollTop;
61+
(* Hack to prevent body scrolling behind the popup on mobile devices: *)
62+
scroll_pos := Dom_html.document##.body##.scrollTop;
6363
html_ManipClass_add "ot-with-popup";
64-
if ios_scroll_pos_fix then Dom_html.document##.body##.scrollTop := !scroll_pos;
64+
Dom_html.document##.body##.style##.top :=
65+
Js.string (Printf.sprintf "%dpx" (- !scroll_pos));
6566

6667
let kill_keydown_thread = ref @@ fun () -> () in
6768

6869
let do_close () =
6970
if (Dom_html.document##getElementsByClassName (Js.string "ot-popup"))
7071
##.length = 1 then begin
7172
html_ManipClass_remove "ot-with-popup";
72-
if ios_scroll_pos_fix then
73-
Dom_html.document##.body##.scrollTop := !scroll_pos
73+
Dom_html.document##.body##.style##.top := Js.string "";
74+
Dom_html.window##scroll 0 !scroll_pos
7475
end;
7576
let () = Eliom_lib.Option.iter Manip.removeSelf !popup in
7677
!kill_keydown_thread ();

src/widgets/ot_popup.eliomi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ val popup :
7171
-> ?onclose:(unit -> unit Lwt.t)
7272
-> ?close_on_background_click:bool
7373
-> ?close_on_escape:bool
74-
-> ?ios_scroll_pos_fix:bool
7574
-> ((unit -> unit Lwt.t) -> [< div_content ] elt Lwt.t)
7675
-> [> `Div ] elt Lwt.t
7776

0 commit comments

Comments
 (0)