简体   繁体   English

如何根据链接是否处于焦点位置更改导航栏的高度?

[英]How do I change my navbar's height based on whether a link is in focus?

I'm using React and javascript to style my webpage, and I have implemented a skip link in my navbar that takes the user further down the page when clicked.我正在使用 React 和 javascript 来设置我的网页样式,并且我在我的导航栏中实现了一个跳过链接,当用户点击该链接时,它会将用户带到页面下方。 This skip link only comes into focus when the user hits the tab key, but it is positioned above the site logo in the navbar and so needs more space when it is visible.此跳转链接仅在用户点击 tab 键时才会成为焦点,但它位于导航栏中的站点徽标上方,因此在可见时需要更多空间。 I want to increase the height of my navbar when the link is visible, and right now I am using a boolean called linkHasFocus (for some conditional CSS styling), along with a function called checkFocus, to do that:当链接可见时,我想增加导航栏的高度,现在我正在使用名为 linkHasFocus 的 boolean (对于某些有条件的 CSS 样式),以及一个名为 ZC1C425268E68385D1ABZ507 的 ZC1C425268E68385D1ABZ507

const TopNavbar = styled. div`
      ...
      height: ${props => (props.linkHasFocus ? '9rem' : '4rem')}; 
      ... 
`;

And here's the checkFocus function:这是 checkFocus function:

const checkFocus = () => {
      const elem = document.getElementById('skip-link');
      if (elem === document.activeElement) {
        this.linkHasFocus = true;
      }
      this.linkHasFocus = false;
      return this.linkHasFocus;    
};

I then pass this to my TopNavbar component (which is the parent of my skip link component) in my return function like so:然后,我在返回 function 中将其传递给我的 TopNavbar 组件(它是我的跳过链接组件的父级),如下所示:

<TopNavbar linkHasFocus={checkFocus}>

But it seems the checkFocus function isn't updating the linkHasFocus variable correctly.但似乎 checkFocus function 没有正确更新 linkHasFocus 变量。 My understanding of props and javascript in general is a little shaky, so apologies if there's glaring issues here.我对 props 和 javascript 的总体理解有点不稳定,如果这里有明显的问题,我深表歉意。

To focus an elemento you have to use the method .focus() .要聚焦一个元素,你必须使用方法.focus()

Then you could use CSS :focus Selector to change the style (and so the height of the component), when the element is focused.然后你可以使用 CSS :focus Selector 在元素获得焦点时更改样式(以及组件的高度)。

Example with a <button> element: <button>元素的示例:

 /* Normal */ #skip-link { height: 4rem; } #skip-link:focus { height: 9rem; } /* Smooth transition */ #skip-link-transition { height: 4rem; -webkit-transition: all.5s ease; -moz-transition: all.5s ease; -ms-transition: all.5s linear; -o-transition: all.5s linear; transition: all.5s ease; } #skip-link-transition:focus { height: 9rem; -webkit-transition: all.5s ease; -moz-transition: all.5s ease; -ms-transition: all.5s linear; -o-transition: all.5s linear; transition: all.5s linear; } /* Table CSS */ table, th, td { border: 1px solid; }
 <table> <tr align="center"> <td width="100rem"><b>Normal</b></td> <td width="100rem"><b>Smooth Transition</b></td> </tr> <tr align="center"> <td><button id="skip-link">Test1</button></td> <td><button id="skip-link-transition">Test2</button></td> </tr> </table>

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

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