背景: 彈出層插件(自適應) 實現過程中突然發現在獲取可視區高度時,無論document.documentElement.clientHeight 還是 $(window).height()都無法正確獲取,代碼沒有問題;
解決方案:通過 cosole.log尋找錯誤點發現$(window).height()獲取不正常並等於$(document).height();網上查找資料是由於沒有正確的HTML5文檔聲明導致的
正確: <!DOCTYPE html> <html> <!-- 文檔內容 --> </html> 錯誤: <!DOCTYPE> <html> <!-- 文檔內容 --> </html> $(function(){ console.log($(window).height()) console.log(document.documentElement.clientHeight) })
獲取的值是不一樣的