[英]How to determine if a resize event was triggered by soft keyboard in mobile browser?
有很多關於軟鍵盤的討論,但我還沒有找到解決我問題的好方法。
我有一個調整大小的功能,如:
$(window).resize(function() {
///do stuff
});
我想在所有調整大小事件的那個函數中做“東西”,除非它被軟鍵盤觸發。 如何確定軟鍵盤是否觸發了調整大小?
我最近遇到了一些需要對此進行檢查的問題。 我設法像這樣解決它:
$(window).on('resize', function(){
// If the current active element is a text input, we can assume the soft keyboard is visible.
if($(document.activeElement).attr('type') === 'text') {
// Logic for while keyboard is shown
} else {
// Logic for while keyboard is hidden
}
}
我只需要它用於文本輸入,但顯然這可以擴展到任何可能觸發軟鍵盤/數字選擇器等的元素。
我剛剛修復了 kirisu_kun 使用 prop() 而不是 attr() 的答案:
$(window).on('resize', function(){
// If the current active element is a text input, we can assume the soft keyboard is visible.
if($(document.activeElement).prop('type') === 'text') {
// Logic for while keyboard is shown
} else {
// Logic for while keyboard is hidden
}
}
問題是,如果活動元素被聚焦,你可以通過關閉鍵盤而不改變焦點來觸發調整大小事件..所以,鍵盤會隱藏但代碼會進入焦點狀態。
與之前的答案類似,但針對所有具有焦點的表單子項(顯然,如果沒有表單父項,輸入將失敗)
$(window).resize(function() {
if($('form *').focus()) {
alert('ignore this');
} else {
// do the thing
}
});
所以也許這個...
$(window).resize(function() {
if($('input, select, etc').focus()) {
alert('ignore this');
} else {
// do the thing
}
});
我一直在尋找類似問題的解決方案。 當 url 輸入進出視圖時,我的調整大小事件被觸發。 這是我一直在努力的事情......有可能的解決方案嗎?
所以基本上你只檢查屏幕的寬度是否已經改變,如果不同,則只在調整大小時觸發你的功能:
例如:
var saveWindowWidth = true;
var savedWindowWidth;
//set the initial window width
if (saveWindowWidth = true){
savedWindowWidth = windowWidth;
saveWindowWidth = false;
}
//then on resize...
$(window).resize(function() {
//if the screen has resized then the window width variable will update
windowWidth = window.innerWidth;
//if the saved window width is still equal to the current window width do nothing
if (savedWindowWidth == windowWidth){
return;
}
//if saved window width not equal to the current window width do something
if(savedWindowWidth != windowWidth) {
// do something
savedWindowWidth = windowWidth;
}
});
有幾件事你需要集中注意力
當瀏覽器調整大小時,您可以使用這些組合
function getWidth(){
return $( window ).width();
}
function getHeight(){
return $( window ).height();
}
function isFocus(){
return $(document.activeElement).prop('tagName')=='INPUT' || $(document.activeElement).prop('tagName')=='TEXTAREA';
}
var focused = false;
var windowWidth=getWidth(),windowHeight=getHeight();
//then on resize...
$(window).resize(function() {
var tWidth=getWidth(),tHeight=getHeight(),tfocused=isFocus();
//if the saved window width is still equal to the current window width do nothing
if (windowWidth == tWidth && ((tHeight < windowHeight && !focused && tfocused) || (tHeight > windowHeight && focused && !tfocused))){
windowWidth=tWidth;
windowHeight=tHeight;
focused=tfocused;
return;
}
else{
windowWidth=tWidth;
windowHeight=tHeight;
focused=tfocused;
//Your Code Here
}
});
在當前版本的 chrome webview 中工作正常。 我剛剛使用以下代碼在 Angular 中實現了窗口大小調整功能。
@HostListener('window:resize', ['$event'])
onResize(event) {
// Do you handling here.
}
注意:同樣可以通過使用來實現
window.addEventListener('resize', () => {
// Do handling here
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.