1

我对 WebView 中的滚动条有问题。我想在加载 web 视图时,滚动条自动滚动到 web 视图的末尾。我该怎么做?

这是我的样本

package sample;  
import javafx.fxml.FXML;  
import javafx.fxml.Initializable;  
import javafx.scene.Node;  
import javafx.scene.control.ScrollBar;  
import javafx.scene.web.WebEngine;  
import javafx.scene.web.WebView;  
import java.net.URL;  
import java.util.ResourceBundle;  
import java.util.Set;  
public class Controller implements Initializable {  
    @FXML  
    WebView webView;  
    @Override  
    public void initialize(URL url, ResourceBundle resourceBundle) {  
        loadWebView();  
    }  
    private void loadWebView() {  
        webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");  
        StringBuilder html= new StringBuilder().append("<html><body>");  
        for (int i=0; i<100; i++) {  
            html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");  
        }  
        html.append("</body></html>");  
        Set<Node> nodes = webView.lookupAll(".scroll-bar");  
        for (Node node : nodes) {  
            if (ScrollBar.class.isInstance(node)) {  
                System.out.println("Scrollbar here!!!");  
                ScrollBar scroll = (ScrollBar) node;  
                scroll.setValue(scroll.getMax());  
            }  
        }  
        WebEngine webEngine = webView.getEngine();  
        webEngine.loadContent(html.toString());  
    }  
}  

谢谢 4 你的阅读

4

3 回答 3

2

只需在 loadContent 调用后执行此脚本:

webEngine.executeScript("window.scrollTo(0, document.body.scrollHeight);");
于 2017-01-18T15:51:33.347 回答
1

来自https://forums.oracle.com/message/11243184#11243184

package sample;  


import com.sun.javafx.scene.control.skin.ScrollBarSkin;  
import com.sun.webpane.sg.theme.ScrollBarThemeImpl;  
import javafx.beans.value.ChangeListener;  
import javafx.beans.value.ObservableValue;  
import javafx.collections.ListChangeListener;  
import javafx.event.EventHandler;  
import javafx.fxml.FXML;  
import javafx.fxml.Initializable;  
import javafx.geometry.Orientation;  
import javafx.scene.Node;  
import javafx.scene.control.ScrollBar;  
import javafx.scene.control.TextArea;  
import javafx.scene.web.WebEngine;  
import javafx.scene.web.WebEvent;  
import javafx.scene.web.WebView;  


import java.net.URL;  
import java.util.ResourceBundle;  
import java.util.Set;  


public class Controller implements Initializable {  
    @FXML  
    WebView webView;  


    @Override  
    public void initialize(URL url, ResourceBundle resourceBundle) {  
        loadWebView();  
    }  


    private void loadWebView() {  
        webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");  
        StringBuilder html= new StringBuilder().append("<html>");  
        html.append("<head>");  
        html.append("   <script language=\"javascript\" type=\"text/javascript\">");  
        html.append("       function toBottom(){");  
        html.append("           window.scrollTo(0, document.body.scrollHeight);");  
        html.append("       }");  
        html.append("   </script>");  
        html.append("</head>");  
        html.append("<body onload='toBottom()'>");  


        for (int i=0; i<100; i++) {  
            html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");  
        }  


        html.append("</body></html>");  


        WebEngine webEngine = webView.getEngine();  
        webEngine.loadContent(html.toString());  
    }  
}  
于 2013-10-24T04:30:08.343 回答
0

您必须等到Worker完成才能继续向下滚动。

webView.getEngine().loadContent("<p>my text</p>");
webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
    if (newValue == Worker.State.SUCCEEDED)
        webView.getEngine().executeScript("window.scrollTo(0, document.body.scrollHeight);");
}

});

于 2020-06-05T17:28:39.857 回答