如何知道浏览器在javascript中最小化
嗨,我有一个通知如何知道浏览器在javascript中最小化,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,嗨,我有一个通知div(divNotify),母版页上有一些信息和计时器 Protected Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Try Me.GetNotification_Stats() ScriptManager.RegisterClientScriptBlock(Me.Page, Ge
div(divNotify)
,母版页上有一些信息和计时器
Protected Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
Me.GetNotification_Stats()
ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "Alert", "Show_NotifyDiv();", True)
Catch ex As Exception
Me.lblError.Visible = True
Me.lblError.InnerText = ex.Message
End Try
End Sub
divNotify
将在某个时间间隔内显示。这里我需要当用户将浏览器最小化时,他将通过闪烁浏览器和更改浏览器颜色得到通知 但首先,我如何知道浏览器是否在javasript中最小化 这里我使用jquery来显示div标记
function Show_NotifyDiv() {
$("#div_NotificationOuter").show(1000);
$("#div_NotificationOuter").animate({ bottom: '+=30px' }, 4000);
}
不可能通过JavaScript确定页面是否最小化,但可以使用确定页面是否对用户可见 目前有Chrome、Mozilla和IE10版本
我们会耐心等待,但visibilityState即将到来:
除了c69的答案之外,我还建议使用图书馆。
它具有访问W3C页面可见性API的实用功能,您不必担心跨浏览器支持(统一moz或webkit前缀函数)除了c69的答案之外,我还建议使用该库。
它具有访问W3C页面可见性API的实用程序功能,您无需担心跨浏览器支持(统一了moz或webkit前缀函数)页面可见性API提供了事件,您可以查看这些事件以了解文档何时变为可见或隐藏,以及查看页面当前可见性状态的功能 注意事项:页面可见性API在保存时特别有用 通过让页面避免执行 文档不可见时不必要的任务 当用户最小化窗口或切换到另一个选项卡时,API会发送一个事件,让侦听器知道页面的状态已更改。您可以检测事件并执行某些操作或以不同的方式进行操作。例如,如果您的web应用正在播放视频,它可以在用户将选项卡置于后台时暂停视频,并在用户返回选项卡时恢复播放。用户不会失去在视频中的位置,视频的音轨不会干扰新前景选项卡中的音频,同时用户不会错过任何视频 用例
让我们考虑页面可见性API的一些用例。
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
var videoElement = document.getElementById("videoElement");
// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
if (document[hidden]) {
videoElement.pause();
} else {
videoElement.play();
}
}
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
document.addEventListener(visibilityChange, handleVisibilityChange, false);
// When the video pauses, set the title.
// This shows the paused
videoElement.addEventListener("pause", function(){
document.title = 'Paused';
}, false);
// When the video plays, set the title.
videoElement.addEventListener("play", function(){
document.title = 'Playing';
}, false);
}
来源:页面可见性API提供了可查看的事件,以了解文档何时可见或隐藏,以及查看页面当前可见性状态的功能 注意事项:页面可见性API在保存时特别有用 通过让页面避免执行 文档不可见时不必要的任务 当用户最小化窗口或切换到另一个选项卡时,API会发送一个事件,让侦听器知道页面的状态已更改。您可以检测事件并执行某些操作或以不同的方式进行操作。例如,如果您的web应用正在播放视频,它可以在用户将选项卡置于后台时暂停视频,并在用户返回选项卡时恢复播放。用户不会失去在视频中的位置,视频的音轨不会干扰新前景选项卡中的音频,同时用户不会错过任何视频 用例
让我们考虑页面可见性API的一些用例。
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
var videoElement = document.getElementById("videoElement");
// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
if (document[hidden]) {
videoElement.pause();
} else {
videoElement.play();
}
}
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
document.addEventListener(visibilityChange, handleVisibilityChange, false);
// When the video pauses, set the title.
// This shows the paused
videoElement.addEventListener("pause", function(){
document.title = 'Paused';
}, false);
// When the video plays, set the title.
videoElement.addEventListener("play", function(){
document.title = 'Playing';
}, false);
}
来源:可能重复的可能重复的