我能想到的唯一方法是在使用效果之前或同时在后台在 JS 中运行某种速度测试。这应该捕获由于处理器速度而速度较慢的设备,反之亦然,捕获时间准确/快速的设备。但是,如果设备具有优化意味着它们使用不同的处理器来计算图形效果,这将不起作用。
var speedtest = function(){
/// record when we start
var target = new Date().getTime(), count = 0, slow = 0;
/// trigger a set interval to keep a constant eye on things
var iid = setInterval(function(){
/// get where we actually are in time
var actual = new Date().getTime();
/// calculate where we expect time to be
target += 100;
/// 100 value here would need to be tested to find the best sensitivity
if ( (actual - target) > 100 ) {
/// make sure we aren't firing on a one off slow down, wait until this
/// has happened a few times in a row. 5 could be too much / too little.
if ( (++slow) > 5 ) {
/// finally if we are slow, stop the interval
clearInterval(iid);
/// and disable our fancy resource-hogging things
turnOffFancyAnimations();
}
}
else if ( slow > 0 ){
/// decrease slow each time we pass a speedtest
slow--;
}
/// update target to take into account browsers not being exactly accurate
target = actual;
/// use interval of 100, any lower than this might be unreliable
},100);
}
当然,运行它也会影响设备的速度,所以它并不是最好的解决方案。作为一项规则,我倾向于在屏幕很小的时候禁用动画和其他多余的东西。
这种方法的另一个缺点——我以前经历过——是一个实现多选项卡环境 setIntervals 的特定浏览器在不查看选项卡时会自动限制为特定速度。这意味着对于这个浏览器来说,tab键会自动降低他们的体验——除非这个强加的速度限制可以以某种方式被检测到。