当窗口宽度跨越 1439px 时,我试图让页面刷新。我知道这样做不是一个好习惯,但是当屏幕变得小于 1439 像素时,我需要激活一个插件。我环顾四周,有几个实例可以工作,但是刷新只是无限循环。
任何人都知道如何强制页面以 1439px 精确刷新并只运行一次?
谢谢!
也许最好的选择是将一个参数附加到 URL 以不重新加载插件。像这样的东西:
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
if($(window).width() === 1439 && !getURLParameter(noReload))
window.location.href = window.location.href + "?noReload=1&loadPlugin=1";
之后,您需要检查参数 loadPlugin 是否存在并加载插件
You don't need to refresh the page at all.
$(window).load(function(){
var scrollPaneDone = false,
testScrollPane = function(){
if(scrollPaneDone || $(window).width() > 1439) return;
$('#someElement').jScrollPane();
scrollPaneDone = true;
};
$(window).bind('resize', testScrollPane);
testScrollPane();
});
Explanation:
First, we define a variable scrollPaneDone, which will store whether or not we have already initialized the jScrollPane plugin. (So we don't initialize it twice)
Then, we define a function that checks if we have already initialized the scrollpane or if the window width is larger than 1439, the function will return and do nothing. Otherwise, we'll initialize the jScrollPane plugin and set our variable scrollPaneDone to true.
Then, we bind this function to the window resize event. And then call the function once, just in case the window is already less than 1439px it will activate the jScrollPane plugin on page load.