我正在尝试解决移动浏览器上CSS“position:fixed”属性的问题。我有一个固定的div:
<div id="logo">
...other content here...
</div>
使用CSS:
#logo{
position: fixed;
-webkit-backface-visibility: hidden;
bottom: 100px;
right: 0px;
width: 32px;
height: 32px;
}
通常情况下,div位置总是在窗口的右下角,与滚动位置无关,这是期望的行为。我的问题是,在移动浏览器上,当用户缩放页面时,超过一定的缩放级别,div位置就会出现错误(有时候div会消失在窗口之外)。
我知道fixed position在移动浏览器上支持不好,但我想知道是否有一些解决方法。我在onScroll事件上尝试了这段JS代码:
window.addEventListener('scroll', function(e){
drag.style['-webkit-transform'] = 'scale(' +window.innerWidth/document.documentElement.clientWidth + ')';\\I want to avoid zoom on this element
var r = logo.getBoundingClientRect();
var w = window.innerWidth;
var h = window.innerHeight;
if(r.right != w){
rOff = r.right - w;
logo.style.right = rOff;
}
if(r.top+132 != h){\
tOff = r.top + 132 - h;
logo.style.bottom = tOff;
}
});
很遗憾,代码似乎返回了错误的位置。
有没有人有什么提示?