作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
元标记“viewport”让我可以设置网站的初始比例,但如果用户随后翻转设备的方向,这会变得很奇怪。
例如,如果我将比例设置为 800x380,而用户以纵向模式打开网站,这个比例显然是错误的,当用户旋转 90 度时,网站最终更像是 1650 宽。
我如何设置视口(viewport),如果设备一开始是横向的,它是 800x380,如果它是纵向的,它是 380x800?
最佳答案
要检测方向变化,请将事件监听器附加到窗口:
window.addEventListener('orientationchange', updateOrientation, false);
在 updateOrientation 函数中,您可以检测设备的方向并相应地重置视口(viewport)属性:
function updateOrientation() {
if (!(navigator.userAgent.match(/iPhone/i)) && !(navigator.userAgent.match(/iPod/i))) {
return;
}
var viewport = document.querySelector("meta[name=viewport]");
switch (window.orientation) {
case 0: //portrait
//set the viewport attributes to whatever you want!
//For Ex : viewport.setAttribute('content', 'width=device-width, initial-scale=1.0, user-scalable=1;');
break;
case 90: case -90: //landscape
//set the viewport attributes to whatever you want!
break;
default:
//set the viewport attributes to whatever you want!
break;
}
}
关于android - 如何设置视口(viewport),使其根据方向为 380x800 或 800x380?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199495/
我正在尝试获得一个按钮,按下该按钮时会改变颜色。当再次按下时,它应该变回原来的颜色。我究竟做错了什么? 我的模板中的按钮: export default { data: {
我是一名优秀的程序员,十分优秀!