简体   繁体   English

使用 python 中的 dash-bootstrap-component 按钮跳转到 html 锚点

[英]Jump to html anchor using dash-bootstrap-component button in python

I'm trying to get a button to jump/scroll to a certain part of the webpage using dash-bootstrap-components.我正在尝试使用 dash-bootstrap-components 获得一个按钮来跳转/滚动到网页的某个部分。 The anchor is added to the URL correctly, but the webpage doesn't move.锚点已正确添加到 URL 中,但网页没有移动。

import dash_html_components as html
import dash_bootstrap_components as dbc

layout = html.Div([
...
  html.Div([
    dbc.Button('Record Information', id='record-info-btn', href='#record-info'
                className='btn btn-orange align-middle btn btn-secondary')])
...
  html.Div([
    dbc.Form([
      html.Div([
        dbc.FormGroup([
          html.Div([
            html.H3('Record Information', id='record-info')
            ...
          ])
        ])
      ])
    ])
  ])
], className='whitContainerHalf')

I've tried using dbc.NavLink instead of Button, but the same thing happens.我尝试使用 dbc.NavLink 代替 Button,但同样的事情发生了。

According to the dbc documentation, Button has an href field that can link to an internal id or an external url;根据 dbc 文档,Button 有一个 href 字段,可以链接到内部 id 或外部 url; however, according to an issue on their repo , it doesn't work for internal ids.但是,根据他们 repo 上的一个问题,它不适用于内部 ID。 You have to use html.A() to jump to the section.您必须使用 html.A() 才能跳转到该部分。

import dash_html_components as html
import dash_bootstrap_components as dbc

layout = html.Div([
...
  html.Div([
    html.A(dbc.Button('Record Information', id='record-info-btn', 
                      className='btn btn-orange align-middle btn btn-secondary'), 
           href='#record-info')
  ])
...
  html.Div([
    dbc.Form([
      html.Div([
        dbc.FormGroup([
          html.Div([
            html.H3('Record Information', id='record-info')
            ...
          ])
        ])
      ])
    ])
  ])
], className='whiteContainerHalf')

I think this offers a better demonstration:我认为这提供了一个更好的演示:

app.layout = html.Div([

   html.Div([
    html.A(dbc.Button('Record Information', id='record-info-btn'), 
           href='#record-info')
  ]),

  html.Div([
      html.Div([], style={'display': "block", 'height': "2000px"}),
          html.Div([
            html.H3('Record Information', id='record-info')
          ])
  ])
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM