【问题标题】:Obtaining scroll position on mobile phone/device获取手机/设备上的滚动位置
【发布时间】:2019-03-28 12:54:47
【问题描述】:

我想让移动设备滚动到某个位置,然后将其作为警报输出。我的代码在我的 PC 上的 chrome 中运行良好,但在移动设备上运行良好。当我在我的 PC 上运行 chrome 中的开发人员工具并在移动设备上选择视图时,它不起作用,即使它在 PC 视图模式下工作正常。如果我将位置设置为 30 像素,则在移动设备视图中屏幕根本不会移动,并且警报返回为零。我做错了什么?

<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,400i,300,700" rel="stylesheet" type="text/css">
<script src="crafty/crafty_postcode.class.js"></script> 
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>    
<script type="text/javascript" src="https://cc-cdn.com/generic/scripts/v1/cc_c2a.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> 
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> 
</script>
</head>
<body id="mainBodyId" style="margin-left:0.25cm;height: 1500px;width:1200px;"  >'

<script  language="JavaScript" type="text/javascript"> 
window.scrollTo(0,30);
alert('Current scroll from the top: ' + window.pageYOffset);
</script>

<form>
<span>
    1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4
    <br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7
    <button>test</button>
    <br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>
    1<br>2<br>3<br>4<br>5<br>6<br>6<br>
    <button>test</button>
    1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>
    5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>
    8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>
    2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>
    <button>test</button>
    <br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3
    <br>4<br>5<br>
    <button>test</button>
    6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>
    9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>
    3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6
    <br>7<br>8<br>9
    <button>test</button>
    <br>10<br>1<br>2<br>3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>1<br>2<br>
    3<br>4<br>5<br>6<br>6<br>7<br>8<br>9<br>10<br>
    <button>test</button>
</span>
</form>
</body>
</html>

我希望屏幕垂直移动到 30 像素,并在 chrome 开发人员工具、移动设备视图中输出 30 的警报。

【问题讨论】:

    标签: javascript mobile scroll


    【解决方案1】:

    试试这个而不是window.scrollTo(0, 30)

    window.addEventListener('load', function(){
       window.scrollTo(0,30);
    });
    

    【讨论】:

    • 您的建议适用于桌面视图模式(在开发人员工具中),但当我切换到在移动设备模式下查看时则无效。
    • 嘿@JamesCurrie 我已经更新了代码,它也可以在移动设备上运行。我不确定为什么它不适用于 chorme 移动视图,但我在实际的移动设备上对其进行了测试,它确实有效。
    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多