为什么这些箭头按钮没有定位到左边和右边?

huangapple go评论65阅读模式
英文:

Why these arrow buttons are not positioning to left and right

问题

I want these two left and right arrow buttons to be positioned left and right depending upon the props passed.

import styled from "styled-components";
import ArrowBackIosSharpIcon from '@mui/icons-material/ArrowBackIosSharp';
import ArrowForwardIosSharpIcon from '@mui/icons-material/ArrowForwardIosSharp';

const Container = styled.div`
  width: 100%;
  height: 100vh;
  display: flex;
  position: relative;
  overflow: hidden;
  background-color: #fff6a9;
`;

const Arrow = styled.div`
  width: 50px;
  height: 50px;
  background-color: #fff7f7;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  bottom: 0;

  left: ${(props) => props.direction === "left" && "10px"};
  right: ${(props) => props.direction === "right" && "10px"};

  margin: auto;
  cursor: pointer;
  opacity: 0.5;
  z-index: 2;
`;

const Slider = () => {
    return (
        <Container>
            <Arrow direction="left">
                <ArrowBackIosSharpIcon />
            </Arrow>
            <Arrow direction="right">
                <ArrowForwardIosSharpIcon />
            </Arrow>
        </Container>
    );
}

export default Slider;

I am passing left and right as props to styled components but this is not working.

英文:

I want these two left and right arrow buttons to be positioned left and right depending upon the props passed.

import styled from &quot;styled-components&quot;;
import ArrowBackIosSharpIcon from &#39;@mui/icons-material/ArrowBackIosSharp&#39;;
import ArrowForwardIosSharpIcon from &#39;@mui/icons-material/ArrowForwardIosSharp&#39;;

const Container = styled.div`
  width: 100%;
  height: 100vh;
  display: flex;
  position: relative;
  overflow: hidden;
  background-color: #fff6a9;
`;

const Arrow = styled.div`
  width: 50px;
  height: 50px;
  background-color: #fff7f7;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  bottom: 0;

  left: ${(props) =&gt; props.direction === &quot;left&quot; &amp;&amp; &quot;10px&quot;};
  right: ${(props) =&gt; props.direction === &quot;right&quot; &amp;&amp; &quot;10px&quot;};

  margin: auto;
  cursor: pointer;
  opacity: 0.5;
  z-index: 2;
`;

const Slider = () =&gt; {
    return (
        &lt;Container&gt;
            &lt;Arrow&gt;
                &lt;ArrowBackIosSharpIcon direction=&quot;left&quot; /&gt;
            &lt;/Arrow&gt;
            &lt;Arrow&gt;
                &lt;ArrowForwardIosSharpIcon direction=&quot;right&quot; /&gt;
            &lt;/Arrow&gt;
        &lt;/Container&gt;
    );
}

export default Slider;

I am passing left and right as props to styled components but this is not working.

答案1

得分: 1

提供属性给Arrow组件,而不是左箭头和右箭头。






英文:

Provide props to the Arrow component not the left arrow and right arrow.

        &lt;Arrow direction=&quot;left&quot;&gt;
            &lt;ArrowBackIosSharpIcon  /&gt;
        &lt;/Arrow&gt;
        &lt;Arrow direction=&quot;right&quot;&gt;
            &lt;ArrowForwardIosSharpIcon  /&gt;
        &lt;/Arrow&gt;

答案2

得分: 0

以下是翻译好的部分:

由于您正在检查“Arrow”组件上的 props,您需要将 props 传递给它,而不是“ArrowBackIosSharpIcon”组件。像这样:

<Arrow direction="left">
    <ArrowBackIosSharpIcon />
</Arrow>
<Arrow direction="right">
    <ArrowForwardIosSharpIcon />
</Arrow>

此外,您可以通过条件替换渲染样式属性本身:

{(props) => props.direction === "left" && "left: 10px"};
{(props) => props.direction === "right" && "right: 10px"};
英文:

Since you are checking the props on the 'Arrow' component, you will need to pass the props to it and not the 'ArrowBackIosSharpIcon' component. Like this:

&lt;Arrow direction=&quot;left&quot;&gt;
    &lt;ArrowBackIosSharpIcon /&gt;
&lt;/Arrow&gt;
&lt;Arrow direction=&quot;right&quot;&gt;
    &lt;ArrowForwardIosSharpIcon /&gt;
&lt;/Arrow&gt;

Additionally, you can render the style property itself conditionally by replacing:

left: ${(props) =&gt; props.direction === &quot;left&quot; &amp;&amp; &quot;10px&quot;};
right: ${(props) =&gt; props.direction === &quot;right&quot; &amp;&amp; &quot;10px&quot;};

with this:

${(props) =&gt; props.direction === &quot;left&quot; &amp;&amp; &quot;left: 10px&quot;};
${(props) =&gt; props.direction === &quot;right&quot; &amp;&amp; &quot;right: 10px&quot;};

huangapple
  • 本文由 发表于 2023年3月21日 01:06:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定