How do I retrieve an HTML element's actual width and height?

前端 未结 14 1851
后悔当初
后悔当初 2020-11-22 02:29

Suppose that I have a

that I wish to center in the browser\'s display (viewport). To do so, I need to calculate the width and height of the &l
14条回答
  •  眼角桃花
    2020-11-22 03:06

    Here is the code for WKWebView what determines a height of specific Dom element (doesn't work properly for whole page)

    let html = "\(textValue)"
    webView.navigationDelegate = self
    webView.loadHTMLString(taskHTML, baseURL: nil)
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.getElementById(\"spanEl\").getBoundingClientRect().height;") { [weak self] (response, error) in
            if let nValue = response as? NSNumber {
    
            }
        }
    }
    

提交回复
热议问题