简体   繁体   English

反应 css 侧边栏不会关闭小 window 尺寸

[英]react css sidebar wont close for small window size

I have a react sidebar with pure css for expand/collapse animations that I really like.我有一个带有纯 css 的反应侧边栏,用于我非常喜欢的展开/折叠动画。 Except for that by default, every time I open my page / jsfiddle in this case, the sidebar will always be closed by default.除了默认情况下,每次我在这种情况下打开我的页面/jsfiddle时,侧边栏都会默认关闭。

https://jsfiddle.net/martinradio/x1dz80a6/2/ https://jsfiddle.net/martinradio/x1dz80a6/2/

I've collected all the @media queries in my css file, and have changed it so when the window is big, the sidebar turns yellow.我已经在我的 css 文件中收集了所有@media 查询,并对其进行了更改,因此当 window 很大时,侧边栏变为黄色。 if the window becomes small, the sidebar gets colored red.如果 window 变小,侧边栏将变为红色。

My sidebar expand/collapse logic is pure css, I want my sidebar to collapse if the window is too small (sidebar color = red), can I add a.sidebar value to collapse the sidebar?我的侧边栏展开/折叠逻辑是纯 css,如果 window 太小(侧边栏颜色 = 红色),我希望我的侧边栏折叠,我可以添加 a.sidebar 值来折叠侧边栏吗?


/* ----------------------
@media queries
 ---------------------- */

/* if screen is big: show sidebar */
@media (min-width: 30em) {
  .sidebar {
    background:yellow;
    color:yellow;
  }
}

/* if screen is too small: hide sidebar */
@media (max-width: 31em) {
  .sidebar {
  background:red;
  color:red;
  }
  /* add something here to toggle sidebar as higgen */
}

is there a way, that by adding css, I can have my sidebar start expanded if the user is viewing the page on say a desktop monitor dimensions?有没有办法,通过添加 css,如果用户正在查看桌面显示器尺寸的页面,我可以让我的侧边栏开始扩展? But keep the sidebar hidden for smaller browser windows such as mobile但是对于较小的浏览器 windows (例如移动设备)保持侧边栏隐藏

You can use CSS Media Queries to determine what action to do based on the screen size.您可以使用 CSS 媒体查询来根据屏幕大小确定要执行的操作。 Here, you would want to change the behavior of the sidebar based on the screen size.在这里,您可能希望根据屏幕大小更改侧边栏的行为。

@media (max-width: 600px) { 

*This is for phones, for example (you might need to find more accurate pixel counts)*

}

@media (min-width: 601px) { 

*This is for laptops, for example (you might need to find more accurate pixel counts)*

}

You want the :checked value of your input to control the current expanded state of you sidebar.您希望输入的:checked值来控制侧边栏的当前扩展 state。

But, at the same time, you want the size of the viewport to determine the state of your sidebar.但是,与此同时,您希望视口的大小来确定侧边栏的 state。

The bad news is you can't change or set the value of a checkbox using CSS alone.坏消息是您不能单独使用 CSS 更改或设置复选框的值。

The good news is you can do it using JavaScript.好消息是您可以使用 JavaScript 来做到这一点。 And you can link it to a media query.您可以将其链接到媒体查询。

Here's how to do it in React:以下是在 React 中的操作方法:

  // define media query:
  const mediaQuery = window.matchMedia("(min-width: 42rem)");

  // track query state (if it applies or not)
  const [query, setQuery] = React.useState(mediaQuery);

  // keep query state updated (+ cleanup)
  React.useEffect(() => {
    mediaQuery.addListener(setQuery);
    return () => mediaQuery.removeListener(setQuery);
  }, [mediaQuery]);

  // track sidebar expanded state
  const [expanded, setExpanded] = React.useState(query.matches);

  // update checkbox value when the query state changes
  React.useEffect(() => {
    setExpanded(query.matches);
  }, [query]);

  // everything else stays the same 
  // (except binding the expanded state to the checkbox)

