我正在創建一個腳本,它在一些斷點處更改CSS,並在2個確切點處重定向到另一個頁面。window.location.replace不斷重新加載頁面,如何阻止?
更改CSS的工作流暢,但重定向點,腳本不斷重新加載頁面,如何停止它,並將其設置爲只重載一次,如果需要重新加載「分辨率再次改變」。
下面是代碼:
/*Change CSS in response to common resolutions.*/
$(document).ready(function() {
checkWidth();
$(window).resize(checkWidth);
});
function checkWidth(){
var windowWidth = $(window).width(),
cssLocation = $('link[href$="main.css"]');
if (windowWidth <= 240){
cssLocation.attr('href','stylesheets/240-main.css');
}
else if (windowWidth <= 480){
/*Redirecting point 1*/
window.location.replace("smartPhone-index.html");
cssLocation.attr('href','stylesheets/480-main.css');
}
else if (windowWidth <= 768){
/*Redirecting point 2*/
window.location.replace("index.html");
cssLocation.attr('href','stylesheets/768-main.css');
}
else if (windowWidth <= 1024){
cssLocation.attr('href','stylesheets/1024-main.css');
}
else if (windowWidth >= 1280){
cssLocation.attr('href','stylesheets/1280-main.css');
};
};
這是試圖達到什麼目的?更改CSS文件的href不會導致CSS重新加載,並且我不認爲resize事件也會執行。 – vogomatix
你需要通過外觀來限制你的'$(window).resize()'事件 - 有幾個[jQuery插件](https://www.google.com/search?q=jquery+油門+去抖動),可以幫助你。 – Emissary
更好的主意是編寫你的CSS,以便它調整所有系統的大小。看看[Bootstrap](http://getbootstrap.com),例如 – vogomatix