- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
问题出在事件“visibilitychange”的行为上。
触发: - 当我切换到浏览器窗口内的不同选项卡时。
(没关系)
未触发: - 当我使用 ALT+TAB 切换到不同的窗口/程序时。
(这应该触发,因为就像最小化时一样,窗口的可见性可能会改变)
W3 页面可见性 API 文档:http://www.w3.org/TR/page-visibility/
规范表中没有关于 ALT+TAB/程序切换的“页面可见性”定义。我猜它在操作系统和浏览器之间有一些关系。
是否有解决此问题的解决方法?实现相当简单,我使用 jQuery 监听“visibilitychange”事件,然后在其回调中检查“document.visibilityState”的值,但问题是事件没有按预期触发。
$(document).on('visibilitychange', function() {
if(document.visibilityState == 'hidden') {
// page is hidden
} else {
// page is visible
}
});
这也可以在没有 jQuery 的情况下完成,但是 ALT+TAB 和任务栏切换隐藏/显示预期行为仍然缺失:
if(document.addEventListener){
document.addEventListener("visibilitychange", function() {
// check for page visibility
});
}
我也试过 ifvisible.js 模块 ( https://github.com/serkanyersen/ifvisible.js ),但行为是一样的。
ifvisible.on('blur', function() {
// page is hidden
});
ifvisible.on('focus', function() {
// page is visible
});
我还没有在其他浏览器中测试过,因为如果我不能让它在 Windows 上的 Chrome 中工作,我真的不关心其他浏览器。
有什么帮助或建议吗?
我尝试为事件名称使用不同的 vendor 前缀(visibilitychange、webkitvisibilitychange、mozvisibilitychange、msvisibilitychange),但是当我切换到任务栏中的不同程序或 ALT+ 时,事件仍然没有触发TAB,或者即使我使用覆盖整个屏幕的 Windows 键打开 Windows 中的开始菜单。
我可以在 Chrome、Firefox 和 Internet Explorer 中重现完全相同的问题。
Here's我为这个问题写的一篇综述文章和一个纯 Javascript 解决方法来解决遇到的问题。
编辑以包含来源博客文章的副本。 (见接受的答案)
最佳答案
Here's我为这个问题写的一篇综述文章和一个纯 JavaScript 的变通方法来解决遇到的问题。
编辑以包含来源博客文章的副本:
In any kind of javascript application we develop there may be afeature or any change in the application which reacts according to thecurrent user visibility state, this could be to pause a playing videowhen the user ALT+TABs to a different window, tracking stats about howthe users interact with our application, how often does him switch toa different tab, how long does it take him to return and a lot ofperformance improvements that can benefit from this kind of API.
The Page Visibility API provides us with two top-level attributes:document.hidden (boolean) and document.visibilityState (which could beany of these strings: “hidden”, “visible”, “prerender”, “unloaded”).This would not be not good enough without an event we could listen tothough, that’s why the API also provides the useful visibilitychangeevent.
So, here’s a basic example on how we could take action on a visibilitychange:
function handleVisibilityChange() {
if(document.hidden) {
// the page is hidden
} else {
// the page is visible
}
}
document.addEventListener("visibilitychange", handleVisibilityChange, false);We could also check for document.visibilityState value.
Dealing with vendor issues George Berkeley by John Smibert
Some of the implementations on some browsers still need that theattributes or even the event name is vendor-prefixed, this means wemay need to listen to the msvisibilitychange event or check for thedocument.webkitHidden or the document.mozHidden attributes. In orderto do so, we should check if any vendor-prefixed attribute is set, andonce we know which one is the one used in the current browser (only ifthere’s the need for a prefix), we can name the event and attributesproperly.
Here’s an example approach on how to handle these prefixes:
var browserPrefixes = ['moz', 'ms', 'o', 'webkit'];
// get the correct attribute name
function getHiddenPropertyName(prefix) {
return (prefix ? prefix + 'Hidden' : 'hidden');
}
// get the correct event name
function getVisibilityEvent(prefix) {
return (prefix ? prefix : '') + 'visibilitychange';
}
// get current browser vendor prefix
function getBrowserPrefix() {
for (var i = 0; i < browserPrefixes.length; i++) {
if(getHiddenPropertyName(browserPrefixes[i]) in document) {
// return vendor prefix
return browserPrefixes[i];
}
}
// no vendor prefix needed
return null;
}
// bind and handle events
var browserPrefix = getBrowserPrefix();
function handleVisibilityChange() {
if(document[getHiddenPropertyName(browserPrefix )]) {
// the page is hidden
console.log('hidden');
} else {
// the page is visible
console.log('visible');
}
}
document.addEventListener(getVisibilityEvent(browserPrefix), handleVisibilityChange, false);Other issues There is a challenging issue around the “Page Visibility”definition: how to determine if the application is visible or not ifthe window focus is lost for another window, but not the actualvisibility on the screen? what about different kinds of visibilitylost, like ALT+TAB, WIN/MAC key (start menu / dash), taskbar/dockactions, WIN+L (lock screen), window minimize, window close, tabswitching. What about the behaviour on mobile devices?
There’s lots of ways in which we may lose or gain visibility and a lotof possible interactions between the browser and the OS, therefore Idon’t think there’s a proper and complete “visible page” definition inthe W3C spec. This is the definition we get for the document.hiddenattribute:
HIDDEN ATTRIBUTE On getting, the hidden attribute MUST return true ifthe Document contained by the top level browsing context (root windowin the browser’s viewport) [HTML5] is not visible at all. Theattribute MUST return false if the Document contained by the top levelbrowsing context is at least partially visible on at least one screen.
If the defaultView of the Document is null, on getting, the hiddenattribute MUST return true.
To accommodate accessibility tools that are typically full screen butstill show a view of the page, when applicable, this attribute MAYreturn false when the User Agent is not minimized but is fullyobscured by other applications.
I’ve found several inconsistencies on when the event is actuallyfired, for example (Chrome 41.0.2272.101 m, on Windows 8.1) the eventis not fired when I ALT+TAB to a different window/program nor when IALT+TAB again to return, but it IS fired if I CTRL+TAB and thenCTRL+SHIFT+TAB to switch between browser tabs. It’s also fired when Iclick on the minimize button, but it’s not fired if the window is notmaximized and I click my editor window which is behing the browserwindow. So the behaviour of this API and it’s differentimplementations are still obscure.
A workaround for this, is to compensate taking advantage of the betterimplemented focus and blur events, and making a custom approach to thewhole “Page Visibility” issue using an internal flag to preventmultiple executions, this is what I’ve come up with:
var browserPrefixes = ['moz', 'ms', 'o', 'webkit'],
isVisible = true; // internal flag, defaults to true
// get the correct attribute name
function getHiddenPropertyName(prefix) {
return (prefix ? prefix + 'Hidden' : 'hidden');
}
// get the correct event name
function getVisibilityEvent(prefix) {
return (prefix ? prefix : '') + 'visibilitychange';
}
// get current browser vendor prefix
function getBrowserPrefix() {
for (var i = 0; i < browserPrefixes.length; i++) {
if(getHiddenPropertyName(browserPrefixes[i]) in document) {
// return vendor prefix
return browserPrefixes[i];
}
}
// no vendor prefix needed
return null;
}
// bind and handle events
var browserPrefix = getBrowserPrefix(),
hiddenPropertyName = getHiddenPropertyName(browserPrefix),
visibilityEventName = getVisibilityEvent(browserPrefix);
function onVisible() {
// prevent double execution
if(isVisible) {
return;
}
// change flag value
isVisible = true;
console.log('visible}
function onHidden() {
// prevent double execution
if(!isVisible) {
return;
}
// change flag value
isVisible = false;
console.log('hidden}
function handleVisibilityChange(forcedFlag) {
// forcedFlag is a boolean when this event handler is triggered by a
// focus or blur eventotherwise it's an Event object
if(typeof forcedFlag === "boolean") {
if(forcedFlag) {
return onVisible();
}
return onHidden();
}
if(document[hiddenPropertyName]) {
return onHidden();
}
return onVisible();
}
document.addEventListener(visibilityEventName, handleVisibilityChange, false);
// extra event listeners for better behaviour
document.addEventListener('focus', function() {
handleVisibilityChange(true);
}, false);
document.addEventListener('blur', function() {
handleVisibilityChange(false);
}, false);
window.addEventListener('focus', function() {
handleVisibilityChange(true);
}, false);
window.addEventListener('blur', function() {
handleVisibilityChange(false);
}, false);I welcome any feedback on this workaround. Some other great sourcesfor ideas on this subject:
Using the Page Visibility API Using PC Hardware more efficiently inHTML5: New Web Performance APIs, Part 2 Introduction to the PageVisibility API Conclusion The technologies of the web are continuouslyevolving, we’re still recovering from a dark past where tables wherethe markup king, where semantics didn’t mattered, and they weren’t anystandards around how a browser should render a page.
It’s important we push these new standards forward, but sometimes ourdevelopment requirements make us still need to adapt to these kind oftransitions, by handling vendor prefixes, testing in differentbrowsers and differents OSs or depend on third-party tools to properlyidentify this differences.
We can only hope for a future where the W3C specifications arestrictly revised, strictly implemented by the browser developer teams,and maybe one day we will have a common standard for all of us to workwith.
As for the Page Visibility API let’s just kinda cite George Berkeleyand say that:
“being visible” is being perceived.
关于javascript - 使用 ALT+TAB 切换程序/窗口或单击任务栏时不会触发 visibilitychange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28993157/
我是 C 语言新手,我编写了这个 C 程序,让用户输入一年中的某一天,作为返回,程序将输出月份以及该月的哪一天。该程序运行良好,但我现在想简化该程序。我知道我需要一个循环,但我不知道如何去做。这是程序
我一直在努力找出我的代码有什么问题。这个想法是创建一个小的画图程序,并有红色、绿色、蓝色和清除按钮。我有我能想到的一切让它工作,但无法弄清楚代码有什么问题。程序打开,然后立即关闭。 import ja
我想安装screen,但是接下来我应该做什么? $ brew search screen imgur-screenshot screen
我有一个在服务器端工作的 UDP 套接字应用程序。为了测试服务器端,我编写了一个简单的 python 客户端程序,它发送消息“hello world how are you”。服务器随后应接收消息,将
我有一个 shell 脚本,它运行一个 Python 程序来预处理一些数据,然后运行一个 R 程序来执行一些长时间运行的任务。我正在学习使用 Docker 并且我一直在运行 FROM r-base:l
在 Linux 中。我有一个 c 程序,它读取一个 2048 字节的文本文件作为输入。我想从 Python 脚本启动 c 程序。我希望 Python 脚本将文本字符串作为参数传递给 c 程序,而不是将
前言 最近开始整理笔记里的库存草稿,本文是 23 年 5 月创建的了(因为中途转移到 onedrive,可能还不止) 网页调起电脑程序是经常用到的场景,比如百度网盘下载,加入 QQ 群之类的 我
对于一个类,我被要求编写一个 VHDL 程序,该程序接受两个整数输入 A 和 B,并用 A+B 替换 A,用 A-B 替换 B。我编写了以下程序和测试平台。它完成了实现和行为语法检查,但它不会模拟。尽
module Algorithm where import System.Random import Data.Maybe import Data.List type Atom = String ty
我想找到两个以上数字的最小公倍数 求给定N个数的最小公倍数的C++程序 最佳答案 int lcm(int a, int b) { return (a/gcd(a,b))*b; } 对于gcd,请查看
这个程序有错误。谁能解决这个问题? Error is :TempRecord already defines a member called 'this' with the same paramete
当我运行下面的程序时,我在 str1 和 str2 中得到了垃圾值。所以 #include #include #include using namespace std; int main() {
这是我的作业: 一对刚出生的兔子(一公一母)被放在田里。兔子在一个月大时可以交配,因此在第二个月的月底,每对兔子都会生出两对新兔子,然后死去。 注:在第0个月,有0对兔子。第 1 个月,有 1 对兔子
我编写了一个程序,通过对字母使用 switch 命令将十进制字符串转换为十六进制,但是如果我使用 char,该程序无法正常工作!没有 switch 我无法处理 9 以上的数字。我希望你能理解我,因为我
我是 C++ 新手(虽然我有一些 C 语言经验)和 MySQL,我正在尝试制作一个从 MySQL 读取数据库的程序,我一直在关注这个 tutorial但当我尝试“构建”解决方案时出现错误。 (我正在使
仍然是一个初学者,只是尝试使用 swift 中的一些基本函数。 有人能告诉我这段代码有什么问题吗? import UIKit var guessInt: Int var randomNum = arc
我正在用 C++11 编写一个函数,它采用 constant1 + constant2 形式的表达式并将它们折叠起来。 constant1 和 constant2 存储在 std::string 中,
我用 C++ 编写了这段代码,使用运算符重载对 2 个矩阵进行加法和乘法运算。当我执行代码时,它会在第 57 行和第 59 行产生错误,非法结构操作(两行都出现相同的错误)。请解释我的错误。提前致谢:
我是 C++ 的初学者,我想编写一个简单的程序来交换字符串中的两个字符。 例如;我们输入这个字符串:“EXAMPLE”,我们给它交换这两个字符:“E”和“A”,输出应该类似于“AXEMPLA”。 我在
我需要以下代码的帮助: 声明 3 个 double 类型变量,每个代表三角形的三个边中的一个。 提示用户为第一面输入一个值,然后 将用户的输入设置为您创建的代表三角形第一条边的变量。 将最后 2 个步
我是一名优秀的程序员,十分优秀!