【问题标题】:Automatically scroll to a div when Flask returns rendered templateFlask 返回渲染模板时自动滚动到 div
【发布时间】:2015-10-30 01:31:16
【问题描述】:

我想滚动到渲染模板上的特定 div。我知道我可以添加像#something 这样的锚点,但我正在渲染模板,我无法更改网址。我该怎么做?

<div id="something">...</div>
def search():
    ...
    return render_template('search.html')  # should scroll to #something

【问题讨论】:

    标签: python flask


    【解决方案1】:

    你不能对来自服务器的响应做任何事情,但是你可以在渲染的模板上添加一个小的 JavaScript sn-p,它将页面滚动到你想要的位置。请参阅Element.scrollIntoViewlocation.hash

    # pass scroll if you want to scroll somewhere
    return render_template('search.html', scroll='something')
    
    <div id="something">
    {% if scroll %}
    <script>
        document.getElementById('{{ scroll }}').scrollIntoView();
        // or
        document.location.hash = '#' + '{{ scroll }}';
    </script>
    {% endif %}
    

    更多信息请参见How to scroll HTML page to given anchor?

    如果您正在重定向,因此您可以控制 URL,请参阅Link to a specific location in a Flask template

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-04
      • 2021-11-12
      • 2023-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多