简体   繁体   English

如何强制固定定位元素内的元素响应Android Web浏览器上的javascript onclick事件?

[英]How can I force elements inside a fixed positioned element respond to javascript onclick events on the Android web browser?

In the following code, the onclick event never fires when using android browser: 在以下代码中,使用Android浏览器时onclick事件永远不会触发:

<div id="__log"></div>
<div id="hud">
    <div>Hello</div>
    <div>
         <a style="border:1px solid #fff;" href="#" onclick="document.getElementById('__log').innerHTML = document.getElementById('__log').innerHTML + '<br />clicked'; return false;">Click Me</a
    </div>
</div>

Style: 样式:

#hud {
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
padding: 10px;
background:transparent url('gray95op.png') repeat;
-webkit-transform: scale(1);
-webkit-transition: all 0.25s  ease-in-out;
position: fixed;
top:10px;
left:0;
right:0;
margin:0 auto;
width: 75%;
z-index: 1001;
color: #fff;
text-align:center;
}
#hud a { color: #ccc; z-index:1002;  }

This works fine in Safari on iPhone and Chrome on desktop. 这适用于iPhone上的Safari和桌面上的Chrome。

Here is an example you can try for yourself on an Android device: http://kortina.net/android.html 以下是您可以在Android设备上自行尝试的示例: http//kortina.net/android.html

YES! 是! Of course you can use position fixed. 当然你可以使用固定的位置。 It is supported above 2.2 in Android. 在Android中支持2.2以上。 But still is buggy. 但仍然是马车。 http://kentbrewster.com/android-scroller/ shows you how. http://kentbrewster.com/android-scroller/向您展示如何。 Basically you need to disable zoom in your with 基本上你需要禁用缩放功能

<meta name="viewport" content="initial-scale=1, user-scalable=no, maximum-scale=1" />

I had the same problem that touch events were falling through position fixed. 我有同样的问题,触摸事件通过固定的位置下降。 I finally figured out why. 我终于找到了原因。 You need to make sure the parent doesn't have a webkit hack like using translate3d or translateZ. 您需要确保父级没有像使用translate3d或translateZ那样的webkit黑客。

-webkit-transform: translate3d(0, 0, 0);

Thus in my mobile web framework I do the following. 因此,在我的移动Web框架中,我执行以下操作。 I think you can read my example from my code base: 我想你可以从我的代码库中读取我的例子:

body:not([data-platform=android]) > .apps > section {-webkit-transform: translate3d(0, 0, 0);}

And there you go, now position fixed will now allow touch events, because it isn't rendered using webkit transforms. 你去了,现在固定的位置现在允许触摸事件,因为它不是使用webkit变换呈现的。

Since you are using transforms on your fixed element, that's problem what is happening. 由于您在固定元素上使用转换,因此发生了什么问题。 Try disabling any css3 animations. 尝试禁用任何css3动画。

  1. I'd favor using id 's that start with letters just in case certain browsers don't follow W3C's definition of id . 我倾向于使用以字母开头的id ,以防某些浏览器不遵循W3C对id的定义

  2. A void element, like br , may leave out the self-closing / character in HTML5 . br这样void元素可能会忽略HTML5中的自闭/字符 If this is a stylistic change, it would make more sense to adjust the element's style (CSS) (ie, height) rather than it's content (HTML). 如果这是一种风格变化,那么调整元素的样式(CSS)(即高度)而不是内容(HTML)会更有意义。

  3. Why not just use jQuery ? 为什么不直接使用jQuery It's fast, promotes Unobtrusive JavaScript , and has already worked out all the cross-browser compatibility issues related to JavaScript Event Handling . 它很快,推广了Unobtrusive JavaScript ,并且已经解决了与JavaScript事件处理相关的所有跨浏览器兼容性问题。 Also, Google optimizes the initial page load of the jQuery source (which is often already cached in the browser because so many other sites load it). 此外, Google优化了jQuery源的初始页面加载 (通常已经在浏览器中缓存,因为有很多其他站点加载它)。

    I haven't tested this, but according to the jQuery docs on .click() and the jQuery docs on .css() , it'd look something like this: 我没有测试过这个,但根据.click()上jQuery文档和.css()上jQuery文档 ,它看起来像这样:

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script> $("#__log").click(function () { $(this).css('height', "+=10px"); }); </script> 

    If that doesn't work on all modern browsers, including Android's, I'd file a jQuery bug report . 如果这不适用于所有现代浏览器,包括Android,我会提交一个jQuery错误报告

The solution is to simply attach a touch event to any non-fixed element that will pick up the touch event as it bubbles up from the fixed element. 解决方案是简单地将触摸事件附加到任何非固定元素,该元素将在从固定元素冒泡时拾取触摸事件。

For example, attaching an event to the window: 例如,将事件附加到窗口:

$(window).on('touchstart', function () {});

All touch events will propagate to the window, even from fixed position elements. 即使从固定位置元素,所有触摸事件也将传播到窗口。 This is clearly a bug fix, but it works. 这显然是一个错误修复,但它的工作原理。

I originally answered on this issue as it was filed on Github, so you can see the full report here: 我最初是在这个问题上回答的,因为它是在Github上提交的,所以你可以在这里看到完整的报告:

https://github.com/jquery/jquery-mobile/issues/3912 https://github.com/jquery/jquery-mobile/issues/3912

I'm pretty sure that the Android browser doesn't support fixed position. 我很确定Android浏览器不支持固定位置。 I don't believe mobile safari on iphone did either, until they added support in iOS 5. (some people have recreated fixed position using javascript, ie http://daringfireball.net/2009/12/pastrykit — post ios 5 this will no longer be necessary) 我不相信iphone上的移动游猎也没有,直到他们在iOS 5中添加支持。(有些人使用javascript重新创建固定位置,即http://daringfireball.net/2009/12/pastrykit - post ios 5这将不再需要)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript获取“固定”定位元素的包含块? - How can I get the containing block of a "fixed" positioned element with javascript? 如何强制浏览器向下滚动以显示“绝对”或“固定”位置的所有元素? - How can I force the browser to scroll down to show all elements with “absolute” or “fixed” positioning? 我可以在一个元素中有两个 JavaScript onclick 事件吗? - Can I have two JavaScript onclick events in one element? 如何强制定位元素与可见浏览器区域保持一致? - How to force positioned elements to stay withing viewable browser area? 我可以在一个元素中包含2个JavaScript onClick事件吗? - Can I have 2 JavaScript onClick events in a single element? 如何强制浏览器扩展程序/插件在Web应用程序内部处于非活动状态? - How can I force browser extensions/plugins to be inactive inside in my web app? 如何使用Javascript中的按钮onclick事件在div中加载php? - How can I load php in a div with button onclick events in Javascript? 如何将固定定位的元素堆叠在静态定位的元素前面? - How do I stack a fixed positioned element in front of a statically positioned element? 如何让 Django 代码中的 Javascript 监听和响应前端的点击事件? - How do I make Javascript inside Django code listen & respond to on click events on the front end? 当浏览器放大或缩小时,如何保持绝对定位的元素响应? - How can I keep an absolutely positioned element responsive when the browser is zoomed in or out?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM