12

on my mobile view of my webpage i can scroll in vertical and horizontal direction, but this starts always at the top left corner. Now i want to set the viewport to my custom position with window.scrollTo or something equivalent. window.scroll seem just to work for desktop browser.

any idea how i could solve that?

Safari
  • 3,302
  • 9
  • 45
  • 64
  • Please be more specific as to what your issue is. `window.scrollTo(x, y);` works perfectly fine if your content is large enough. – Torsten Walter Aug 07 '12 at 11:54
  • My dom looks like this
    now i can scroll the within the image in mobile and desktop browsern. but when i what a different startposition, where i look at the image i try to use window. scrollTo(x, y); but this works only in the desktop not in the mobil browser.
    – Safari Aug 07 '12 at 11:59
  • Please provide a test case complete with html, css and JavaScript. Without knowing how large your image or how the dimensions of your content are, there is no possibility to gauge where the problem is. – Torsten Walter Aug 07 '12 at 12:07

2 Answers2

6

I got it finally working.

i had to use additionally the setTimeout function

setTimeout(window.scrollTo(x,y),100);
Safari
  • 3,302
  • 9
  • 45
  • 64
  • 2
    You should use Events instead of timeouts. for example Event.observe(window, 'load', function() { window.scrollTo(x,x); }) – Pawel Dubiel Nov 11 '13 at 13:31
  • 3
    I don't think your code is right, in fact window.scrollTo will not be called after 100ms but right away. You should use the following: ```JavaScriptsetTimeout(function () { window.scrollTo(x, y); }, 100);``` – Eric Villemure Jun 22 '17 at 12:17
0

As I said in my comment, this works if your content is large enough. This means larger than the viewport. Without knowing specifics, did you look into setting the viewport meta tag?

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Now, if your content div or image or whatever exceeds the size of the viewport width (320 dips on iPhone) you can scroll on the x axis. The same is true for the y axis with different values though.

Torsten Walter
  • 5,614
  • 23
  • 26
  • i already have this meta tag for the viewport the images i use are about 800x1000 px big. so they are bigger then the viewport, but the function scrollTo is still not working – Safari Aug 07 '12 at 12:09
  • See, it's difficult to guess if you don't give the complete context ;) – Torsten Walter Aug 07 '12 at 12:10
  • ok. see. when i want to see my pic zoomed within my webpage, then i load the big version of the big and then i want to the position of the pic, where the user tabed in before. the values of the point are correct. so i use: window.scrollTo(point.x, point.y); within the InitZoom function. – Safari Aug 07 '12 at 12:14