javascript设置第一次加载页面时ipad的绝对位置
javascript setting absolute position for ipad when the page loads the first time
改变iPad的方向后,绝对定位开始工作。当页面第一次加载时,没有定位。
如何使页面以正确的位置加载到正确的方向?
<script type="text/javascript">
window.onorientationchange = detectIPadOrientation;
function detectIPadOrientation() {
if (orientation == 0) {
//alert ('Portrait Mode, Home Button bottom');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "390px"
elementStyle.left = "20px";
} else if (orientation == 90) {
//alert ('Landscape Mode, Home Button right');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "470px"
elementStyle.left = "30px";
} else if (orientation == -90) {
//alert ('Landscape Mode, Home Button left');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "470px"
elementStyle.left = "30px";
} else if (orientation == 180) {
//alert ('Portrait Mode, Home Button top');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "390px"
elementStyle.left = "20px";
}
}
</script>
<div id="emkeypos">
<a href="contest.html"><img src="#httploc#/expmeridian/assets/customer/EMkey.png" width="50px"></a>
</div>
两个步骤:
-
将您的脚本(所有脚本)移动到HTML的底部,就在关闭
</body>
标记之前。这样,下载脚本就不会阻碍渲染页面;(与你的问题更相关)他们试图采取行动的元素在运行之前就会存在。更多:YUI加速网站的最佳实践 -
为您的函数添加一个调用:
detectIPadOrientation();
附带说明:不需要type
属性,默认为JavaScript。
因此(见评论):
<div id="emkeypos"><a href="contest.html"><img src="#httploc#/expmeridian/assets/customer/EMkey.png" width="50px" ></a></div>
<!-- MOVED SCRIPTS -->
<script>
window.onorientationchange = detectIPadOrientation;
detectIPadOrientation(); // <=== ADDED CALL
function detectIPadOrientation () {
if ( orientation == 0 ) {
//alert ('Portrait Mode, Home Button bottom');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "390px"
elementStyle.left = "20px";
}
else if ( orientation == 90 ) {
//alert ('Landscape Mode, Home Button right');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "470px"
elementStyle.left = "30px";
}
else if ( orientation == -90 ) {
//alert ('Landscape Mode, Home Button left');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "470px"
elementStyle.left = "30px";
}
else if ( orientation == 180 ) {
//alert ('Portrait Mode, Home Button top');
var elementStyle = document.getElementById("emkeypos").style;
elementStyle.position = "absolute";
elementStyle.top = "390px"
elementStyle.left = "20px";
}
}
</script>
</body>
</html>
相关文章:
- jquery试图按名称获取按钮位置
- 我可以获得相对于被点击元素的确切点击位置吗
- HTML5音频加载和播放获胜'我不能在iPad上工作
- 谷歌地图固定位置覆盖
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 对iPad上的点击事件反应缓慢
- 循环比赛位置算法
- es6 相当于下划线查找位置
- jQuery循环在特定位置暂停
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 自定义函数中的光标位置
- 安卓平台上的QWebView HTML5地理位置
- DIV并排,位置不正确
- 使用jQuery更改元素的顶部位置
- javascript设置第一次加载页面时ipad的绝对位置
- 地理位置代码不适用于iPhone / iPod touch / iPad
- 在 iPad 上绝对定位不会将元素设置在所需位置
- 可以't获取位置&点击iPad获取容器信息
- 如何让滑块在ipad上点击到特定位置.HTML Javascript
- jquery滚动位置在真正的Ipad上不起作用