See it working: https://jsfiddle.net/websiter/td9rgcpq/看到它工作: https://jsfiddle.net/websiter/td9rgcpq/

When I worked on my answer, I realized that the requirements were not clear to me.当我研究我的答案时,我意识到这些要求对我来说并不明确。

What do you want to have on a big screen?你想在大屏幕上有什么? I can think of two different options.我可以想到两种不同的选择。

  1. The sidebar is not shown when you open the page.打开页面时,侧边栏不显示。 Instead, it appears with animation when you press the toggle button.相反,当您按下切换按钮时,它会显示为 animation。
  2. The sidebar is visible when you open the page and disappears when you press a toggle button.打开页面时侧边栏可见,按下切换按钮时侧边栏消失。 Should it appear with animation on page load?它是否应该在页面加载时与 animation 一起出现?

And in both cases, you do not want the sidebar on the small screen.在这两种情况下,您都不希望小屏幕上出现侧边栏。

I still do not understand the following requirement.我仍然不明白以下要求。

I can have my sidebar start expanded if the user is viewing the page on say a desktop monitor dimensions?如果用户在桌面显示器尺寸上查看页面,我可以让我的侧边栏开始扩展吗?

When a user opens the page on the big screen, what should it look like?当用户在大屏幕上打开页面时,它应该是什么样子? What is the initial state of the sidebar on the big screen?大屏侧边栏的初始state是什么? Do you want the sidebar to open with animation on page load?您希望边栏在页面加载时以 animation 打开吗?

Anyway, please find my suggestion below.无论如何,请在下面找到我的建议。

Main idea大意

The sidebar becomes visible when they check the #sidebar-checkbox checkbox.当他们选中#sidebar-checkbox复选框时,侧边栏变得可见。 I suggest limiting this behaviour to only large enough screens.我建议将此行为限制在足够大的屏幕上。 In other words, we bind the checkbox state with the sidebar visibility only for big screens.换句话说,我们将复选框 state 与侧边栏可见性绑定仅适用于大屏幕。

On small screens sidebar is always hidden because we place the rules under the media query for the big screens.在小屏幕上,侧边栏总是隐藏的,因为我们将规则放在大屏幕的媒体查询下。

1. Initially hidden but appears when toggled 1.最初隐藏但切换时出现

The code speaks for itself.代码不言自明。


@media (min-width: 30em) {
  #sidebar-checkbox:checked + .sidebar {
    z-index: 10;
    visibility: visible;
  }

  #sidebar-checkbox:checked ~ .sidebar, #sidebar-checkbox:checked ~ .wrap, #sidebar-checkbox:checked ~ .sidebar-toggle {
  -webkit-transform: translateX(0rem);
      -ms-transform: translateX(0rem);
          transform: translateX(0rem);
  }

  #sidebar-checkbox:checked ~ .wrap {
  -webkit-transform: translateX(14rem);
      -ms-transform: translateX(14rem);
          transform: translateX(14rem);
  }
}

I think we also should show the label for the checkbox only on big screens.我认为我们还应该仅在大屏幕上显示复选框的 label。 But I do not want to mess with your styles too much and try to make the minimal working change.但我不想过多地打扰您的 styles 并尝试进行最小的工作更改。

2. Initially shown on the big screen 2.最初在大屏幕上显示

We must replace the :checked selector with the :not(:checked) selector.我们必须用:not(:checked)选择器替换:checked选择器。 In this case, the sidebar is visible by default on the big screen.在这种情况下,侧边栏默认在大屏幕上可见。

We can also show animation for the sidebar sliding from left to right on the page load.我们还可以显示 animation 在页面加载时侧边栏从左向右滑动。 You might find some explanation in the answer to the question 'css3 transition animation on load?'.您可能会在“css3 transition animation on load?”问题的答案中找到一些解释。

Please check the updated JSFiddle with the suggestions for the second case.请检查更新的 JSFiddle以及第二种情况的建议。

Please let me know if I have got your requirements right.如果我满足您的要求,请告诉我。

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

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