有条件地加载js文件与屏幕宽度
conditionally load js file with screen width
我有下面的java脚本,它根据分辨率和js文件加载屏幕。但是我必须刷新浏览器来重新加载每个分辨率的js文件,否则它会保留最后一个分辨率的js文件。
请帮助我了解如何同时加载这两个文件。
window.requestAnimFrame = (function(){
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); };
})();
var width = screen.width,
height = screen.height,
checkScreenSize = function () {
if (screen.width !== width || screen.height !== height) {
width = screen.width;
height = screen.height;
$(window).trigger('resolutionchange');
}
};
(function loop(){
requestAnimFrame(loop);
checkScreenSize();
})();
function includeJS(incFile) {
document.write('<script type="text/javascript" src="'+ incFile+ '"></scr' + 'ipt>');
}
if (window.matchMedia("only screen and (min-width: 1240px) and (max-width: 1280px)").matches) {
includeJS('changer/js/changer-1280.js');
} else if (window.matchMedia("only screen and (min-width: 1390px) and (max-width: 1440px)").matches) {
includeJS('changer/js/changer-1440.js');
} else if (window.matchMedia("only screen and (min-width: 1441px) and (max-width: 1441px)").matches) {
includeJS('changer/js/changer-1441.js');
}
听起来你可能想看窗口大小调整事件?像这样:
$(window).on('resize', function() {
if (window.matchMedia("only screen and (min-width: 1240px) and (max-width: 1280px)").matches) {
$.getScript('changer/js/changer-1280.js');
} else if (window.matchMedia("only screen and (min-width: 1390px) and (max-width: 1440px)").matches) {
$.getScript('changer/js/changer-1440.js');
} else if (window.matchMedia("only screen and (min-width: 1441px) and (max-width: 1441px)").matches) {
$.getScript('changer/js/changer-1441.js');
}
});
既然你正在使用jQuery,你可以使用它的$.getScript
而不是手动注入script元素。
我看到有一些代码在那里,似乎观察窗口的高度和宽度实现自定义窗口调整大小事件。但这并不是必要的。我想你尤其不想在RAF循环中这样做,因为它可能会在每一帧中触发布局。
每次窗口的调整大小事件触发时运行那些matchMedia检查也会阻碍调整大小的性能,所以你应该在调整大小暂停后才处理这个事件。像这样:
var resizeTimer;
$(window).on('resize', function() {
clearTimeout(resizeTimer);
// Wait half a second before reacting to the resize event,
// in case the user is still actively resizing the window.
resizeTimer = setTimeout(handleWindowResize, 500);
});
function handleWindowResize() {
if (window.matchMedia("only screen and (min-width: 1240px) and (max-width: 1280px)").matches) {
$.getScript('changer/js/changer-1280.js');
} else if (window.matchMedia("only screen and (min-width: 1390px) and (max-width: 1440px)").matches) {
$.getScript('changer/js/changer-1440.js');
} else if (window.matchMedia("only screen and (min-width: 1441px) and (max-width: 1441px)").matches) {
$.getScript('changer/js/changer-1441.js');
}
}
相关文章:
- 如果使用javascript函数屏幕太小,我该如何更改HTML文件的背景色
- 我可以通过点击html文件中的按钮来记录我的屏幕/网页吗
- “无法下载此文件”时尝试在主屏幕Web应用程序中访问ics文件
- 根据屏幕分辨率包含 php 文件
- 只加载宽屏幕大小的javascript文件
- 从json文件输出多个屏幕截图(例如itunes搜索api)
- 将特定html文件上的幻灯片图像调整为移动屏幕
- 如何在屏幕大小上将html文件更改为另一个html文件
- 如何根据屏幕大小加载.js文件
- 如何使用纯javascript或jQuery将HTML5画布屏幕作为png图像保存到项目的特定文件夹中
- 基于屏幕大小的动态包含文件不起作用
- 为什么它要将包含的文件中的所有注释打印到屏幕上
- 如何在较小尺寸的屏幕中间以单独的 html 形式获取 php 文件的输出
- iPhone上的html文件到照片库的屏幕截图
- 如何打开另一个文件索引.html,具体取决于屏幕的宽度
- 当浏览器下载所需的文件时,GIF 在初始屏幕中缓慢制作动画
- 如何将 swf 文件居中显示在不同的屏幕分辨率上
- 量角器-html-屏幕截图-报告器未显示报告器文件中执行的所有测试
- 如何使用 html5 输入本地文件并在屏幕上输出文件
- CasperJS屏幕截图未将图像渲染为png文件