[英]Removing the $(window).resize Event in jQuery
我正在開發的頁面的一部分需要在用戶單擊按鈕時將$(窗口).resize事件添加到div,以便在用窗口調整大小並將其固定為原始大小之間切換:
function startResize() {
$(window).resize(function() {
$("#content").width(newWidth);
$("#content").height(newHeight);
});
}
我無法解決的是,當再次單擊該按鈕時,如何“關閉”此事件,以便內容停止調整大小。
function endResize() {
// Code to end $(window).resize function
$("#content").width(originalWidth);
$("#content").height(originalHeight);
}
任何有關這方面的幫助將不勝感激。
function endResize() {
$(window).off("resize");
$("#content").width(originalWidth);
$("#content").height(originalHeight);
}
請注意,這非常突兀,可能會破壞其他代碼。
這是更好的方法:
function resizer() {
$("#content").width(newWidth);
$("#content").height(newHeight);
}
function startResize() {
$(window).resize(resizer);
}
function endResize() {
$(window).off("resize", resizer);
}
function startResize() {
$(window).on("resize.mymethod",(function() {
$("#content").width(newWidth);
$("#content").height(newHeight);
}));
}
function endResize() {
$(window).off("resize.mymethod");
}
在查詢方法上使用命名空間將允許僅為您的方法關閉resize事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.