Scroll p:messages into view when it is updated and (error) messages are present

前端 未结 4 1289
暖寄归人
暖寄归人 2021-01-05 18:58

I am working with PrimeFaces messages, I want my whole page to scroll to top when p:messages is rendered.

4条回答
  •  半阙折子戏
    2021-01-05 19:47

    Deprecated with PrimeFaces < 6.2

    In you backing bean (that one which produces the messages), you should know when you render a p:message. If so simply execute this:

    RequestContext.getCurrentInstance().execute("window.scrollTo(0,0);");
    

    Update:

    With the newer PrimeFaces versions (>= 6.2), the approach to execute Javascript on the client side is (by using x and y coordinates):

    PrimeFaces instance = PrimeFaces.current();
    instance.execute("window.scrollTo(0,0);");
    

    To scroll to an element use the element's clientId:

    PrimeFaces instance = PrimeFaces.current();
    instance.scrollTo("myElementsClientId");
    

    Find more information here:

    • http://de.selfhtml.org/javascript/objekte/window.htm#scroll_to
    • examples with jQuery for smooth scrolling as well: Scroll to the top of the page using JavaScript/jQuery?

提交回复
热议问题