Next.js 点击时渲染一个新的 div。

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

Nextjs render a new div onClick

问题

以下是您提供的内容的中文翻译:

  1. 单击时渲染 div
function zoom_img(event) {
    console.log(event.target);
    console.log(event.target['data-loaded-src'])
    const src = event.target['data-loaded-src']
    
    return (
      <div className='w-screen h-screen fixed flex justify-center items-center'>
        <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
          <Image src={src} fill={true} />
        </div>
      </div>
    )
  }

...

<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={zoom_img}/>
  1. 自定义组件
function Glasses ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      return (
        <div className='w-screen h-screen fixed flex justify-center items-center'>
          <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
            <Image src={src} fill={true} />
          </div>
        </div>
      )
    }
    return (
      <Image src={src} width={width} height={height} alt={alt} onClick={enlrg} />
    )
  }


...

<Glasses src={'/gafas.png'} width={150} height={150} alt='Gafas 1' />
  1. 单击现有的 div 以移除隐藏类
function Glasses2 ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      let x = document.getElementById('temp');
      x.classList.remove('hidden');
      x.classList.add('flex');
    }
    return (
      <Image src={src} width={width} height={height} alt={alt} onClick={enlrg} />
    )
  }


...

<Glasses2 src={'/gafas.png'} width={150} height={150} alt='Gafas 1' />
<div id='temp' className='w-screen h-screen fixed hidden justify-center items-center'>
  <div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
    <Image src={'/gafas.png'} fill={true} />
  </div>
</div>
  1. 这个方法确实可行,但我失去了 Next.js 图像优化
function zoom_img (event) {
    console.log(event.target);
    console.log(event.target['data-loaded-src'])
    const src = event.target['data-loaded-src']
    const new_cont = document.createElement('div');
    const new_div = document.createElement('div');
    const main = document.getElementById('main');
    new_cont.classList.add('w-screen', 'h-screen', 'fixed', 'flex', 'justify-center', 'items-center');
    new_div.classList.add('w-9/12', 'h-5/6', 'bg-slate-200', 'opacity-50');
    // 在这里添加 img 标签
    new_cont.appendChild(new_div);
    main.appendChild(new_cont);
  }


...

<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={zoom_img} />

在每种方法中,您都可以在控制台中看到日志,但只有第四种方法中会看到新的 div。是否第四种方法是唯一正确的方式?如果可能的话,我更喜欢使用 Image 组件。您认为哪种方法更好?

英文:

I have an image and I want to create a new div (on click) with a larger version of the same image.

I've tried four approaches:

  1. Render div on Click
