ifvisible.js

以跨浏览器和轻量级方式检查用户是否正在查看页面或与页面进行交互。「Crossbrowser & lightweight way to check if user is looking at the page or interacting with it.」

Github星跟踪图

ifvisible.js

Crossbrowser & lightweight way to check if user is looking at the page or interacting with it.

Check out the Demo or read below for code example or Check Annotated Source

Installation

FOSSA Status

From npm

npm install ifvisible.js --save

From Bower

bower install ifvisible.js

For Meteor

mrt add ifvisible

meteor package is provided by @frozeman via Atmosphere

Examples


// If page is visible right now
if( ifvisible.now() ){
	// Display pop-up
	openPopUp();
}

// You can also check the page status
// using `now` method
if( !ifvisible.now('hidden') ){
  // Display pop-up if page is not hidden
  openPopUp();
}

// Possible statuses are:
// idle: when user has no interaction
// hidden: page is not visible
// active: page is visible and user is active

Handle tab switch or browser minimize states


ifvisible.on("blur", function(){
	// example code here..
	animations.pause();
});

ifvisible.on("focus", function(){
	// resume all animations
	animations.resume();
});

ifvisible.js can handle activity states too, such as being IDLE or ACTIVE on the page


ifvisible.on("idle", function(){
	// Stop auto updating the live data
	stream.pause();
});

ifvisible.on("wakeup", function(){
	// go back updating data
	stream.resume();
});

Default idle duration is 60 seconds but you can change it with setIdleDuration method


ifvisible.setIdleDuration(120); // Page will become idle after 120 seconds

You can manually trigger status events by calling them directly or you can set events with their names by giving first argument as a callback


ifvisible.idle(); // will put page in a idle status

ifvisible.idle(function(){
	// This code will work when page goes into idle status
});

// other methods are
ifvisible.blur(); // will trigger idle event as well
ifvisible.idle();

ifvisible.focus(); // Will trigger wakeup event as well
ifvisible.wakeup();

You can set your smart intervals with ifvisible.js, if user is IDLE or not seeing the page the interval will automatically stop itself


// If page is visible run this function on every half seconds
ifvisible.onEvery(0.5, function(){
    // Do an animation on the logo only when page is visible
	animateLogo();

});

License

It's MIT, Go crazy.

FOSSA Status

主要指标

概览
名称与所有者serkanyersen/ifvisible.js
主编程语言TypeScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2012-05-18 07:38:01
推送于2023-01-07 08:09:12
最后一次提交2020-12-08 13:04:18
发布数8
最新版本名称v2.0.10 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数1.9k
关注者数62
派生数186
提交数102
已启用问题?
问题数42
打开的问题数23
拉请求数11
打开的拉请求数15
关闭的拉请求数12
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?