当单击按钮时,如何更改translateX的值?

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

How can i change translateX value when a button is clicked?

问题

Here's the translated part of your text:

"我是新手学习React。我想要在按钮被点击时,通过1170像素的量来改变translateX的值,但当我查看网站时,我不太明白在React中如何渲染onClick属性。

以下是代码:

function Slideshow() {
  const slider = [
    { name: slide_logo_1, link: "https://timesrecords.lnk.to/CITOPIA" },
    {
      name: slide_logo_2,
      link: "https://store.hangdiathoidai.com/search?q=lana+del+rey",
    },
    { name: slide_logo_3, link: "https://timesrecords.lnk.to/TocTien-Cong" },
    { name: slide_logo_4, link: "https://timesrecords.lnk.to/TSMidnights" },
    { name: slide_logo_5, link: "https://timesrecords.lnk.to/LINK-HTL" },
  ];
  const getTranslateX = () => {
    var myElement = document.querySelector(".owl-wrapper");
    var style = window.getComputedStyle(myElement);
    var matrix = new DOMMatrix(style.transform);
    <Alert>{matrix}</Alert>;
    
  };

  return (
    <section>
      <div className="wrapper">
        <div className="inner space-30 section-slider">
          <div
            className="slider-carousel owl-carousel owl-theme"
            id="slider-carousel"
            data-mobile="[479,1]"
            data-tabletsmall="[768,1]"
            data-desktopsmall="[979,1]"
            data-desktop="[1199,1]"
            data-pagination="false"
            data-navigation="true"
            data-autoplay="5000"
            data-items="1"
            style={{ display: "block", opacity: 1 }}
          >
            <div
              className="owl-wrapper-outer autoHeight"
              style={{ height: 431 }}
            >
              <div
                className="owl-wrapper"
                style={{
                  width: 11700,
                  left: 0,
                  display: "block",
                  transition: `all 400ms ease 0s`,
                  transform: `translate3d(0px, 0, 0)`,
                }}
              >
                {slider.map((products, index) => {
                  return (
                    <div className="owl-item" style={{ width: 1170 }}>
                      <div className="text-center">
                        <Link
                          className="slide"
                          to={products.link}
                          title=""
                          key={index}
                        >
                          <img src={products.name} alt="" />
                        </Link>
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>

            <div className="owl-controls clickable">
              <div className="owl-buttons">
                <div
                  className="sangtrai owl-prev"
                  onClick={getTranslateX}
                ></div>
                <div className="sangphai owl-next"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

export default Slideshow;
```"

Is there anything else you need assistance with?

<details>
<summary>英文:</summary>

I&#39;m new to react. I wanna make my button change the translateX value when it is clicked by amount of 1170px, but when i checked the site i quite don&#39;t get how the onClick attribute is rendered in reactjs.

Here is the code:

function Slideshow() {
const slider = [
{ name: slide_logo_1, link: "https://timesrecords.lnk.to/CITOPIA" },
{
name: slide_logo_2,
link: "https://store.hangdiathoidai.com/search?q=lana+del+rey",
},
{ name: slide_logo_3, link: "https://timesrecords.lnk.to/TocTien-Cong" },
{ name: slide_logo_4, link: "https://timesrecords.lnk.to/TSMidnights" },
{ name: slide_logo_5, link: "https://timesrecords.lnk.to/LINK-HTL" },
];
const getTranslateX = () => {
var myElement = document.querySelector(".owl-wrapper");
var style = window.getComputedStyle(myElement);
var matrix = new DOMMatrix(style.transform);
<Alert>{matrix}</Alert>

};

return (
<section>
<div className="wrapper">
<div className="inner space-30 section-slider">
<div
className="slider-carousel owl-carousel owl-theme"
id="slider-carousel"
data-mobile="[479,1]"
data-tabletsmall="[768,1]"
data-desktopsmall="[979,1]"
data-desktop="[1199,1]"
data-pagination="false"
data-navigation="true"
data-autoplay="5000"
data-items="1"
style={{ display: "block", opacity: 1 }}
>
<div
className="owl-wrapper-outer autoHeight"
style={{ height: 431 }}
>
<div
className="owl-wrapper"
style={{
width: 11700,
left: 0,
display: "block",
transition: all 400ms ease 0s,
transform: translate3d(0px, 0, 0),
}}
>
{slider.map((products, index) => {
return (
<div className="owl-item" style={{ width: 1170 }}>
<div className="text-center">
<Link
className="slide"
to={products.link}
title=""
key={index}
>
<img src={products.name} alt="" />
</Link>
</div>
</div>
);
})}
</div>
</div>

        &lt;div className=&quot;owl-controls clickable&quot;&gt;
&lt;div className=&quot;owl-buttons&quot;&gt;
&lt;div
className=&quot;sangtrai owl-prev&quot;
onClick={getTranslateX}
&gt;&lt;/div&gt;
&lt;div className=&quot;sangphai owl-next&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;

);
}

export default Slideshow;

The thing in here is when i click **.sangtrai**, the translateX value in **.owl-wrapper&#39;s transform** will be added by amount of **1170px** and when i click **.sangphai**, the translateX value in **.owl-wrapper&#39;s transform** will be reduced by amount of **1170px**
</details>
# 答案1
**得分**: 0
我只能建议你拆分这个功能,并创建单独的函数来处理它。另外,稍微加入一些状态管理可能也会有所帮助。
为维护 x 值添加状态和单独的函数:
```javascript
const [translateX, setTranslateX] = useState(0);
const handlePrevClick = () => {
setTranslateX(translateX + 1170);
};
const handleNextClick = () => {
setTranslateX(translateX - 1170);
};

然后更新具有 .slider-carousel 类的 div,以使用这个状态值:

<div className="owl-wrapper" style={{
  width: 11700,
  left: 0,
  display: "block",
  transition: `all 400ms ease 0s`,
  transform: `translate3d(${translateX}px, 0, 0)`,
}}>

最后,在不同的事件上调用单独的函数:

<div className="sangtrai owl-prev" onClick={handlePrevClick}></div>
<div className="sangphai owl-next" onClick={handleNextClick}></div>

注意: 我没有测试上面的代码,但如果它不能如预期般工作,这可能是一个可以帮到你的方法。祝好运!

英文:

I can only recommend that you split this functionality and create separate functions for this. Also a touch of state management may help here as well.

Add state and separate functions for maintaining the x value:

  const [translateX, setTranslateX] = useState(0);
const handlePrevClick = () =&gt; {
setTranslateX(translateX + 1170);
};
const handleNextClick = () =&gt; {
setTranslateX(translateX - 1170);
};

Then update the div with .slider-carousel to use this state value:

  &lt;div className=&quot;owl-wrapper&quot; style={{
width: 11700,
left: 0,
display: &quot;block&quot;,
transition: `all 400ms ease 0s`,
transform: `translate3d(${translateX}px, 0, 0)`,
}}&gt;

and finally call seperate functions on the seperate events:

&lt;div className=&quot;sangtrai owl-prev&quot; onClick={handlePrevClick}&gt;&lt;/div&gt;
&lt;div className=&quot;sangphai owl-next&quot; onClick={handleNextClick}&gt;&lt;/div&gt;

Note: I haven't tested the code above but it may be an approach that can help if it doesn't work as expected. Cheers!

huangapple
  • 本文由 发表于 2023年5月25日 11:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328767.html
匿名

发表评论

匿名网友

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

确定