function zoom_img (event) {
    console.log(event.target);
    console.log(event.target[&#39;data-loaded-src&#39;])
    const src = event.target[&#39;data-loaded-src&#39;]
    
    return (
      &lt;div className=&#39;w-screen h-screen fixed flex justify-center items-center&#39;&gt;
        &lt;div className=&#39;w-9/12 h-5/6 bg-slate-200 opacity-50&#39;&gt;
          &lt;Image src={src} fill={true} /&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    )
  }

...

&lt;Image src={&#39;/gafas.png&#39;} width={150} height={150} alt=&#39;Gafas 1&#39; onClick={zoom_img}/&gt;
  1. Custom component
function Glasses ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      return (
        &lt;div className=&#39;w-screen h-screen fixed flex justify-center items-center&#39;&gt;
          &lt;div className=&#39;w-9/12 h-5/6 bg-slate-200 opacity-50&#39;&gt;
            &lt;Image src={src} fill={true} /&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      )
    }
    return (
      &lt;Image src={src} width={width} height={height} alt={alt} onClick={enlrg} /&gt;
    )
  }


...

&lt;Glasses src={&#39;/gafas.png&#39;} width={150} height={150} alt=&#39;Gafas 1&#39; /&gt;
  1. Existing div onClick remove hidden class
function Glasses2 ({src, height, width, alt}) {
    function enlrg() {
      console.log(src);
      let x = document.getElementById(&#39;temp&#39;);
      x.classList.remove(&#39;hidden&#39;);
      x.classList.add(&#39;flex&#39;);
    }
    return (
      &lt;Image src={src} width={width} height={height} alt={alt} onClick={enlrg} /&gt;
    )
  }


...

&lt;Glasses2 src={&#39;/gafas.png&#39;} width={150} height={150} alt=&#39;Gafas 1&#39; /&gt;
&lt;div id=&#39;temp&#39; className=&#39;w-screen h-screen fixed hidden justify-center items-center&#39;&gt;
  &lt;div className=&#39;w-9/12 h-5/6 bg-slate-200 opacity-50&#39;&gt;
    &lt;Image src={&#39;/gafas.png&#39;} fill={true} /&gt;
  &lt;/div&gt;
&lt;/div&gt;
  1. This one DID work but I'm loosing nextjs image optimization
function zoom_img (event) {
    console.log(event.target);
    console.log(event.target[&#39;data-loaded-src&#39;])
    const src = event.target[&#39;data-loaded-src&#39;]
    const new_cont = document.createElement(&#39;div&#39;);
    const new_div = document.createElement(&#39;div&#39;);
    const main = document.getElementById(&#39;main&#39;);
    new_cont.classList.add(&#39;w-screen&#39;, &#39;h-screen&#39;, &#39;fixed&#39;, &#39;flex&#39;, &#39;justify-center&#39;, &#39;items-center&#39;);
    new_div.classList.add(&#39;w-9/12&#39;, &#39;h-5/6&#39;, &#39;bg-slate-200&#39;, &#39;opacity-50&#39;);
    // add img tag here
    new_cont.appendChild(new_div);
    main.appendChild(new_cont);
  }


...

&lt;Image src={&#39;/gafas.png&#39;} width={150} height={150} alt=&#39;Gafas 1&#39; onClick={zoom_img} /&gt;

In every approach I DO get to see the log in the console but only in approach 4 I see the new div.

Is approach 4 the only correct way? I would prefer to use the Image component if possible. What do you guys think?

答案1

得分: 1

你可以尝试类似这样的方式:

设置钩子:

[zoomImage, setZoomImage] = useState(false)
<Image src={'/gafas.png'} width={150} height={150} alt={'Gafas 1'} onClick={() => setZoomImage(true)}/>

// 放大后的图片

<div className={`${zoomImage ? 'block' : 'hidden'} w-screen h-screen fixed flex justify-center items-center`}>
<div className='w-9/12 h-5/6 bg-slate-200 opacity-50'>
   <Image src={src} fill={true} />
</div>
</div>
英文:

You can try something like this:

Set the hook:

[zoomImage, setZoomImage] = useState(false)
&lt;Image src={&#39;/gafas.png&#39;} width={150} height={150} alt=&#39;Gafas 1&#39; onClick={() =&gt; setZoomImage(true)}/&gt;

// zoomed image
&lt;div className={`${zoomImage ? &#39;block&#39; : &#39;hidden&#39;} w-screen h-screen fixed flex justify-center items-center`}&gt;
&lt;div className=&#39;w-9/12 h-5/6 bg-slate-200 opacity-50&#39;&gt;
   &lt;Image src={src} fill={true} /&gt;
&lt;/div&gt;
&lt;/div&gt;

In this code you are actually telling Next.JS that whenever user clicks that image, change variable zoomImage to true and as a result that bigger image that is hidden by default, will have display block. (I supposed you're using tailwindcss)

答案2

得分: 0

谢谢 @Atena Dadkhah。你的答案完美地解决了问题。

在项目中,我不只有一个图像,而是有一堆图像,所以最终的代码看起来像这样:

function enlarge(e) {
    setLrg(true);
    let img = document.getElementById('largerImage'),
        src = e.target['data-loaded-src'];
    img['src'] = src;
}

然后是一堆类似这样的图像:

<Image src={'/gafas.png'} width={150} height={150} alt='Gafas 1' onClick={enlarge} className='cursor-zoom-in' />

接着是隐藏的 div:

<div className={`${lrg ? 'flex' : 'hidden'} w-screen h-screen fixed justify-center items-center`}>
  <div className='w-9/12 h-5/6 bg-slate-200 flex justify-center items-center relative'>
    <Image id='largerImage' fill={true} />
  </div>
</div>

仍然缺少关闭 div 的行为,但应该很容易添加。

再次感谢 Next.js 点击时渲染一个新的 div。

英文:

Thank you very much @Atena Dadkhah. Your answer worked perfectly.

In the project a have not one but a bunch of images so the final code looked like this:

function enlarge(e) {
    setLrg(true);
    let img = document.getElementById(&#39;largerImage&#39;),
        src = e.target[&#39;data-loaded-src&#39;];
    img[&#39;src&#39;] = src;
  }

Then a bunch of images like this:

&lt;Image src={&#39;/gafas.png&#39;} width={150} height={150} alt=&#39;Gafas 1&#39; onClick={enlarge} className=&#39;cursor-zoom-in&#39; /&gt;

Then the hidden div:

&lt;div className={`${lrg ? &#39;flex&#39; : &#39;hidden&#39;} w-screen h-screen fixed justify-center items-center`}&gt;
  &lt;div className=&#39;w-9/12 h-5/6 bg-slate-200 flex justify-center items-center relative&#39;&gt;
    &lt;Image id=&#39;largerImage&#39; fill={true} /&gt;
  &lt;/div&gt;
&lt;/div&gt;

It's still missing the behaviour for closing the div but that should be easy to add.

Once again. Thank you Next.js 点击时渲染一个新的 div。

huangapple
  • 本文由 发表于 2023年5月8日 02:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195505.html
匿名

发表评论

匿名网友

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

确定